numpy.concaternte
- Join a sequence of arrays along an existing axis.
- [https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html#numpy.concatenate]
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
import numpy as np
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]])
np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
numpy.hstack 水平
- Stack arrays in sequence horizontally (column wise).
- numpy.hstack(tup)
a = np.array((1,2,3))
b = np.array((4,5,6))
np.hstack((a,b))
array([1, 2, 3, 4, 5, 6])
a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
np.hstack((a,b))
array([[1, 4],
[2, 5],
[3, 6]])
numpy.vstack 垂直
Stack arrays in sequence vertically (row wise)
a = np.array((1,2,3))
b = np.array((4,5,6))
np.vstack((a,b))
array([[1, 2, 3],
[4, 5, 6]])
a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
np.vstack((a,b))
array([[1],
[2],
[3],
[4],
[5],
[6]])
numpy.dstack(tup) 沿第三轴
Stack arrays in sequence depth wise (along third axis).
a = np.array((1,2,3))
b = np.array((2,3,4))
np.dstack((a,b))
array([[[1, 2],
[2, 3],
[3, 4]]])
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.dstack((a,b))
array([[[1, 2]],
[[2, 3]],
[[3, 4]]])
numpy.stack(arrays, axis=0, out=None) 沿新轴
Join a sequence of arrays along a new axis.
arrays = [np.random.randn(3, 4) for _ in range(10)]
np.stack(arrays, axis=0).shape
(10, 3, 4)
np.stack(arrays, axis=1).shape
(3, 10, 4)
np.stack(arrays, axis=2).shape
(3, 4, 10)
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.stack((a, b))
array([[1, 2, 3],
[4, 5, 6]])
np.stack((a, b), axis=-1)
array([[1, 4],
[2, 5],
[3, 6]])
numpy.block :拼接
Assemble an nd-array from nested lists of blocks.
A = np.eye(2) * 2
B = np.eye(3) * 3
np.block([
[A,np.zeros((2, 3))],
[np.ones((3, 2)), B]
])
array([[2., 0., 0., 0., 0.],
[0., 2., 0., 0., 0.],
[1., 1., 3., 0., 0.],
[1., 1., 0., 3., 0.],
[1., 1., 0., 0., 3.]])
np.block([1, 2, 3]) # hstack([1, 2, 3])
array([1, 2, 3])
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.block([a, b, 10]) # hstack([a, b, 10])
array([ 1, 2, 3, 4, 5, 6, 10])
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.block([[a], [b]]) # vstack([a, b])
array([[1, 2, 3],
[4, 5, 6]])
A = np.ones((2, 2), int)
B = 2 * A
np.block([[A], [B]]) # vstack([A, B])
array([[1, 1],
[1, 1],
[2, 2],
[2, 2]])
a = np.array(0)
b = np.array([1])
np.block([a]) # atleast_1d(a)
array([0])
np.block([b]) # atleast_1d(b)
array([1])
np.block([[a]]) # atleast_2d(a)
array([[0]])
np.block([[b]]) # atleast_2d(b)
array([[1]])
numpy.column_stack(tup) 作为列合并
Stack 1-D arrays as columns into a 2-D array.
a = np.array((1,2,3))
b = np.array((2,3,4))
np.column_stack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])
numpy.append(arr, values, axis=None)
Append values to the end of an array
np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 指定轴时,值必须具有正确的形状。
np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], axis=1)
array([[ 1, 2, 3, 7, 8, 9],
[ 4, 5, 6, 10, 11, 12]])