0

I am trying to append column for the following arrays

train = np.append(train_data, train_labels, axis=1)

(60000, 784)
(60000,)

And I get the error

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

I cannot understand what the issue is... I need output of

(60000, 785)
2
  • What are your train_data and train_labels? Commented Jun 1, 2019 at 23:30
  • When given an axis, np.append` is just np.concatenate((arr, values), axis=axis). When using concatenate the number of dimensions of the inputs has to match (in this case all must be 2d). Commented Jun 1, 2019 at 23:47

2 Answers 2

1

I think you should use axis=0.

>>> np.append((1,2),(2,),axis=0)
array([1, 2, 2])

If those tuples that you have posted are array shapes, then you can use

train = np.append(train_data, train_labels[:,None], axis=1)

The arrays must have the same number of dimensions (2 in this case). Using None in indexing for non-existing dimension adds a singleton dimension to a an array.

>>> train_labels[:,None].shape
(60000, 1)
Sign up to request clarification or add additional context in comments.

2 Comments

with axis 0 I get the same error and I want to add column, axis 0 is for rows
@YohanRoth You are welcome. I added a bit of explanation (not very accurate, but you can check numpy docs on broadcasting for an accurate (but confusing) explanation.
1

You should run

train = np.append(train_data, train_labels.reshape(-1, 1), axis=1)

That will fix your problem. Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.