如何连接两个二维 NumPy 数组?
有时,连接或合并这些 NumPy 数组中的两个或多个可能很有用或需要。在本文中,我们将讨论连接两个二维数组的各种方法。但首先,我们必须导入 NumPy 包才能使用它:
# import numpy package
import numpy as np
然后必须通过使用arrange()和reshape()函数来创建两个二维数组来执行这些操作。使用 NumPy,我们可以以各种方式和方法执行多个 2D 数组的连接。
方法一:使用concatenate()函数
我们可以使用concatenate ()函数执行连接操作。使用此函数,数组可以按行或按列连接,前提是它们分别具有相等的行或列。可以通过将轴等于 1 作为函数中的参数来完成逐列连接。
例子:
Python
# 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)
Python
# 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)
Python
# 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)
Python3
# 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)
Python
# 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))
Python
# 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))
Python
# 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))
Python3
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)
输出:
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 来完成逐行连接。
例子:
Python
# 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。通过使用它,这些阵列彼此堆叠。
例子:
Python
# 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]]])
或者通过将axis等于2,连接与高度一起完成,如下所示。
例子:
Python3
# 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()函数水平堆叠数组,即沿一列。
例子:
Python
# 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]])
方法四:使用vstack()函数
vstack()函数垂直堆叠数组,即沿一行。
例子:
Python
# 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 代表深度,连接与高度一起出现,如下所示:
例子:
Python
# 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 数组将它们连接成二维数组。
Python3
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]]