如何附加两个 NumPy 数组?
先决条件: Numpy
Python中的两个数组可以以多种方式附加,下面讨论所有可能的数组。
方法一:使用append()方法
此方法用于将值附加到数组的末尾。
Syntax :
numpy.append(array, values, axis = None)
Parameters :
- array: [array_like]Input array.
- values : [array_like]values to be added in the arr. Values should be
shaped so that arr[…,obj,…] = values. If the axis is defined values can be of any
shape as it will be flattened before use.
- axis : Axis along which we want to insert the values. By default, array is flattened.
Return :
A copy of array with values being appended at the end as per the mentioned object, along a given axis.
例子:
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Appending both arrays using Append method
array1 = numpy.append(array1, array2)
print(array1)
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Appending both Arrays using concatenate() method.
array1 = numpy.concatenate([array1, array2])
print(array1)
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Join a sequence of arrays along a new axis.
array1 = numpy.stack([array1, array2])
print(array1)
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack arrays in sequence horizontally (column wise).
array1 = numpy.hstack([array1, array2])
print(array1)
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack arrays in sequence vertically (row wise).
array1 = numpy.vstack([array1, array2])
print(array1)
Python3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack 1-D arrays as columns into a 2-D array.
array1 = numpy.column_stack([array1, array2])
print(array1)
输出:
[ 1 2 3 4 5 6 7 8 9 10]
方法二:使用 concatenate() 方法
Concatenate 方法沿现有轴连接一系列数组。
Syntax :
numpy.concatenate((arr1, arr2, …), axis=0, out=None)
Parameters :
- arr1, arr2, … : [sequence of array_like] The arrays must have the same shape, except in the dimension corresponding to axis.
- axis : [int, optional] The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
- out : [ndarray, optional] If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
Return : [ndarray] The concatenated array.
例子:
蟒蛇3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Appending both Arrays using concatenate() method.
array1 = numpy.concatenate([array1, array2])
print(array1)
输出:
[ 1 2 3 4 5 6 7 8 9 10]
方法 3:使用 stack() 方法
Stack 方法 沿新轴连接一系列数组。
Syntax : numpy.stack(arrays, axis)
Parameters :
- arrays : [array_like] Sequence of arrays of the same shape.
- axis : [int] Axis in the resultant array along which the input arrays are stacked.
Return : [stacked ndarray] The stacked array of the input arrays which has one more dimension than the input arrays.
例子:
蟒蛇3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Join a sequence of arrays along a new axis.
array1 = numpy.stack([array1, array2])
print(array1)
输出:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
方法 4:使用 hstack() 方法
hstack 方法 水平(按列)按顺序堆叠数组。
Syntax : numpy.hstack(tup)
Parameters :
- tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the second axis.
Return : [stacked ndarray] The stacked array of the input arrays.
例子:
蟒蛇3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack arrays in sequence horizontally (column wise).
array1 = numpy.hstack([array1, array2])
print(array1)
输出:
[ 1 2 3 4 5 6 7 8 9 10]
方法 5:使用 vstack() 方法
vstack 方法 垂直(按行)按顺序堆叠数组。
Syntax : numpy.vstack(tup)
Parameters :
- tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the first axis.
Return : [stacked ndarray] The stacked array of the input arrays.
例子:
蟒蛇3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack arrays in sequence vertically (row wise).
array1 = numpy.vstack([array1, array2])
print(array1)
输出:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
方法 6 :使用 column_stack() 方法
column_stack()方法将数组作为列堆叠成二维数组。
Syntax: column_stack( array)
蟒蛇3
import numpy
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
# Stack 1-D arrays as columns into a 2-D array.
array1 = numpy.column_stack([array1, array2])
print(array1)
输出:
[[ 1 6]
[ 2 7]
[ 3 8]
[ 4 9]
[ 5 10]]