函数 | 含义 |
---|---|
np.vstack(tup)、np.hstack(tup) | 将tuple内的所有数组按照vertical、horizontal 方向拼接 |
np.concatenate(a_tuple, axis=0) | 将a_tuple内的数组按照某个方向拼接 |
np.split(ary,indices_or_sections,axis) | 对数组按照某方向均匀分割成indices_or_sections段 |
np.array_split(ary, indices_or_sections, axis) | 实现不均匀分割 |
np.vstack((a, b)):
Stack arrays in sequence vertically
np.hstack((a, b)):
Stack arrays in sequence horizontally
import numpy as np
a = np.array([1, 2, 3])
b = np.array([7, 8, 9])
c = np.vstack((a, b)) # 在垂直方向上拼接
print(c)
print(np.hstack((a, b))) # 在水平方向上拼接
import numpy as np
a = np.array([1, 2, 3])[np.newaxis,:]
b = np.array([7, 8, 9])[np.newaxis,:]
c = np.concatenate((a, b), axis = 0)
print(c)
import numpy as np
a = np.arange(2,14).reshape(3,4)
print(a)
print(np.split(a, 3, axis= 0))
indices_or_sections:是要把ary均匀的切割成多少段,不能均匀切割的话会报错
例如报错:三行无法均匀分割成两行
import numpy as np
a = np.arange(2,14).reshape(3,4)
print(a)
print(np.split(a, 2, axis= 0)) # ValueError: array split does not result in an equal division 三行无法均匀分割成两行
import numpy as np
a = np.arange(2, 14).reshape(3, 7)
print(a)
print(np.array_split(a, 3, axis=1))
print(np.array_split(a, 2, axis=0))
可以看出不均匀分割的原则: