0

I am working with a deep learning model that is trying to concatenate a label with dimensions (1,2) with a numpy array of (25,25). I'm not really sure if it is possible to get a dimension of (627,0), however, the model summary says that is the input shape it expects.

I've tried to concatenate them, but I get the error " all the input array dimensions except for the concatenation axis must match exactly" as expected.

    x = np.concatenate((X[1], to_categorical(Y_train[1]))   

Where X = (25,25) and Y_train is ( 1,0), making to_categorical(Y_train[1]) equal to (2,1).

Is there a way to get this (627, 0) dimension with these dimensions?

kdf
  • 358
  • 4
  • 16
  • You can reshape `X` and make it a 1D array. – DYZ Apr 21 '19 at 05:11
  • 1
    An array of shape `(627, 0)` would have no elements in it. I don't see why you would want to concatenate the two arrays you have, or what you expect such an operation to mean. Without more context, it's impossible to tell what you should be doing. – user2357112 Apr 21 '19 at 05:51
  • 1
    Why would `Y_train` have shape `(1, 0)`? That's an array with no data (and `Y_train[1]` is out of bounds for that shape). Something has already gone wrong before even the part you're asking us about. – user2357112 Apr 21 '19 at 05:53
  • 1
    Possible duplicate of [Concat two arrays of different dimensions numpy](https://stackoverflow.com/questions/46700081/concat-two-arrays-of-different-dimensions-numpy) – Alec Apr 21 '19 at 06:17

1 Answers1

0

@Psidom has a great answer to this:

Let's say you have a 1-d and a 2-d array

You can use numpy.column_stack:

np.column_stack((array_1, array_2))

Which converts the 1-d array to 2-d implicitly, and thus equivalent to np.concatenate((array_1, array_2[:,None]), axis=1).


a = np.arange(6).reshape(2,3)
b = np.arange(2)

a
#array([[0, 1, 2],
#       [3, 4, 5]])

b
#array([0, 1])

np.column_stack((a, b))
#array([[0, 1, 2, 0],
#       [3, 4, 5, 1]])
Alec
  • 8,529
  • 8
  • 37
  • 63