numpy.concatenate((a1, a2, ...), axis=0)
numpy.hstack(tup) #水平拼接
numpy.vstack(tup) #竖直拼接
示例1:
import numpy as np
from numpy import random
t1 = np.arange(8).reshape(2,4)
t2 = random.randint(1,8,size=(2,4))
print(t1)
print('#'*15)
print(t2)
[[0 1 2 3]
[4 5 6 7]]
###############
[[1 7 6 7]
[2 5 7 7]]
#竖直拼接
np.vstack((t1,t2))
array([[0, 1, 2, 3],
[4, 5, 6, 7],
[1, 7, 6, 7],
[2, 5, 7, 7]])
#水平拼接
np.hstack((t1,t2))
array([[0, 1, 2, 3, 1, 7, 6, 7],
[4, 5, 6, 7, 2, 5, 7, 7]])
示例2:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
numpy.split(ary, indices_or_sections, axis=0)
ary:要切分的数组
indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
axis:沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分
import numpy as np
x = np.arange(8)
print(np.split(x, 3))
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7., 8.])]
print(np.split(x, [3, 5, 6, 10]))
[array([ 0., 1., 2.]),
array([ 3., 4.]),
array([ 5.]),
array([ 6., 7.]),
array([], dtype=float64)]