1

I have a list of arrays that contain the same rows, but different columns. I printed out the shape of the array and checked that they have same rows.

print ("Type x_test : actual",type(x_dump),x_dump.shape, type(actual), actual.shape, pred.shape)
cmp = np.concatenate([x_test,actual,pred],axis = 1)

('Type x_test : actual', <type 'numpy.ndarray'>, (2420L, 4719L), <type 'numpy.ndarray'>, (2420L,), (2420L,))

This gives me an error:

ValueError: all the input arrays must have same number of dimensions

I tried to replicate this error using the below commands:

x.shape,x1.shape,x2.shape
Out[772]: ((3L, 1L), (3L, 4L), (3L, 1L))

np.concatenate([x,x1,x2],axis=1)
Out[764]: 
array([[ 0,  0,  1,  2,  3,  0],
       [ 1,  4,  5,  6,  7,  1],
       [ 2,  8,  9, 10, 11,  2]])

I dont get any error here. Is anyone facing similar issue ?

EDIT 1: Right after writing this question, I figured out that the dimensions are different. @Gareth Rees: has explained beautifully the different between numpy array (R,1) and (R,) here.

Fixed using:

# Reshape and concatenate
actual = actual.reshape(len(actual),1)
pred = pred.reshape(len(pred),1)

EDIT 2: Marking to close this answer as a duplicate of Difference between numpy.array shape (R, 1) and (R,).

Community
  • 1
  • 1
ForeverLearner
  • 1,437
  • 2
  • 23
  • 40
  • what is the issue? the result seems correct. Could you be more precise on what you're trying to do? – fonfonx May 09 '17 at 12:33
  • @fonfonx my first command is giving an error. The shape is also (2420L,) and not (2420L,1L). Even if i use x_dump.reshape(2420,1) it doesnt work. why would the col value be blank in x_dump.shape – ForeverLearner May 09 '17 at 13:23
  • 1
    "The shape is also (2420L,) and not (2420L,1L)" - this is your problem. You need to reshape the things with shape `(2420L,)` to have shape `(2420L,1)`. – Eric May 09 '17 at 13:43
  • It's not complaining about the wrong size of the dimensions but about the wrong number of dimensions. Maybe make yourself aware of the difference between the dimensions (n,1) and (n,). The first is 2-dimensional, the second 1-dimensional. – obachtos May 09 '17 at 14:08
  • @Eric Thanks. Yes, I noted that right after. Reshape has fixed the issue. – ForeverLearner May 09 '17 at 16:08
  • Possible duplicate of [Difference between numpy.array shape (R, 1) and (R,)](http://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r) – ForeverLearner May 09 '17 at 16:21

1 Answers1

-1

EDIT

After posting this, the OP figured out the error. This can be ignored unless one needs to see the construct with shaped (R,1) versus (R,). In any event, it will give the down voters practice space.

ORIGINAL

Given your shapes, the answer is correct.

a = np.arange(3).reshape(3,1)

b = np.arange(12).reshape(3,4)

c = np.arange(3).reshape(3,1)

np.concatenate([a, b, c], axis=1)
Out[4]: 
array([[ 0,  0,  1,  2,  3,  0],
       [ 1,  4,  5,  6,  7,  1],
       [ 2,  8,  9, 10, 11,  2]])

a
Out[5]: 
array([[0],
       [1],
       [2]])

b
Out[6]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

c
Out[7]: 
array([[0],
       [1],
       [2]])
NaN
  • 1,536
  • 2
  • 19
  • 22