Solving Error After numpy array concatenate – ValueError: all the input arrays must have same number of dimensions

ValueError: all the input arrays must have same number of dimensions is an error which occurs when you try to concatenate numpy arrays the wrong way.

In this article I am going to explain why the error is happening and how to not get the error again. Also we are going to check out other solutions that may work for your particular case.

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

This is an error which occurs when you try to concatenate numpy arrays the wrong way.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

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

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : use .shape and .reshape

The easiest way to concatenate two arrays is to use .shape and .reshape

You can do that like in the line of code bellow

                                                                       #
val = np.concatenate((x,np.reshape(y,(x.shape[0],1))),axis=1)
                                                                       #

val is the resulting array which you can print using

                                                                       #
print(val)
                                                                       #

This solution should have solved the issue and the error should be removed. Please consider trying the solution bellow, If that is not the case.

Solution 2 : use np.column_stack or np.concatenate

There are two options to achieve your goal, the first one is to do some manual operations and the second one is automated.

The non automated solution is using np.concatenate.

The trick is to concatenate along the first axis, axis=1 after extending the second array to 2D, The command bellow is how you achieve that.

                                                                       #
np.concatenate((a,b[:,None]),axis=1)
                                                                       #

The automated option is to simply use np.column_stack like in the command bellow.

                                                                       #
np.column_stack((a,b))
                                                                       #

This solution should have solved the issue and the error should be removed.

I hope this guide solved your problem, thank you for reading.

Summing-up

This is the end of our article, this error could be confusing but with a little bit of investigating the error could be solved easily, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/