4.NumPy数组操作
4.NumPy数组操作
4.1改变NumPy数组维度
前面已经学习了怎样使用reshape函数,接下来我们来学习展开数组
- ravel
ravel函数完成展平数组的操作。
import numpy as np
b = np.arange(60).reshape(3,4,5)
b
array([[[ 0, 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, 53, 54],
[55, 56, 57, 58, 59]]])
b.ravel()
array([ 0, 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, 53, 54, 55, 56, 57, 58, 59])
- flatten
与ravel函数的功能相同。不过,flatten函数会请求分配内存来保存结果,而ravel函数只是返回数组的一个视图(view)。
b.flatten()
array([ 0, 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, 53, 54, 55, 56, 57, 58, 59])
- 用元组设置维度
除了可以使用reshape函数,我们也可以直接用一个正整数元组来设置数组的维度。
b.shape
(3, 4, 5)
b.shape = (4,15)
b
array([[ 0, 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, 53, 54, 55, 56, 57, 58, 59]])
这样的做法将直接改变所操作的数组,现在数组b成了一个15×4的多维数组。
- transpose
转置矩阵
b.transpose()
array([[ 0, 15, 30, 45],
[ 1, 16, 31, 46],
[ 2, 17, 32, 47],
[ 3, 18, 33, 48],
[ 4, 19, 34, 49],
[ 5, 20, 35, 50],
[ 6, 21, 36, 51],
[ 7, 22, 37, 52],
[ 8, 23, 38, 53],
[ 9, 24, 39, 54],
[10, 25, 40, 55],
[11, 26, 41, 56],
[12, 27, 42, 57],
[13, 28, 43, 58],
[14, 29, 44, 59]])
- resize
resize和reshape函数的功能一样,但resize会直接修改所操作的数组。
b.resize(3,4,5)
b
array([[[ 0, 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, 53, 54],
[55, 56, 57, 58, 59]]])
b.resize(3,20)
b
array([[ 0, 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, 53, 54, 55,
56, 57, 58, 59]])
4.2组合数组
- hstack
将ndarray对象构成的元组作为参数,传给hstack函数,可以将多个数组进行水平组合
a = np.arange(9).reshape(3,3)
a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
b = a * 2
b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
c = a * 3
c
array([[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
np.hstack((a, b, c))
array([[ 0, 1, 2, 0, 2, 4, 0, 3, 6],
[ 3, 4, 5, 6, 8, 10, 9, 12, 15],
[ 6, 7, 8, 12, 14, 16, 18, 21, 24]])
- vstack
垂直组合
np.vstack((a,b,c))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16],
[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
+ concatenate
concatenate 也可以实现组合效果,当指定axis=1时,为水平组合,当指定axis=0时,为垂直组合。
np.concatenate((a,b,c), axis=1)
array([[ 0, 1, 2, 0, 2, 4, 0, 3, 6],
[ 3, 4, 5, 6, 8, 10, 9, 12, 15],
[ 6, 7, 8, 12, 14, 16, 18, 21, 24]])
np.concatenate((a,b,c), axis=0)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16],
[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
- dstack
深度组合,就是将一系列数组沿着纵轴(深度)方向进行层叠组合。如:有若干张二维平面内的图像点阵数据,我们可以将这些图像数据沿纵轴方向层叠在一起。
d = np.dstack((a,b,c))
d
array([[[ 0, 0, 0],
[ 1, 2, 3],
[ 2, 4, 6]],
[[ 3, 6, 9],
[ 4, 8, 12],
[ 5, 10, 15]],
[[ 6, 12, 18],
[ 7, 14, 21],
[ 8, 16, 24]]])
d.shape
(3, 3, 3)
- column_stack
列组合
对于一维数组将按列方向进行组合
x = np.arange(6)
y = x * 2
np.column_stack((x,y))
array([[ 0, 0],
[ 1, 2],
[ 2, 4],
[ 3, 6],
[ 4, 8],
[ 5, 10]])
对于二维数组,column_stack与hstack的效果是相同的:
np.column_stack((a,b,c))
array([[ 0, 1, 2, 0, 2, 4, 0, 3, 6],
[ 3, 4, 5, 6, 8, 10, 9, 12, 15],
[ 6, 7, 8, 12, 14, 16, 18, 21, 24]])
np.column_stack((a,b,c)) == np.hstack((a,b,c))
array([[ True, True, True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True, True, True]])
可以用==运算符来比较两个NumPy数组
- row_stack
对于两个一维数组,将直接层叠起来组合成一个二维数组。
np.row_stack((x,y))
array([[ 0, 1, 2, 3, 4, 5],
[ 0, 2, 4, 6, 8, 10]])
对于二维数组,row_stack与vstack的效果是相同的
np.row_stack((a,b,c))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16],
[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]])
np.row_stack((a,b,c)) == np.vstack((a,b,c))
array([[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True]])
4.3分割数组
- hsplit
水平分割,把数组沿着水平方向分割为多个相同大小的子数组
a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
(d,e,f) = np.hsplit(a, 3)
print(d)
print(e)
print(f)
[[0]
[3]
[6]]
[[1]
[4]
[7]]
[[2]
[5]
[8]]
- vsplit
垂直分割,将把数组沿着垂直方向分割。
(d,e,f) = np.vsplit(a, 3)
print(d)
print(e)
print(f)
[[0 1 2]]
[[3 4 5]]
[[6 7 8]]
- split
split函数用于分割数组,当axis=1时为水平分割,当axis=0时为垂直分割。
(d,e,f) = np.split(a, 3, axis= 1)
print(d)
print(e)
print(f)
[[0]
[3]
[6]]
[[1]
[4]
[7]]
[[2]
[5]
[8]]
(d,e,f) = np.split(a, 3, axis=0)
print(d)
print(e)
print(f)
[[0 1 2]]
[[3 4 5]]
[[6 7 8]]
- dsplit
dsplit函数将按深度方向分割数组。
x = np.arange(120).reshape(4,5,6)
x
array([[[ 0, 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, 53],
[ 54, 55, 56, 57, 58, 59]],
[[ 60, 61, 62, 63, 64, 65],
[ 66, 67, 68, 69, 70, 71],
[ 72, 73, 74, 75, 76, 77],
[ 78, 79, 80, 81, 82, 83],
[ 84, 85, 86, 87, 88, 89]],
[[ 90, 91, 92, 93, 94, 95],
[ 96, 97, 98, 99, 100, 101],
[102, 103, 104, 105, 106, 107],
[108, 109, 110, 111, 112, 113],
[114, 115, 116, 117, 118, 119]]])
np.dsplit(x, 2)
[array([[[ 0, 1, 2],
[ 6, 7, 8],
[ 12, 13, 14],
[ 18, 19, 20],
[ 24, 25, 26]],
[[ 30, 31, 32],
[ 36, 37, 38],
[ 42, 43, 44],
[ 48, 49, 50],
[ 54, 55, 56]],
[[ 60, 61, 62],
[ 66, 67, 68],
[ 72, 73, 74],
[ 78, 79, 80],
[ 84, 85, 86]],
[[ 90, 91, 92],
[ 96, 97, 98],
[102, 103, 104],
[108, 109, 110],
[114, 115, 116]]]),
array([[[ 3, 4, 5],
[ 9, 10, 11],
[ 15, 16, 17],
[ 21, 22, 23],
[ 27, 28, 29]],
[[ 33, 34, 35],
[ 39, 40, 41],
[ 45, 46, 47],
[ 51, 52, 53],
[ 57, 58, 59]],
[[ 63, 64, 65],
[ 69, 70, 71],
[ 75, 76, 77],
[ 81, 82, 83],
[ 87, 88, 89]],
[[ 93, 94, 95],
[ 99, 100, 101],
[105, 106, 107],
[111, 112, 113],
[117, 118, 119]]])]
4.4数组的属性
除了shape和dtype属性以外,ndarray对象还有很多其他的属性。
- ndim - 给出数组的维数,或数组轴的个数。
b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
b.ndim
2
x
array([[[ 0, 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, 53],
[ 54, 55, 56, 57, 58, 59]],
[[ 60, 61, 62, 63, 64, 65],
[ 66, 67, 68, 69, 70, 71],
[ 72, 73, 74, 75, 76, 77],
[ 78, 79, 80, 81, 82, 83],
[ 84, 85, 86, 87, 88, 89]],
[[ 90, 91, 92, 93, 94, 95],
[ 96, 97, 98, 99, 100, 101],
[102, 103, 104, 105, 106, 107],
[108, 109, 110, 111, 112, 113],
[114, 115, 116, 117, 118, 119]]])
x.ndim
3
- size - 给出数组元素的总个数
b.size
9
x.size
120
- itemsize - 给出数组中的元素在内存中所占的字节数
b.itemsize
4
x.itemsize
4
- nbytes - 整个数组所占的存储空间,该属性的值就是itemsize和size属性值的乘积。
b.nbytes
36
b.itemsize * b.size
36
- T - 和transpose函数一样,对数组进行转置
b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
b.T
array([[ 0, 6, 12],
[ 2, 8, 14],
[ 4, 10, 16]])
对于一维数组,其T属性就是原数组
y = np.arange(10)
y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y.T
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- real - 给出复数数组的实部。
z = np.array([1+2j, 2+3j, 3+4j])
z
array([1.+2.j, 2.+3.j, 3.+4.j])
z.real
array([1., 2., 3.])
+ imag - 给出复数数组的虚部。
z.imag
array([2., 3., 4.])
- flat - 返回一个numpy.flatiter对象,这是获得flatiter对象的唯一方式——我们无法访问flatiter的构造函数。这个迭代器可以让我们像遍历一维数组一样去遍历任意的多维数组
b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
f = b.flat
f
for item in f: print(item)
0
2
4
6
8
10
12
14
16
可以用flatiter对象直接获取一个数组元素
b.flat[5]
10
b.flat[1:3]
array([2, 4])
b.flat[[1,3,5]]
array([ 2, 6, 10])
flat属性是一个可赋值的属性。对flat属性赋值将导致整个数组的元素都被覆盖。
b.flat = 7
b
array([[7, 7, 7],
[7, 7, 7],
[7, 7, 7]])
b.flat[[1,3,5]] = 2
b
array([[7, 2, 7],
[2, 7, 2],
[7, 7, 7]])
4.5数组的转换
- tolist - 将NumPy数组转换成Python列表
b
array([[7, 2, 7],
[2, 7, 2],
[7, 7, 7]])
b.tolist()
[[7, 2, 7], [2, 7, 2], [7, 7, 7]]
- astype - 可以在转换数组时指定数据类型
z
array([1.+2.j, 2.+3.j, 3.+4.j])
z.astype(int)
C:\Users\Administrator\AppData\Local\Temp\ipykernel_2760\2528053780.py:1: ComplexWarning: Casting complex values to real discards the imaginary part
z.astype(int)
array([1, 2, 3])
b.astype('complex')
array([[7.+0.j, 2.+0.j, 7.+0.j],
[2.+0.j, 7.+0.j, 2.+0.j],
[7.+0.j, 7.+0.j, 7.+0.j]])