如果要将两份数据组合到一起,就需要拼接操作。
numpy.concatenate((a1, a2, ...), axis=0, out=None)
Join a sequence of arrays along an existing axis.
【例】连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)。
import numpy as np
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]
【例】原来x,y都是二维的,拼接后的结果也是二维的。
import numpy as np
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]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 7 8 9]]
【例】x,y在原来的维度上进行拼接。
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
numpy.stack(arrays, axis=0, out=None)
Join a sequence of arrays along a new axis.
【例】沿着新的轴加入一系列数组(stack为增加维度的拼接)。
import numpy as np
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
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]]
【例】
import numpy as np
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.stack([x, y])
print(z.shape) # (2, 1, 3)
print(z)
# [[[1 2 3]]
#
# [[7 8 9]]]
z = np.stack([x, y], axis=1)
print(z.shape) # (1, 2, 3)
print(z)
# [[[1 2 3]
# [7 8 9]]]
z = np.stack([x, y], axis=2)
print(z.shape) # (1, 3, 2)
print(z)
# [[[1 7]
# [2 8]
# [3 9]]]
【例】
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.stack([x, y])
print(z.shape) # (2, 2, 3)
print(z)
# [[[ 1 2 3]
# [ 4 5 6]]
#
# [[ 7 8 9]
# [10 11 12]]]
z = np.stack([x, y], axis=1)
print(z.shape) # (2, 2, 3)
print(z)
# [[[ 1 2 3]
# [ 7 8 9]]
#
# [[ 4 5 6]
# [10 11 12]]]
z = np.stack([x, y], axis=2)
print(z.shape) # (2, 3, 2)
print(z)
# [[[ 1 7]
# [ 2 8]
# [ 3 9]]
#
# [[ 4 10]
# [ 5 11]
# [ 6 12]]]
numpy.vstack(tup)
Stack arrays in sequence vertically (row wise).numpy.hstack(tup)
Stack arrays in sequence horizontally (column wise).
【例】一维的情况。
import numpy as np
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.vstack((x, y))
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.hstack((x, y))
print(z.shape) # (6,)
print(z)
# [1 2 3 7 8 9]
z = np.concatenate((x, y))
print(z.shape) # (6,)
print(z) # [1 2 3 7 8 9]
【例】二维的情况。
import numpy as np
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.vstack((x, y))
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.concatenate((x, y), axis=0)
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.hstack((x, y))
print(z.shape) # (1, 6)
print(z)
# [[ 1 2 3 7 8 9]]
z = np.concatenate((x, y), axis=1)
print(z.shape) # (1, 6)
print(z)
# [[1 2 3 7 8 9]]
【例】二维的情况。
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.vstack((x, y))
print(z.shape) # (4, 3)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate((x, y), axis=0)
print(z.shape) # (4, 3)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.hstack((x, y))
print(z.shape) # (2, 6)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
z = np.concatenate((x, y), axis=1)
print(z.shape) # (2, 6)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
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)
# 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)