-1

I have one array with dimension (1538,4) called X_scaled and another array with dimensions (1538,1) called Y_mlp. I want to add Y_mlp to X_scaled such that Y_mlp becomes the fifth column in X_scaled. How can I do this?

cs95
  • 379,657
  • 97
  • 704
  • 746
Alex_ban
  • 221
  • 2
  • 11
  • Possible duplicate of [How to add an extra column to an numpy array](https://stackoverflow.com/questions/8486294/how-to-add-an-extra-column-to-an-numpy-array) – taras Aug 25 '17 at 15:31

1 Answers1

1

You're looking for np.hstack.

numpy.hstack(tup) 

Take a sequence of arrays and stack them horizontally to make a single array.

import numpy as np

X_scaled, Y_mlp = ..., ...

Y_mlp = Y_mlp.reshape(-1, 1)   
out = np.hstack((X_scaled, Y_mlp))

print(out.shape)

Output:

(5, 5)

Concatenation occurs along the second dimension.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • It says dimensions don't match when I concatenate. When I enter Y_mlp.shape it prints(1538,) – Alex_ban Aug 25 '17 at 15:25
  • @Alex_ban That means that your arrays don't have that shape... You need to do `Y_mlp = Y_mlp.reshape(-1, 1)` – cs95 Aug 25 '17 at 15:26
  • What does (1538,) means? There is no 1 after the comma as it should be. – Alex_ban Aug 25 '17 at 15:27
  • 1
    @Alex_ban Please understand. You need 2 arrays with 2 dimensions each for concatenation to work. `(xxx, )` means that it is a 1D array. `(xxx, 1)` means it is a 2D array with xxx rows and 1 column. Look at my edit. Follow those steps, and if it works, then all's well. – cs95 Aug 25 '17 at 15:29
  • With 2d arrays, concatenation occurs on the 2nd dimension (axis=1). `hstack` doesn't add much to `concatenate(..., axis=1)`. – hpaulj Aug 25 '17 at 17:03
  • @hpaulj Ah, sorry. My terminology was off. – cs95 Aug 25 '17 at 17:11