Search code examples
pythonnumpyconcatenation

Concatenate Multidimensional Numpy array with 1D numpy array


I have two numpy arrays (letters from the EMNIST dataset):

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]

With the following dimensions:

X_train.shape = (124800, 784)

y_train.shape = (124800, 1)

Now, I want to concatenate the two, so that the new shape would be: (124800, 785).

Based on this link, I tried:

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

However, that results in the following shape: array([124800, 784, 124800, 1]).

How can I 'paste' y_train behind X_train so that the shape will be (124800, 785)?


Solution

  • If you concatenate two arrays, you have to concatenate the data inside the arrays, not the shape. Furthermore you want to concatenate on the second ("short") axis, which is axis=1:

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