Python Numpy

ndarray 对象

同类型元素的N维数组

创建

方法 说明
np.array(object) 由object(常见为list)创建ndarray数组对象
np.asarray(object) 由object(常见为list)创建ndarray数组对象
由list创建时,二者一样;由ndarray创建时,array会新复制出一个ndarray,asarray则仍引用原来的ndarray
np.empty(shape, dtype) 创建一个指定形状、数据类型、未初始化的数组,shape as tuple
注意,empty指的是未初始化,数组元素值为随机值,并不是0值
np.zeros(shape, dtype) 创建一个指定形状、指定数据类型,由0填充的数组
np.ones(shape, dtype) 创建一个指定形状、指定数据类型,由1填充的数组
np.arange(start, stop, step, dtype) 根据startstopstep,生成一个指定类型的定步长数组
np.linspace(start, stop, num=50, endpoint=True, retstep=False) 根据startstopnum,生成一个等差数列数组,endpoint表明是否包含stop值,retstep表明结果是否返回公差
np.logspace(start, stop, num=50, endpoint=True, base=10.0) 根据startstopnum,生成一个等比数列数组,序列起始值为base ** start,序列的终止值为:base ** stop
np.random.rand(d0, d1, ..., dn) 随机值[0, 1)序列,d给出shape
ndarray.copy() 深拷贝

属性

方法 说明
ndarray.shape 数组各维度的大小(是数量不是阶数)
ndarray.size 数组元素的总个数(等于shape中各值相乘)
ndarray.dtype 数组的元素类型
ndarray.ndim 轴的数量,或维度的数量rank。轴编号从0ndim-1

轴的概念

很多操作可以声明axisaxis=i,代表沿着第i个轴(下标变化)的方向,进行操作。
例如,对于二维数组axis=0表示沿着第0轴进行操作,即固定其他轴,遍历对第一个下标,这样相当于每次处理了一列的数据,即按操作;axis=1表示沿着第1轴进行操作,即按操作。

更加具体的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
arr = np.arange(16).reshape(2, 4, 2)
'''
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],

[[ 8, 9],
[10, 11],
[12, 13],
[14, 15]]])
'''
arr.sum(axis=0)
'''
array([[ 8, 10],
[12, 14],
[16, 18],
[20, 22]])
'''

例子中,arrshape(2, 4, 2)arrshape的下标为(0, 1, 2)
axis=0,即对shape下标的第一个位置进行处理。由(2, 4, 2)可知,每次处理2个,固定(4, 2)作为最后结果shape
求算时,arr[0][a][b]+arr[1][a][b]作为结果s[a][b]的值。

切片

注意:切片数组返回一个view,而非copy;这与python list不同,python list的切片是copy

方法 说明
arr[idx] 一维索引
arr[start:stop:step] 一维,切片索引
arr[start:] 一维,start直至最后
arr[np.array([(idx1, idx2), (idx3, idx4)])] 通过一维生成二维
arr[1] 二维,第二行
arr[1, 1] 二维,第二行、第二列
arr[:, 1] 二维,所有行、第二列
arr[-1, :] 二维,最后行、所有列
arr[..., 1] 二维,所有行、第二列
对于多维,如3darray,arr[..., 1] == arr[:, :, 1]
arr[..., 1:] 二维,所有行、第二至最后列
arr[0:1, 1:2] 二维,一至二行、二至三列
arr[[0,1,2], [0,1,0]] 二维,数组索引,分别指定idx_x、idx_y。即,arr[(0, 0), (1, 1), (2, 0)]
arr[arr > 5] 布尔索引,其中arr > 5会给出一个和arr同shape的布尔数组,按这个布尔数组索引
arr[[4, 2, 1, 7]] 花式索引,索引为数组。如果目标是一维数组,那么索引的结果就是对应位置的元素;如果目标是二维数组,那么就是对应下标的行。
np.where(condition) np.asarray(condition).nonzero(),False作为0。由于nonzero返回的是索引,选择时,使用arr[np.where(condition)]
np.where(condition, x, y) 满足条件(condition),输出x(对应位置内容),不满足输出y(对应位置内容)。相当于对zip(condition, x, y)进行选择。zip将各个对象打包(同idx的在同一批处理),如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。zip(*)解压出各个对象

数字后的:表示范围;单独的:表示所有

调整形状

方法 说明
ndarray.reshape() 调整数组shape,返回view。shape中-1表示(根据其他维度设置)自动决定数量
np.resize(arr, shape) 调整数组shape,返回copy
ndarray.flat 返回数组元素迭代器(可直接索引arr.flat[3]
ndarray.ravel() 返回数组flat后的view
ndarray.flatten() 返回数组flat后的copy(默认参数为”C”,即按照行flat;设置参数为”F”,可按照列falt)
ndarray.T 转置数组
np.transpose(arr, axes) 转置数组,axes指定转置后的维度下标顺序。如3darray,默认axes为[0, 1, 2],当指定为[1, 0, 2]时,所有元素第一下标和第二下标互换位置
np.expand_dims(arr, axis) 扩展维度,axis为新轴插入的位置。例如,一个shape为(2, 2)的数组,经过axis=0扩展,新shape为(1, 2, 2)
np.squeeze(arr, axis) 从给定数组,把shape中为1的维度去掉。例如,shape(1, 2, 2) -> shape(2, 2)。可通过axis参数指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错

拼接

方法 说明
np.concatenate((a1, a2, ...), axis) axis=0增加行;axis=1增加列;axis=None获得flat
np.vstack(tup) 增加行,入参为tuple,例如(a1, a2, ...)vertical方向
np.hstack(tup) 增加列;horizontal方向
np.r_[ar1, ar2] 增加行,row
np.c_[ar1, ar2] 增加列,column
np.dstack(tup) 会拓展维度,增加维度(深度),depth
np.stack(arrays, axis) 会拓展维度,在新增维度上堆叠。axis指定新增哪个维度,axis=0新增第一个维度;axis=-1新增最后一个维度(dstack)
或者用np.expand_dims拓展维度axis=0后,对行进行拼接,然后transpose

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
a = np.zeros((2, 3), dtype=np.uint64)
'''
[[0 0 0]
[0 0 0]]
'''
b = np.ones((2, 3), dtype=np.uint64)
'''
[[1 1 1]
[1 1 1]]
'''
# a、b的shape可以不完全相同,但需要在待拼接的维度中一致

c1 = np.concatenate((a, b))
c11 = np.vstack((a, b)) #np.r_
'''
[[0 0 0]
[0 0 0]
[1 1 1]
[1 1 1]]
'''
c2 = np.concatenate((a, b), axis=1)
c22 = np.hstack((a, b)) #np.c_
'''
[[0 0 0 1 1 1]
[0 0 0 1 1 1]]
'''

# np.stack会新创建一个维度,位置由axis指定;而上文三个方法不会新增维度
cc1 = np.stack((a, b))
'''
[[[0 0 0]
[0 0 0]]

[[1 1 1]
[1 1 1]]]
'''
print(cc1.shape)
# (2, 2, 3)

# 深度方向堆叠
cc2 = np.stack((a, b), axis=2) #np.dstack()
'''
[[[0 1]
[0 1]
[0 1]]

[[0 1]
[0 1]
[0 1]]]
'''
print(cc2.shape)
# (2, 3, 2)

增删行列

方法 说明
np.append(arr, values, axis=None) 末尾添加,axis可指定添加行、列,shape需保证对齐
np.insert(arr, obj, values, axis) 在指定索引标号obj之前,沿axis指定轴,向数组中插入值values,shape需保证对齐
np.delete(arr, obj, axis) 在指定索引标号obj,沿axis指定轴,删除数组中(某行、列)数据
np.unique(arr, return_index=False, return_inverse=False, return_counts=False) 去除数组中的重复元素

计算函数

方法 说明
np.around(a, decimals) 四舍五入,decimals指定小数点后位数;负值表示round到十位、百位…
np.floor(a) 取地板
np.ceil(a) 取天花板
np.reciprocal(a) 取倒数
np.power(a, b)
np.mod(a) 取余
np.amin(a) np.nanmin(a) ignoring any NaNs
np.amax(a) np.nanmax(a)
np.sum(a) np.nansum(a)
np.percentile(a, q) np.nanpercentile(a, q) q在0~100之间
np.quantile(a, q) np.nanquantile(a, q) q在0~1之间
np.median(a) np.nanmedian(a)
np.mean(a) np.nanmean(a)
np.average(a, weights=arr) 可指定权重,计算加权平均
np.std(a) np.nanstd(a)
np.var(a) np.nanvar(a)
np.dot(a, b) 矩阵乘法,得到矩阵
np.inner(a) 向量内积(对位相乘后相加),得到一个数

排序与索引查询

方法 说明
np.sort(a)
np.argsort(a) 从小到大的排序后数组,对应a的索引值
np.argmax(a) a中最大元素的索引
np.argmin(a) a中最小元素的索引
np.nonzero(a) a中非0元素的索引
np.where(condition) np.asarray(condition).nonzero(),给出符合条件的索引(a包含在条件中)