安徽网站建设信息,域名更新,品牌建设 奖,更换域名wordpress简介#xff1a; 本文介绍了使用 NumPy 进行数据操作的基本步骤#xff0c;包括导入库、创建数组、基本操作等。通过实例演示了如何利用 NumPy 进行数组的创建、索引、切片、变形、级联和切割等操作#xff0c;以及如何应用这些功能在图像处理中进行实际应用。 numpy get st…简介 本文介绍了使用 NumPy 进行数据操作的基本步骤包括导入库、创建数组、基本操作等。通过实例演示了如何利用 NumPy 进行数组的创建、索引、切片、变形、级联和切割等操作以及如何应用这些功能在图像处理中进行实际应用。 numpy get started
导入numpy库并查看numpy版本
In [1]: import numpy as np In [3]: np.__version__ Out[3]: 1.24.3 一、创建ndarray
1.使用np.array()由python list 创建
参数为列表[1,4,2,5,3]
注意
numpy默认ndarray的所有元素的类型是相同的 如果传进去的列表中包含不同的类型则统一为同一类型优先级strfloatint
In [4]: l[1,4,2,5,7] l Out[4]: [1, 4, 2, 5, 7] In [8] type(l) Out[8]: list In [10]: ndnp.array(l) nd Out[10]: array([1, 4, 2, 5, 7]) In [11]: type(nd) Out[11]: numpy.ndarray In [12]: l2[[1,3,5],[2,4,6]] l2 Out[12]: [[1, 3, 5], [2, 4, 6]] In [13]: nd2np.array(l2) nd2 Out[13]: array([[1, 3, 5],[2, 4, 6]]) 方法更多运行更快
In [15]: nd.max() Out[15]: 7 In [16]: xnp.arange(1,10000,1) x Out[16]: array([ 1, 2, 3, ..., 9997, 9998, 9999]) In [17]: %timeit x.sum() 5.21 µs ± 201 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)In [18]: def sum1(x): res0 for i in x: resi return res In [19]: %timeit sum1(x) 952 µs ± 30.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each) 2.使用np的routines函数创建
包含以下常见创建方法 1np.ones(shape,dtypeNone,orderC)
创建个指定形状的全为1的数组
In [2]: np.ones(shape(5,5),dtypenp.int8) Out[2]: array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], dtypeint8) 2)np.zeros(shape,dtypefloat,orderC)
在创建指定形状的全为0的数组
In [3]: np.zeros(shape(2,3,4),dtypenp.float16) Out[3]: array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]], dtypefloat16) 3)np.full(shape,fill_value,dtypeNone,orderC)
再创建指定形状并填充特定值的数组
In [5]: np.full(shape(3,4),fill_value3.14) Out[5]: array([[3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14]]) 4)np.eye(N,Mnone,k0,dtypefloat) 对角线为1其他位置为0
创建一个单位矩阵对角线为1其他位置为0
In [6]: np.eye(N5) Out[6]: array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]]) 5)np.linspace(start,stop,num50,endpointTrue,retstepFalse,dtypeNone)
让它在指定范围内生成指定数量的等间隔数值
In [14]: np.linspace(0,100,num51) Out[14]: array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98., 100.]) 6)np.arange([start,]stop,[stop,]dtypeNone)
让它在指定范围内以指定步长生成数组
In [15]: np.arange(0,100,3) Out[15]: array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48,51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]) 7)np.random.randint(low,highNone,sizeNone,dtypel)
生成指定范围内的随机整数数组
随机一下
In [16]: np.random.randint(0,100,(5,5)) Out[16]: array([[29, 48, 25, 62, 47],[54, 95, 67, 16, 80],[ 5, 1, 97, 32, 40],[55, 44, 71, 21, 2],[ 7, 21, 89, 85, 93]]) 8)np.random.randn(d0,d1,d2,...,dn) 标准正态分布
生成服从标准正态分布的随机数组
In [17]:
#平均值为0方差为1 np.random.randn(4,5) Out[17]: array([[ 0.12986708, 0.38932889, -0.68477293, -0.48738848, -1.29017705],[-0.62040021, 1.77145003, -0.03924932, -0.3581067 , -0.63517962],[ 0.09991903, -1.3522429 , -0.58625064, 1.70491804, -3.71604832],[-0.3656734 , 2.28166109, 0.43121117, 0.74830232, 0.30966411]]) 9np.random.normal(loc0.0,scala1.0,sizeNone) loc:位置 scala:标准差
In [27]: ndnp.random.normal(175,10,size1000).round(2) #round(n):取小数位后n位
nd
. . .
In [28]: nd.mean() Out[28]: 174.88183999999998 In [29]: nd.var() #方差
Out[29]: 98.93779501440001 In [31]: nd.std() #标准差方差开平方
Out[31]: 9.946747961741064 10np.random.random(sizeNone) 生成0到1的随机数左闭右开
In [32]: np.random.random(100) . . .
二、ndarray的属性
4个必记参数ndim:维度 shape:形状各维度的长度 size:总长度 dtype:元素类型
In [33]: nd.ndim Out[33]: 1 In [34]: nd.shape Out[34]: (1000,) In [35]: nd.size Out[35]: 1000 In [36]: nd.dtype Out[36]: dtype(float64) 三、ndarray的基本操作
1.索引
一维与列表完全一致 多维时同理
In [38]: nd2np.random.randint(0,150,size(4,5)) nd2 Out[38]: array([[ 26, 85, 41, 21, 49],[ 27, 2, 51, 55, 34],[133, 78, 63, 52, 135],[ 26, 56, 77, 51, 13]]) In [39]: nd2[1,1] Out[39]:
2
In [40]: nd2[2] Out[40]: array([133, 78, 63, 52, 135]) 根据索引修改数据
2.切片
一维与列表完全一致 多维时同理
In [41]: nd2 Out[41]: array([[ 26, 85, 41, 21, 49],[ 27, 2, 51, 55, 34],[133, 78, 63, 52, 135],[ 26, 56, 77, 51, 13]]) In [43]: nd2[0:3] Out[43]: array([[ 26, 85, 41, 21, 49],[ 27, 2, 51, 55, 34],[133, 78, 63, 52, 135]]) In [45]: nd2[-2:] Out[45]: array([[133, 78, 63, 52, 135],[ 26, 56, 77, 51, 13]]) In [46]: nd2[0:3,0:3] Out[46]: array([[ 26, 85, 41],[ 27, 2, 51],[133, 78, 63]]) 将数据反转例如[1,2,3]----[3,2,1]
In [50]: nd3nd[:10] nd3 Out[50]: array([167.79, 195.33, 178.68, 171.35, 170.08, 154.38, 180.5 , 173.5 ,168.88, 154.37]) In [51]: nd3[::-1] Out[51]: array([154.37, 168.88, 173.5 , 180.5 , 154.38, 170.08, 171.35, 178.68,195.33, 167.79]) 两个::进行切片
In [52]: nd3[::2] #隔一行算一行
Out[52]: array([167.79, 178.68, 170.08, 180.5 , 168.88])