【问题标题】:Concatenate Multidimensional Numpy array with 1D numpy array将多维 Numpy 数组与一维 numpy 数组连接起来
【发布时间】:2020-06-16 08:32:53
【问题描述】:

我有两个 numpy 数组(来自 EMNIST 数据集的字母):

import scipy .io
emnist = scipy.io.loadmat(DATA_DIR + '/emnist-letters.mat')
data = emnist ['dataset']
X_train = data ['train'][0, 0]['images'][0, 0]
y_train = data ['train'][0, 0]['labels'][0, 0]

具有以下尺寸:

X_train.shape = (124800, 784)

y_train.shape = (124800, 1)

现在,我想将两者连接起来,这样新的形状将是:(124800, 785)。

基于this链接,我试过了:

np.concatenate((X_train.shape, y_train.shape), axis = 0)

但是,这会导致以下形状:array([124800, 784, 124800, 1])。

如何在X_train 后面“粘贴”y_train 以使形状变为 (124800, 785)?

【问题讨论】:

    标签: python numpy concatenation


    【解决方案1】:

    如果连接两个数组,则必须连接数组内的数据,而不是形状。此外,您想在第二个(“短”)轴上连接,即axis=1

    np.concatenate((X_train, y_train), axis=1)
    

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2022-07-12
      • 2012-03-03
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多