数组操作
更改形状
numpy.ndarray.shape
import numpy as np
x = np.array(np.arange(8))
print('x is ', x,'\nx\'s shape is ', x.shape)
x.shape = [2,4]
print(x)
x is [0 1 2 3 4 5 6 7]
x's shape is (8,)
[[0 1 2 3]
[4 5 6 7]]
numpy.ndarray.flat
将数组转换为一维的迭代器,这意味着可以用for访问数组的每一个元素,改变生成的一维迭代器的值会改变原来ndarray的值
x = np.array([[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]])
y = x.flat
print(y)
for i in y:
print(i,end=" ")
y[3] = 0
print("\n")
print(x)
<numpy.flatiter object at 0x0000021D3D566C40>
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
[[11 12 13 0 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]
[31 32 33 34 35]]
numpy.ndarray.flatten(order = "C")
flatten()
函数返回的是拷贝
x = np.array([[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]])
y = x.flatten()
print(y)
y[3] = 0
print(x)
[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]
[[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]]
order:‘C’ – 按行,‘F’ – 按列
x = np.array([[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]])
y = x.flatten(order = 'F')
print(y)
[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
35]
numpy.ravel(a, order = "C")
ravel()
函数取默认值时返回的是flattened视图
x = np.array([[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]])
y = np.ravel(x)
print(y)
y[3] = 0
print(x)
[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]
[[11 12 13 0 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]
[31 32 33 34 35]]
ravel(a, order = "F")
返回拷贝
x = np.array([[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]])
y = np.ravel(x, order='F')
print(y)
y[3] = 0
print(x)
[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
35]
[[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]]
numpy.reshape(a, newshape[, order = "C"])
newshape = [rows, -1]
-1的维度会根据其他维度自动确定
x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype) # int32
print(y)
y = np.reshape(x, [3, -1])
print(y)
y = np.reshape(x,[-1,3])
print(y)
int32
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
返回的是视图
y = np.reshape(x,[-1,3])
y[0, 1] = 100
print(x)
[ 0 100 2 3 4 5 6 7 8 9 10 11]
数组转置
numpy.transpose(a, axes = None)
numpy.ndarray.T
x = np.random.rand(5,5)*10
x = np.around(x,2)
print(x)
y = x.T
print(y)
y = np.transpose(x)
print(y)
[[8.81 1.75 6.08 0.74 8.4 ]
[5.18 1.08 5.78 5.85 3.84]
[6.15 1.55 8.24 3.54 9.73]
[5.85 7.15 1.81 9.65 7.13]
[0.73 6.79 5.58 5.97 3.16]]
[[8.81 5.18 6.15 5.85 0.73]
[1.75 1.08 1.55 7.15 6.79]
[6.08 5.78 8.24 1.81 5.58]
[0.74 5.85 3.54 9.65 5.97]
[8.4 3.84 9.73 7.13 3.16]]
[[8.81 5.18 6.15 5.85 0.73]
[1.75 1.08 1.55 7.15 6.79]
[6.08 5.78 8.24 1.81 5.58]
[0.74 5.85 3.54 9.65 5.97]
[8.4 3.84 9.73 7.13 3.16]]
更改维度
numpy.newaxis = None
很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis参数来增加一个维度
x = np.array([1,2,9,4,5,6,7,8])
print(x.shape)
print(x)
y = x[np.newaxis,:]
print(y.shape)
print(y)
y = x[:, np.newaxis, np.newaxis]
print(y.shape)
print(y)
(8,)
[1 2 9 4 5 6 7 8]
(1, 8)
[[1 2 9 4 5 6 7 8]]
(8, 1, 1)
[[[1]]
[[2]]
[[9]]
[[4]]
[[5]]
[[6]]
[[7]]
[[8]]]
numpy.squeeze(a, axis = None)
从数组的形状中删除多余的维度,即把shape中为1的维度去掉axis
用于指定需要删除的维度,在该维度上数组的size为1
用途:在机器学习和深度学习中,通常算法的结果是可以表示向量的数组(即包含两对或以上的方括号形式[[]]),如果直接利用这个数组进行画图可能显示界面为空(见后面的示例)。我们可以利用squeeze()函数将表示向量的数组转换为秩为1的数组,这样利用 matplotlib 库函数画图时,就可以正常的显示结果了
x = np.array([[[0], [1], [2]]])
print(x.shape) # (1, 3, 1)
print(x)
# [[[0]
# [1]
# [2]]]
y = np.squeeze(x)
print(y.shape) # (3,)
print(y) # [0 1 2]
y = np.squeeze(x, axis=0)
print(y.shape) # (3, 1)
print(y)
# [[0]
# [1]
# [2]]
y = np.squeeze(x, axis=2)
print(y.shape) # (1, 3)
print(y) # [[0 1 2]]
y = np.squeeze(x, axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one
(10,)
(1, 10)
[0 1 2 3 4 5 6 7 8 9]
(10,)
(1, 3, 1)
[[[0]
[1]
[2]]]
(3,)
[0 1 2]
(3, 1)
[[0]
[1]
[2]]
(1, 3)
[[0 1 2]]
import matplotlib.pyplot as plt
x = np.array([[1, 4, 9, 16, 25]])
print(x.shape) # (1, 5)
plt.plot(x)
plt.show()
x = np.squeeze(x)
print(x.shape) # (5, )
plt.plot(x)
plt.show()
(1, 5)
(5,)
数组组合
numpy.concatenate((a1,a2,...), axis = 0, out = None)
沿着某个轴axis拼接数组Join a sequence of arrays along an existing axis.拼接得到的数组的维度与原数组一样
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)
z = np.concatenate([x, y], axis=0)
print(z)
[1 2 3 7 8 9]
[1 2 3 7 8 9]
numpy.stack(arrays, axis = 0, out = None)
Join a sequence of arrays along a new axis, 沿着新的轴加入数组,会增加数组的维度
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
z = np.stack([x, y], axis=1)
print(z.shape) # (3, 2)
print(z)
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.stack([x, y])
print(z.shape) # (2, 1, 3)
print(z)
(2, 3)
[[1 2 3]
[7 8 9]]
(3, 2)
[[1 7]
[2 8]
[3 9]]
(2, 1, 3)
[[[1 2 3]]
[[7 8 9]]]
numpy.vstack(tup)
Stack arrays in sequence vertically (row wise).numpy.hstack(tup)
Stack arrays in sequence horizontally (column wise).
hstack(),vstack()分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate,用于在已有轴上进行操作。
数组拆分
numpy.split(array, indices_or sections, axis=0)
Split an array into multiple sub-arrays as views into ary.
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.split(x, [1, 3])
print(y)
y = np.split(x, [1, 3], axis=1)
print(y)
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
[21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
[array([[11],
[16],
[21]]), array([[12, 13],
[17, 18],
[22, 23]]), array([[14],
[19],
[24]])]
numpy.vsplit(ary, indices_or sections)
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
numpy.hsplit(ary, indices_or sections)
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)
[array([[11, 12],
[16, 17],
[21, 22]]), array([[13, 14],
[18, 19],
[23, 24]])]
数组平铺
numpy.tile(A, reps)
Construct an array by repeating A the number of times given by reps.
x = np.array([[1, 2], [3, 4]])
y = np.tile(x, (1, 3))
print(y)
y = np.tile(x, (3, 1))
print(y)
y = np.tile(x, (3, 3))
print(y)
[[1 2 1 2 1 2]
[3 4 3 4 3 4]]
[[1 2]
[3 4]
[1 2]
[3 4]
[1 2]
[3 4]]
[[1 2 1 2 1 2]
[3 4 3 4 3 4]
[1 2 1 2 1 2]
[3 4 3 4 3 4]
[1 2 1 2 1 2]
[3 4 3 4 3 4]]
numpy.repeat(a, repeats, axis = None)
axis = 0
,沿着y轴复制,实际增加了行数axis = 1
,沿着x轴复制,实际上增加了列数repeats
,可以为一个数,也可以为一个矩阵- ⭐
axis = None
时就会flatten当前矩阵,实际上就是变成了一个行向量
x = np.repeat(3, 4)
print(x)
x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2)
y = np.repeat(x, 2, axis=0)
print(y)
y = np.repeat(x, 2, axis=1)
print(y)
y = np.repeat(x, [2, 3], axis=0)
print(y)
[3 3 3 3]
[[1 2]
[1 2]
[3 4]
[3 4]]
[[1 1 2 2]
[3 3 4 4]]
[[1 2]
[1 2]
[3 4]
[3 4]
[3 4]]
添加和删除元素
numpy.unique(ar, return_index = False, return_inverse = False, return_counts = False, axis = None)
查找数组中的唯一元素- return_index:the indices of the input array that give the unique values
- return_inverse:the indices of the unique array that reconstruct the input array
- return_counts:the number of times each unique value comes up in the input array
a=np.array([1,1,2,3,3,4,4])
b=np.unique(a,return_counts=True)
print(b)
(array([1, 2, 3, 4]), array([2, 1, 2, 2], dtype=int64))