Numpy学习——Task02 数组操作
1.改变形状(shape)
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。
(1) shape
通过修改 shape 属性来改变数组的形状。
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,) 查看数组形状
x.shape = [2, 4] #改变shape 列表内数字乘积为8即可
print(x)
# [[1 2 9 4]
# [5 6 7 8]]
(2)flat
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 #此时y 与x 数据指向相同,即改变一者的数据另外一个也会有相同变化
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]]
(3)flatten()
-
numpy.ndarray.flatten([order='C'])
将数组的副本转换为一维数组,并返回。 -
order:‘C’ – 按行,‘F’ – 按列,默认为‘C’
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.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]
(4)reshape()
-
numpy.reshape(a, newshape[, order='C'])
在不更改数据的情况下为数组赋予新的形状。 -
reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
-
reshape()函数当参数newshape = -1时,表示将数组降为一维。
import numpy as np x = np.arange(12) y = np.reshape(x, [3, 4]) print(y.dtype) # int32 print(y) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] y = np.reshape(x, [3, -1]) print(y) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] y = np.reshape(x,[-1,3]) print(y) # [[ 0 1 2] # [ 3 4 5] # [ 6 7 8] # [ 9 10 11]] y[0, 1] = 10 print(x) # [ 0 10 2 3 4 5 6 7 8 9 10 11](改变x去reshape后y中的值,x对应元素也改变) x = np.random.randint(12, size=[2, 2, 3]) print(x) # [[[11 9 1] # [ 1 10 3]] # # [[ 0 6 1] # [ 4 11 3]]] y = np.reshape(x, -1) print(y) # [11 9 1 1 10 3 0 6 1 4 11 3]
2.数组转置
transpose / T
-
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.round(x, 2)#四舍五入,保留两位小数 print(x) # [[6.74 8.46 6.74 5.45 1.25] # [3.54 3.49 8.62 1.94 9.92] # [5.03 7.22 1.6 8.7 0.43] # [7.5 7.31 5.69 9.67 7.65] # [1.8 9.52 2.78 5.87 4.14]] y = x.T print(y) # [[6.74 3.54 5.03 7.5 1.8 ] # [8.46 3.49 7.22 7.31 9.52] # [6.74 8.62 1.6 5.69 2.78] # [5.45 1.94 8.7 9.67 5.87] # [1.25 9.92 0.43 7.65 4.14]] y = np.transpose(x) print(y) # [[6.74 3.54 5.03 7.5 1.8 ] # [8.46 3.49 7.22 7.31 9.52] # [6.74 8.62 1.6 5.69 2.78] # [5.45 1.94 8.7 9.67 5.87] # [1.25 9.92 0.43 7.65 4.14]]
3.更改维度
(1)增加维度
很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis参数来增加一个维度。
-
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]]
(2)删除维度
-
numpy.squeeze(a, axis=None)
从数组的形状中删除单维度条目,即把shape中为1的维度去掉。 -
a表示输入的数组;
-
axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
import numpy as np 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