Python

※This article is based on Python 3.7.3

章节数据准备 [edit]

此章节后面a的值为下方的初始化值。

>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

基本属性 [edit]

>>> a.shape #(2, 3),返回一个元组,表示每个维度的大小。ndarray对象的尺度,对于矩阵,n行m列
>>> a.shape = (2,3) #shape属性是可写的,这个方法与a.reshape(2,3)效果一样。
>>> a.size #6,ndarray对象元素的个数,相当于.shape中n*m的值
>>> a.ndim #2 秩,即轴的数量或维度的数量
>>> a.itemsize #8 ndarray对象中每个元素的大小,以字节为单位
>>> a.dtype #dtype('int64')ndarray对象的元素类型
>>> a.itemsize #8 ndarray对象中每个元素的大小,以字节为单位
>>> a.flags #ndarray对象的内存信息
  C_CONTIGUOUS : True #数组位于单一的、C 风格的连续区段内
  F_CONTIGUOUS : False #数组位于单一的、Fortran 风格的连续区段内
  OWNDATA : True #数组的内存从其它对象处借用
  WRITEABLE : True #数据区域可写入。 将它设置为flase会锁定数据,使其只读
  ALIGNED : True #数据和任何元素会为硬件适当对齐
  WRITEBACKIFCOPY : False #
  UPDATEIFCOPY : False #这个数组是另一数组的副本。当这个数组释放时,源数组会由这个数组中的元素更新

最大最小值 [edit]

>>> a.max()#最大值
6
>>> a.min()#最小值
1
>>> a.max(axis = 0)# axis=0 行方向最大值,即获得每列的最大值,若axis=1,则取列方向最大值,即获得每行的最大值
array([4, 5, 6])
>>> a.min(axis = 1)
array([1, 4])
>>> a.argmax(axis=1)# 最大值元素所在的位置
array([2, 2])

平均值 [edit]

平均值mean() [edit]

获得矩阵中元素的平均值可以通过函数mean()。同样地,可以获得整个矩阵、行或列的平均值。

>>> a.mean() #3.5 即所有元素的平均值。
>>> a.mean(axis = 0)#行方向均值,即每列的均值。
array([2.5, 3.5, 4.5])

加权平均值numpy.average() [edit]

是由每个分量乘以反映其重要性的因子得到的平均值。 numpy.average()函数根据在另一个数组中给出的各自的权重计算数组中元素的加权平均值。 该函数可以接受一个轴参数。 如果没有指定轴,则数组会被展开。比如数组[1,2,3,4]和相应的权重[4,3,2,1],则加权平均值 = (1*4+2*3+3*2+4*1)/(4+3+2+1)

>>> np.average(a)#如果不指定权值,则和平均值mean()效果一样。
3.5
>>> np.average(a,weights = [[1,2,3],[3,2,1]])#返回加权平均值。
3.5
>>> np.average(a,weights = [[1,2,3],[3,2,1]],returned = True)#返回加权平均值和权重的和。
(3.5, 12.0)

中值numpy.median() [edit]

中值定义为将数据样本由小到大排序,取最中间的那一个,如果中间的数字有两个,取它们的平均值。

>>> np.median(a)#3.5

数组操作 [edit]

修改数据 [edit]

#一维数组
a=np.arange(0,10,1)**2
>>>array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

a[-1]=100 #单个赋值
>>>array([  0,   1,   4,   9,  16,  25,  36,  49,  64, 100])

a[1:4]=100 #批量赋值
>>>array([  0, 100, 100, 100,  16,  25,  36,  49,  64, 100])

b=[np.sqrt(np.abs(i)) for i in a] #通过a循环遍历赋值
print(b)
>>>[0.0, 10.0, 10.0, 10.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10.0]

np.append() [edit]

返回新数组,不影响原来的数组

>>> a = np.arange(8)
>>> a
array([1, 2, 3, 4, 5, 6, 7])

>>>np.append(a, 8)
array([1, 2, 3, 4, 5, 6, 7, 8])

>>>np.append(a, [9,10])
array([1, 2, 3, 4, 5, 6, 7, 9, 10])

>>> x
array([1, 2, 3, 4, 5, 6, 7])

注意一维数组和多维数组追加的区别:

n维数组要保持维度,只能追加n维数组,不然报错;

多维数组追加一维数组后,会变成一维数组(axis=None,先展平)

np.where [edit]

np.where 函数是三元表达式,有下面两种用法

np.where(condition,x,y) [edit]

当where内有三个参数时,第一个参数表示条件,当条件成立时where方法返回x,当条件不成立时where返回y

np.where(condition) [edit]

当where内只有一个参数时,那个参数表示条件,当条件成立时,where返回的是每个符合condition条件元素的坐标,返回的是以元组的形式

返回的是坐标

import numpy as np
 
a = np.array([2, 4, 6, 8, 10])
#一维矩阵
result_1 = np.where(a > 5)
print(result_1)
 
b = np.random.randn(4, 4)
#二维矩阵
print(b)
result_2 = np.where(b > 0)
print(result_2)

结果

Output from spyder call 'get_namespace_view':
(array([2, 3, 4], dtype=int64),)
[[-0.83362412 -2.23605027  0.15374728  0.70877121]
 [-0.30212209  0.56606258  0.95593288  1.03250978]
 [-0.85764257  1.48541971  0.73199465  1.66331547]
 [-0.22020036  0.46416537 -0.75622715  0.32649036]]
(array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3], dtype=int64), array([2, 3, 1, 2, 3, 1, 2, 3, 1, 3], dtype=int64))

例程 [edit]

使用numpy.where在图片上提取某一位置的坐标,遍历所有点耗费太耗费时间了,通过nump.where获取图片的某一位置坐标

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

print(np.where(a==2))

返回

(array([0], dtype=int64), array([1], dtype=int64))就是第2行第1列

Troubleshooting [edit]

operands could not be broadcast together with shapes [edit]

形状不同 [edit]

a = np.array([[1,2], [3,4]])
b = np.array([10,20,30,40])
c = a+b
# ValueError: operands could not be broadcast together with shapes (2,2) (4,)

错误原因:将一个长度为4的数组加到一个2 x 2的数组上显然是不合法的,因为这两个矩阵的形状不同。

改为

b = np.array([10,20,30,40]).reshape(2,2)
c = a+b

#结果:
array([[11, 22],
       [33, 44]])

维数不同 [edit]

ValueError: operands could not be broadcast together with shapes (3,2) (3,)

列的数量不同导致的

a = a.reshape(3,1)

コメント:



(画像の文字列を入力して下さい)

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS