你如何在 numpy 中连接几个二维数组?

How do you concatenate several 2D arrays in numpy?

我愿意

np.concatenate((np.array([[5,5],[2,3]]),np.array([[6,4],[7,8]])))

屈服

[ [[5,5],[2,3]], [[6,4],[7,8]] ]

连接并不能解决问题,但我不知道该怎么做!

任何想知道 np.stack((a,b)) 是否有效的人:)

你可以使用numpy.stack()numpy.append()(如果你有大代码我建议append)。注意是numpy的append。不是 python 的内置 append

>>> import numpy as np
>>> a = np.array([[5,5],[2,3]])
>>> b = np.array([[6,4],[7,8]])
>>> np.append([a], [b], axis = 0)
# answer: 
array([[[5, 5],
        [2, 3]],

       [[6, 4],
        [7, 8]]])

现在如果我们选择 np.stack():

>>> d = np.stack((a,b))
>>> c == d
# answer:
array([[[ True,  True],
        [ True,  True]],

       [[ True,  True],
        [ True,  True]]])

如您所见,它们是相同的。
你可以看到 user guide of numpy.append here and user guide of numpy.vstack here.