侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python Numpy 数组的初始化和基本操作

Python  /  管理员 发布于 7年前   348

Python 是一种高级的,动态的,多泛型的编程语言。Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代码来实现一个非常强大的想法。

一.基础:

Numpy的主要数据类型是ndarray,即多维数组。它有以下几个属性:

ndarray.ndim:数组的维数
ndarray.shape:数组每一维的大小
ndarray.size:数组中全部元素的数量
ndarray.dtype:数组中元素的类型(numpy.int32, numpy.int16, and numpy.float64等)
ndarray.itemsize:每个元素占几个字节

例子:

>>> import numpy as np>>> a = np.arange(15).reshape(3, 5)>>> aarray([[ 0, 1, 2, 3, 4],    [ 5, 6, 7, 8, 9],    [10, 11, 12, 13, 14]])>>> a.shape(3, 5)>>> a.ndim2>>> a.dtype.name'int64'>>> a.itemsize8>>> a.size15>>> type(a)<type 'numpy.ndarray'>>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)<type 'numpy.ndarray'>

二.创建数组:

使用array函数讲tuple和list转为array:

>>> import numpy as np>>> a = np.array([2,3,4])>>> aarray([2, 3, 4])>>> a.dtypedtype('int64')>>> b = np.array([1.2, 3.5, 5.1])>>> b.dtypedtype('float64')

多维数组:

>>> b = np.array([(1.5,2,3), (4,5,6)])>>> barray([[ 1.5, 2. , 3. ],    [ 4. , 5. , 6. ]])

生成数组的同时指定类型:

>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )>>> carray([[ 1.+0.j, 2.+0.j],    [ 3.+0.j, 4.+0.j]])

生成数组并赋为特殊值:

ones:全1
zeros:全0
empty:随机数,取决于内存情况

>>> np.zeros( (3,4) )array([[ 0., 0., 0., 0.],    [ 0., 0., 0., 0.],    [ 0., 0., 0., 0.]])>>> np.ones( (2,3,4), dtype=np.int16 )        # dtype can also be specifiedarray([[[ 1, 1, 1, 1],    [ 1, 1, 1, 1],    [ 1, 1, 1, 1]],    [[ 1, 1, 1, 1],    [ 1, 1, 1, 1],    [ 1, 1, 1, 1]]], dtype=int16)>>> np.empty( (2,3) )     # uninitialized, output may varyarray([[ 3.73603959e-262,  6.02658058e-154,  6.55490914e-260],    [ 5.30498948e-313,  3.14673309e-307,  1.00000000e+000]])

生成均匀分布的array:

arange(最小值,最大值,步长)(左闭右开)
linspace(最小值,最大值,元素数量)

>>> np.arange( 10, 30, 5 )array([10, 15, 20, 25])>>> np.arange( 0, 2, 0.3 )         # it accepts float argumentsarray([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])>>> np.linspace( 0, 2, 9 )         # 9 numbers from 0 to 2array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])>>> x = np.linspace( 0, 2*pi, 100 )    # useful to evaluate function at lots of points

三.基本运算:

整个array按顺序参与运算:

>>> a = np.array( [20,30,40,50] )>>> b = np.arange( 4 )>>> barray([0, 1, 2, 3])>>> c = a-b>>> carray([20, 29, 38, 47])>>> b**2array([0, 1, 4, 9])>>> 10*np.sin(a)array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])>>> a<35array([ True, True, False, False], dtype=bool)

两个二维使用*符号仍然是按位置一对一相乘,如果想表示矩阵乘法,使用dot:

>>> A = np.array( [[1,1],...       [0,1]] )>>> B = np.array( [[2,0],...       [3,4]] )>>> A*B # elementwise productarray([[2, 0],    [0, 4]])>>> A.dot(B)          # matrix productarray([[5, 4],    [3, 4]])>>> np.dot(A, B)        # another matrix productarray([[5, 4],    [3, 4]])

内置函数(min,max,sum),同时可以使用axis指定对哪一维进行操作:

>>> b = np.arange(12).reshape(3,4)>>> barray([[ 0, 1, 2, 3],    [ 4, 5, 6, 7],    [ 8, 9, 10, 11]])>>>>>> b.sum(axis=0)  # sum of each columnarray([12, 15, 18, 21])>>>>>> b.min(axis=1)  # min of each rowarray([0, 4, 8])>>>>>> b.cumsum(axis=1) # cumulative sum along each rowarray([[ 0, 1, 3, 6],    [ 4, 9, 15, 22],    [ 8, 17, 27, 38]])

Numpy同时提供很多全局函数

>>> B = np.arange(3)>>> Barray([0, 1, 2])>>> np.exp(B)array([ 1.    , 2.71828183, 7.3890561 ])>>> np.sqrt(B)array([ 0.    , 1.    , 1.41421356])>>> C = np.array([2., -1., 4.])>>> np.add(B, C)array([ 2., 0., 6.])

四.寻址,索引和遍历:

一维数组的遍历语法和python list类似:

>>> a = np.arange(10)**3>>> aarray([ 0,  1,  8, 27, 64, 125, 216, 343, 512, 729])>>> a[2]8>>> a[2:5]array([ 8, 27, 64])>>> a[:6:2] = -1000  # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000>>> aarray([-1000,   1, -1000,  27, -1000,  125,  216,  343,  512,  729])>>> a[ : :-1]     # reversed aarray([ 729,  512,  343,  216,  125, -1000,  27, -1000,   1, -1000])>>> for i in a:...   print(i**(1/3.))...nan1.0nan3.0nan5.06.07.08.09.0

多维数组的访问通过给每一维指定一个索引,顺序是先高维再低维:

>>> def f(x,y):...   return 10*x+y...>>> b = np.fromfunction(f,(5,4),dtype=int)>>> barray([[ 0, 1, 2, 3],    [10, 11, 12, 13],    [20, 21, 22, 23],    [30, 31, 32, 33],    [40, 41, 42, 43]])>>> b[2,3]23>>> b[0:5, 1]# each row in the second column of barray([ 1, 11, 21, 31, 41])>>> b[ : ,1]# equivalent to the previous examplearray([ 1, 11, 21, 31, 41])>>> b[1:3, : ]           # each column in the second and third row of barray([[10, 11, 12, 13],    [20, 21, 22, 23]])When fewer indices are provided than the number of axes, the missing indices are considered complete slices:>>>>>> b[-1]     # the last row. Equivalent to b[-1,:]array([40, 41, 42, 43])

…符号表示将所有未指定索引的维度均赋为 : ,:在python中表示该维所有元素:

>>> c = np.array( [[[ 0, 1, 2],        # a 3D array (two stacked 2D arrays)...         [ 10, 12, 13]],...        [[100,101,102],...         [110,112,113]]])>>> c.shape(2, 2, 3)>>> c[1,...]      # same as c[1,:,:] or c[1]array([[100, 101, 102],    [110, 112, 113]])>>> c[...,2]      # same as c[:,:,2]array([[ 2, 13],    [102, 113]])

遍历:

如果只想遍历整个array可以直接使用:

>>> for row in b:...   print(row)...[0 1 2 3][10 11 12 13][20 21 22 23][30 31 32 33][40 41 42 43]

但是如果要对每个元素进行操作,就要使用flat属性,这是一个遍历整个数组的迭代器

>>> for element in b.flat:...   print(element)...

总结

以上所述是小编给大家介绍的Python Numpy 数组的初始化和基本操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    python用户管理系统
    下一条:
    python 中的list和array的不同之处及转换问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客