如何将两个二维NumPy数组串联起来
有时,将两个或多个NumPy数组连接或合并可能是有用的或需要的。在这篇文章中,我们将讨论连接两个二维数组的各种方法。但首先,我们必须导入NumPy包来使用它。
# import numpy package
import numpy as np
然后必须创建两个二维数组来执行操作,通过使用arrange()和reshape()函数。使用NumPy,我们可以通过各种方式和方法对多个二维数组进行串联。
方法1:使用concatenate()函数
我们可以使用concatenate()函数来进行连接操作。通过这个函数,数组可以按行或按列进行连接,前提是它们的行或列都相等。列间连接可以通过将轴等效为1作为函数的参数来完成。
示例:
# Program to concatenate two 2D arrays column-wise
# import numpy
import numpy as np
# Creating two 2D arrays
arr1 = np.arange(1,10).reshape(3,3)
arr2 = np.arange(10,19).reshape(3,3)
arr1
arr2
# Concatenating operation
# axis = 1 implies that it is being done column-wise
np.concatenate((arr1,arr2),axis=1)
输出:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
array([[ 0, 1, 2, 10, 11, 12],
[ 3, 4, 5, 13, 14, 15],
[ 6, 7, 8, 16, 17, 18]])
以同样的方式,行间连接可以通过将轴等效为0来完成。
示例:
# Program to concatenate two 2D arrays row-wise
import numpy as np
# Creating two 2D arrays
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
# axis = 0 implies that it is being done row-wise
np.concatenate((arr1, arr2), axis=0)
输出:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
方法2:使用 stack() 函数
stack()函数的使用方法与concatenate()函数相同,其中轴被等同于1。通过使用这个,数组被一个一个地堆叠起来。
示例:
# Program to concatenate two 2D arrays row-wise
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
# axis = 1 implies that it is being
# done row-wise
np.stack((arr1, arr2), axis=1)
输出:
array([[[ 1, 2, 3],
[10, 11, 12]],
[[ 4, 5, 6],
[13, 14, 15]],
[[ 7, 8, 9],
[16, 17, 18]]])
或者通过将轴等效为2,与高度一起进行连接,如下图所示。
示例:
# Program to concatenate two 2D arrays along
# the height
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
# axis = 2 implies that it is being done
# along the height
np.stack((arr1, arr2), axis=2)
输出:
array([[[ 1, 10],
[ 2, 11],
[ 3, 12]],
[[ 4, 13],
[ 5, 14],
[ 6, 15]],
[[ 7, 16],
[ 8, 17],
[ 9, 18]]])
方法3:使用 hstack() 函数
hstack()函数在水平方向上堆叠数组,即沿着一列堆叠。
示例:
# Program to concatenate two 2D arrays
# horizontally
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
arr = np.hstack((arr1, arr2))
输出:
array([[ 0, 1, 2, 10, 11, 12],
[ 3, 4, 5, 13, 14, 15],
[ 6, 7, 8, 16, 17, 18]])
方法4:使用 vstack() 函数
vstack()函数垂直堆叠数组,即沿着一行堆叠。
示例:
# Program to concatenate two 2D arrays
# vertically
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
arr = np.vstack((arr1, arr2))
输出:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
方法5:使用 dstack() 函数
在dstack( )函数中,d代表深度,连接与高度一起发生,如下所示。
示例:
# Program to concatenate two 2D arrays
# along the height
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
# Concatenating operation
arr = np.dstack((arr1, arr2))
输出:
array([[[ 1, 10],
[ 2, 11],
[ 3, 12]],
[[ 4, 13],
[ 5, 14],
[ 6, 15]],
[[ 7, 16],
[ 8, 17],
[ 9, 18]]])
方法6:使用column_stack()函数
column_stack()函数沿水平方向堆叠数组,即沿着一列堆叠,它通常用于通过水平方向连接id数组,将其串联成2d数组。
import numpy
array1 = numpy.array([[1, 2, 3, 4, 5],[20,30,40,50,60]])
array2 = numpy.array([[6, 7, 8, 9, 10],[9,8,7,6,5]])
# Stack arrays horizontally.
array1 = numpy.column_stack([array1, array2])
print(array1)
输出:
[[ 1 2 3 4 5 6 7 8 9 10]
[20 30 40 50 60 9 8 7 6 5]]