数组操作
数组操作
numpy.ndarray.shape
表示数组的维度,返回一个元组,这个元组的长度就是维度的数组,即ndim
属性(秩)。
import numpy as np
x = np.array([1,2,9,4,5,6,7,8])
print(x.shape) #(8,)
x.shape = [2,4] #定义数组维度,可自定义几行几列
print(x)
# [[1 2 9 4]
# [5 6 7 8]]
`numpy.ndarray.flat`将数组转换为一维的迭代器,可以用for访问数组每一个元素。
import numpy as np
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)
# <numpy.flatiter object at 0x0000020F9BA10C60>
for i in y:
print(i, end=' ')
# 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[3] = 0 #
print(end='\n')
print(x)
# [[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'])`将数组的副本转换为一维数组,并返回。
order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。
import numpy as np # 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)
# [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[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]]
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]
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]]
数组转置
numpy.transpose(a, axes=None)
Permute the dimensions of an array.
numpy.ndarray.T
Same as self.transpose()
,except that self is returned if self.ndim<2
.
import numpy as np
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[0.21 3.52 9.31 8.6 4.94]
# [1.67 0.14 1.76 5.91 1.36]
# [3.48 9.05 5.76 3.41 5.94]
# [9.25 6.12 7.42 7.8 4.18]
# [2.79 5.77 4.63 1.06 2.07]]
y = x.T
print(y)
# [[0.21 1.67 3.48 9.25 2.79]
# [3.52 0.14 9.05 6.12 5.77]
# [9.31 1.76 5.76 7.42 4.63]
# [8.6 5.91 3.41 7.8 1.06]
# [4.94 1.36 5.94 4.18 2.07]]
y = np.transpose(x)
print(y)
# [[0.21 1.67 3.48 9.25 2.79]
# [3.52 0.14 9.05 6.12 5.77]
# [9.31 1.76 5.76 7.42 4.63]
# [8.6 5.91 3.41 7.8 1.06]
# [4.94 1.36 5.94 4.18 2.07]]
更改维度
numpy.newaxis = None None
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
print(x) # [1 2 9 4 5 6 7 8]
y = x[np.newaxis, :]
print(y.shape) # (1, 8)
print(y) # [[1 2 9 4 5 6 7 8]]
y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)
# [[1]
# [2]
# [9]
# [4]
# [5]
# [6]
# [7]
# [8]]
`numpy.squeeze(a, axis=None)`从数组的形状中删除单维度条目,即吧shape中的1的维度去掉。
`a`表示输入的数组;
`axis`用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错。
import numpy as np
x = np.array([[[0], [1], [2]]])
print(x.shape)
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)
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
import numpy as np
import matplotlib.pyplot as plt
x = np.array([[1, 4, 9, 16, 25]])
x = np.squeeze(x)
print(x.shape) # (5, )
plt.plot(x)
plt.show()
数组结合
数组之间可以进行拼接操作
numpy.concatenate((a1, a2, ...), axis=1, out=None)
Join a sequence of arrays along an existing axis.
import numpy as np
# 拼接前和拼接后的维数不变。如原来x, y都是一维时,拼接后也是一维。
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)
# [1 2 3 7 8 9]
z = np.concatenate([x, y], axis=0)
print(z)
# [1 2 3 7 8 9]
`numpy.stack(arrays, axis=1, out=None)`Join a sequence of arrays along a new axis.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y]) # stack是增加维度的拼接
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y], axis=1)
print(z.shape) # (3, 2)
print(z)
# [[1 7]
# [2 8]
# [3 9]]
`hstack(),vstack()`分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于`concatenate`,用于在已有轴上进行操作。
import numpy as np
a = np.hstack([np.array([1, 2, 3, 4]), 5])
print(a) # [1 2 3 4 5]
a = np.concatenate([np.array([1, 2, 3, 4]), 5])
print(a)
# ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
数组拆分
numpy.split(ary, indices_or_sections, axis=0)
Split an array into multiple sub-arrays as views into ary.
import numpy as np # 垂直切分是按数组高度切分
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]])]
y = np.split(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
y = np.vsplit(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.split(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.vsplit(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=0)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
import numpy as np # 水平切分是按宽度切分
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]])]
y = np.split(x, 2, axis=1)
print(y)
# [array([[11, 12],
# [16, 17],
# [21, 22]]), array([[13, 14],
# [18, 19],
# [23, 24]])]
y = np.hsplit(x, [3])
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [3], axis=1)
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.hsplit(x, [1, 3])
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
数组平铺
numpy.tile(A, reps)
Construct an array by repeating A the number of times given by reps.
import numpy as np # 将原矩阵横向、纵向复制
x = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
# [3 4]]
y = np.tile(x, (1, 3))
print(y)
# [[1 2 1 2 1 2]
# [3 4 3 4 3 4]]
y = np.tile(x, (3, 1))
print(y)
# [[1 2]
# [3 4]
# [1 2]
# [3 4]
# [1 2]
# [3 4]]
y = np.tile(x, (3, 3))
print(y)
# [[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)
Repeat elements of an array.
axis=0
,沿着y轴复制,实际上增加了行数。
axis=1
,沿着x轴复制,实际上增加了列数。
repeats
,可以为一个数,也可以为一个矩阵。
axis=None
时就会flatten当前矩阵,实际上就是变成了一个行向量。
import numpy as np # 重复数组的元素
x = np.repeat(3, 4)
print(x) # [3 3 3 3]
x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2)
print(y)
# [1 1 2 2 3 3 4 4]
y = np.repeat(x, 2, axis=0)
print(y)
# [[1 2]
# [1 2]
# [3 4]
# [3 4]]
y = np.repeat(x, 2, axis=1)
print(y)
# [[1 1 2 2]
# [3 3 4 4]]
y = np.repeat(x, [2, 3], axis=0)
print(y)
# [[1 2]
# [1 2]
# [3 4]
# [3 4]
# [3 4]]
y = np.repeat(x, [2, 3], axis=1)
print(y)
# [[1 1 2 2 2]
# [3 3 4 4 4]]