前言
- 通常为了满足计算要求,我们会对数组进行形状变化。本模块会用到
numpy
模块,本中numpy
全部用np
代替,即import numpy as np
。
一、更改数组形状
numpy.ndarray.shape()
- 该函数可表示数组的形状或修改形状。
x = np.array([1,2,3,4])
print(x.shape) # (8,)
x.shape = [2, 2]
print(x)
# [[1, 2]
# [3, 4]]
numpy.ndarray.flat()
- 该函数可把数组转换为一维的迭代器,可通过 for 循环输出。此处生成的是视图,故修改一维迭代器的值时,原数组对应位置的值也会改变。
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y = x.flat
print(y) # <numpy.flatiter object at 0x000001E63CE05960>
for i in y:
print(i, end=' ') # 1 2 3 4 5 6 7 8 9
y[3] = 0
print(x)
# [[1 2 3]
# [0 5 6]
# [7 8 9]]
numpy.ndarray.flatten()
- 该函数可把数组转换成一维数组,此处生成的是副本。
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y1 = x.flatten(order='C') # 按行展开
print(y1) # [1 2 3 4 5 6 7 8 9]
y2 = x.flatten(order='F') # 按列展开
print(y2) # [1 4 7 2 5 8 3 6 9]
# 改变副本数据,原数组不变
y1[3] = 0
print(x)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
numpy.ravel()
- 该函数可把数组转换成一维数组,即可返回视图,也可返回副本。
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 生成视图
y = np.ravel(x)
print(y) # [1 2 3 4 5 6 7 8 9]
# 改变视图数据,原数组对应位置改变
y[3] = 0
print(x)
# [[1 2 3]
# [0 5 6]
# [7 8 9]]
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 生成副本
y2 = np.ravel(x, order='F')
print(y2) # [1 4 7 2 5 8 3 6 9]
# 改变副本数据,原数组不变
y2[3] = 0
print(x)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
numpy.reshape(a, newshape[ ])
- 该函数可把数组转换成任意形状。
x = np.arange(12)
y = np.reshape(x, [3 ,4])
print(y)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# `newshape = [rows,-1]`时,将根据行数自动确定列数
y2 = np.reshape(x, [3, -1])
print(y2)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# `newshape = [-1, columns]`时,将根据列数自动确定行数
y3 = np.reshape(x, [-1, 3])
print(y3)
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]
# 改变x去reshape后y中的值,x对应元素也改变
y[0,1] = 10
print(x) # [ 0 10 2 3 4 5 6 7 8 9 10 11]
# 参数`newshape = -1`时,表示将数组降为一维
x2 = np.random.randint(12, size=[2, 2, 3])
print(x2)
# [[[11 9 1]
# [ 1 10 3]]
#
# [[ 0 6 1]
# [ 4 11 3]]]
y4 = np.reshape(x2, -1)
print(y4)
# [11 9 1 1 10 3 0 6 1 4 11 3]
二、数组转置
numpy.transpose(x)
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.transpose(x)
print(y)
# [[1 4]
# [2 5]
# [3 6]]
numpy.ndarray.T
x = np.array([[1, 2, 3], [4, 5, 6]])
y = x.T
print(y)
# [[1 4]
# [2 5]
# [3 6]]
三、更改维度
numpy.newaxis
- 很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用
newaxis
参数来增加一个维度。
x = np.array([1, 2, 3, 4, 5])
print(x.shape) # (5,)
print(x) # [1 2 3 4 5]
y = x[np.newaxis, :]
print(y.shape) # (1, 5)
print(y) # [[1 2 9 4 5]]
y = x[:, np.newaxis]
print(y.shape) # (5, 1)
print(y)
# [[1]
# [2]
# [3]
# [4]
# [5]]
numpy.squeeze(a, axis=None)
- 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
axis
用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错。
x = np.arange(10)
print(x.shape) # (10,)
# 增加维度
x = x[np.newaxis, :]
print(x.shape) # (1, 10)
# 删除维度
y = np.squeeze(x)
print(y.shape) # (10,)
x2 = np.array([[[0], [1], [2]]])
print(x2.shape) # (1, 3, 1)
y2 = np.squeeze(x2)
print(y2.shape) #(3,)
print(y2) # [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
四、数组组合
numpy.concatenate()
- 一维拼接
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]
- 二维拼接
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
- 复杂类型
x = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
y = np.array([[10, 11, 12],[13, 14, 15], [16, 17, 18]])
z = np.concatenate([x, y])
print(z)
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]
# [13 14 15]
# [16 17 18]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 10 11 12]
# [ 4 5 6 13 14 15]
# [ 7 8 9 16 17 18]]
五、数组拆分
numpy.vsplit()
- 垂直切分,把数组按照高度切分
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24],
[11, 12, 13, 14]])
y = np.vsplit(x, 4)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]]), array([[11, 12, 13, 14]])]
y2 = np.vsplit(x, 2)
print(y2)
# [array([[11, 12, 13, 14], [16, 17, 18, 19]]), array([[21, 22, 23, 24], [11, 12, 13, 14]])]
numpy.hsplit()
- 水平切分是把数组按照宽度切分
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24],
[11, 12, 13, 14]])
y = np.hsplit(x, 4)
print(y)
# [array([[11],
# [16],
# [21],
# [11]]), array([[12],
# [17],
# [22],
# [12]]), array([[13],
# [18],
# [23],
# [13]]), array([[14],
# [19],
# [24],
# [14]])]
y2 = np.hsplit(x, 2)
print(y2)
# [array([[11, 12],
# [16, 17],
# [21, 22],
# [11, 12]]), array([[13, 14],
# [18, 19],
# [23, 24],
# [13, 14]])]
六、数组平铺
numpy.tile(A, reps)
- 将原矩阵横向、纵向地复制
x = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
# [3 4]]
# 横向展开
y = np.tile(x, (1, 2))
print(y)
# [[1 2 1 2]
# [3 4 3 4]]
# 纵向展开
y = np.tile(x, (2, 1))
print(y)
# [[1 2]
# [3 4]
# [1 2]
# [3 4]]
# 双向展开
y = np.tile(x, (2, 2))
print(y)
# [[1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]]
numpy.repeat()
- 将原矩阵每行、每列重复复制
a = np.repeat(3, 4)
print(a) # [3 3 3 3]
x = np.array([[1, 2], [3, 4],[5,6]])
print(x)
# [[1 2]
# [3 4]
# [5 6]]
# 把原数组降为一维数组,然后复制
y = np.repeat(x, 2)
print(y)
# [1 1 2 2 3 3 4 4 5 5 6 6]
# 按列展开,并复制
y = np.repeat(x, 2, axis=0)
print(y)
# [[1 2]
# [1 2]
# [3 4]
# [3 4]
# [5 6]
# [5 6]]
# 按列展开,并指定复制次数
y = np.repeat(x, [1,2, 2], axis=0)
print(y)
# [[1 2]
# [3 4]
# [3 4]
# [3 4]
# [5 6]
# [5 6]]
# 按行展开,并指定复制次数
y = np.repeat(x, [2, 3], axis=1)
print(y)
# [[1 1 2 2 2]
# [3 3 4 4 4]
# [5 5 6 6 6]]
七、删除重复元素
numpy.unique()
- 对于数组或者列表,unique函数去除其中重复的元素,并按元素由大到小的顺序返回一个新的无元素重复的元组或者列表.
A = [3, 2, 3, 2, 1, 2, 2, 5, 4, 3]
a = np.unique(A)
B= (1, 1, 2, 5, 3, 4, 3)
b= np.unique(B)
C= ['fgfh','asd','fgfh','asdfds','wrh']
c= np.unique(C)
D = np.array([[3, 2, 3, 2],[1,3,1,3]])
d = np.unique(D)
print(a) # [1 2 3 4 5]
print(b) # [1 2 3 4 5]
print(c) # ['asd' 'asdfds' 'fgfh' 'wrh']
print(d) # [1 2 3]