How to Solve Python ValueError: all the input arrays must have the same number of dimensions

by | Programming, Python, Tips

If you want to concatenate NumPy arrays using the numpy.concatenate method, the array dimensions must match. If the arrays have incompatible dimensions, you will encounter the ValueError: all the input arrays must have the same number of dimensions.

There are several ways to solve this error:

  • Extend the dimensions of the arrays using numpy.reshape()
  • Concatenate arrays using numpy.column_stack()
  • Concatentate arrays using numpy.c_

This tutorial will go through the error in detail and how to solve it with code examples.


ValueError: all the input array dimensions

In Python, a value is a piece of information stored within a particular object. We will encounter a ValueError in Python when using a built-in operation or function that receives an argument that is the right type but an inappropriate value. The data we want to concatenate is the correct type, NumPy ndarray, but the arrays have the wrong dimensions for concatenation. We can get the dimensions of an array using the shape method, which returns a tuple containing the length of the array dimensions. The number of dimensions of a NumPy array is the length of the tuple returned by the shape method.

import numpy as np

arr = np.array([[2,3], [1, 4]])

print(f'The shape of the array is: {arr.shape}')

print(f'Number of dimensions is: {len(arr.shape)}')
The shape of the array is: (2, 2)
Number of dimensions is: 2

The above array is two dimensional, with size 2 in the first dimension and size 2 in the second dimension.

Example

Let’s look at an example of a two-dimensional array and a one-dimensional array that we want to concatenate.

import numpy as np

arr1 = np.array([[  6487,    400, 489580,      0],
[  6488,    401, 492994,      0],
[  6491,    408, 489247,      0],
[  6491,    408, 489247,      0],
[  6492,    402, 499013,      0]])

arr2 = np.array([3, 17, 12, 12, 15])

print(f'Number of dimensions of arr1 is {len(arr1.shape)}')
print(f'Number of dimensions of arr2 is {len(arr2.shape)}')

Let’s run the code to confirm the dimensions of the two arrays:

Number of dimensions of arr1 is 2
Number of dimensions of arr2 is 1

Next, we will try to concatenate the two arrays using the numpy.concatenate method:

arr3 = np.concatenate([arr1, arr2])

print(arr3)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [46], in <cell line: 14>()
     11 print(f'Number of dimensions of arr1 is {len(arr1.shape)}')
     12 print(f'Number of dimensions of arr2 is {len(arr2.shape)}')
---> 14 arr3 = np.concatenate([arr1, arr2])
     16 print(arr3)

File <__array_function__ internals>:5, in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

We raise the ValueError because the first array has two dimensions and the second array has one dimension.

Solution #1: Extend Array to Two Dimensions

The first way to solve this error is to extend the second array to two dimensions using numpy.reshape.

import numpy as np
arr1 = np.array([[  6487,    400, 489580,      0],
[  6488,    401, 492994,      0],
[  6491,    408, 489247,      0],
[  6491,    408, 489247,      0],
[  6492,    402, 499013,      0]])

arr2 = np.array([3, 17, 12, 12, 15])
arr2 = np.reshape(arr2, (arr1.shape[0],1))
print(arr2)

print(f'Number of dimensions of arr1 is {len(arr1.shape)}')
print(f'Number of dimensions of arr2 is {len(arr2.shape)}')

arr3 = np.concatenate((arr1, arr2), axis=1)

print(arr3)

In the above code, we reshaped the second array to have size 5 in the first dimension and size 1 in the second dimension. Note that we concatenate the arrays along axis 1. Let’s run the code to see the result:

[[ 3]
 [17]
 [12]
 [12]
 [15]]
Number of dimensions of arr1 is 2
Number of dimensions of arr2 is 2
[[  6487    400 489580      0      3]
 [  6488    401 492994      0     17]
 [  6491    408 489247      0     12]
 [  6491    408 489247      0     12]
 [  6492    402 499013      0     15]]

Solution #2: Use numpy.column_stack

We can use column_stack to concatenate the two arrays. In this case, we do not need to reshape the second array. To stack a sequence of arrays, they must have the same first dimension. If they do not have the same first dimension, we will get the error: ValueError: all the input array dimensions for the concatenation axis must match exactly. For example:

ValueError: all the input array dimensions for the concatenation axis must match exactly

import numpy as np

arr1 = np.array([[  6487,    400, 489580,      0],
[  6488,    401, 492994,      0],
[  6491,    408, 489247,      0],
[  6491,    408, 489247,      0],
[  6492,    402, 499013,      0]])

arr2 = np.array([3, 17, 12, 12])

arr3 = np.column_stack((arr1, arr2))

print(arr3)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 5 and the array at index 1 has size 4

We must ensure that all the arrays we want to stack have the same size in the first dimension. Let’s look at the revised code:

import numpy as np

arr1 = np.array([[  6487,    400, 489580,      0],
[  6488,    401, 492994,      0],
[  6491,    408, 489247,      0],
[  6491,    408, 489247,      0],
[  6492,    402, 499013,      0]])

arr2 = np.array([3, 17, 12, 12, 15])

arr3 = np.column_stack((arr1, arr2))

print(arr3)

Let’s run the code to stack the arrays:

[[  6487    400 489580      0      3]
 [  6488    401 492994      0     17]
 [  6491    408 489247      0     12]
 [  6491    408 489247      0     12]
 [  6492    402 499013      0     15]]

Solution #3: Use numpy.c_

We can solve the ValueError by using the numpy.c_ method, which works the same way as the numpy.column_stack() method. The arrays must have the same first dimension; otherwise, NumPy will throw the ValueError: all the input array dimensions for the concatenation axis must match exactly. Let’s look at the revised code:

import numpy as np

arr1 = np.array([[  6487,    400, 489580,      0],
[  6488,    401, 492994,      0],
[  6491,    408, 489247,      0],
[  6491,    408, 489247,      0],
[  6492,    402, 499013,      0]])

arr2 = [3, 17, 12, 12, 15]

arr3 = np.c_[arr1, arr2]

print(arr3)

Let’s run the code to see the result:

[[  6487    400 489580      0      3]
 [  6488    401 492994      0     17]
 [  6491    408 489247      0     12]
 [  6491    408 489247      0     12]
 [  6492    402 499013      0     15]]

Summary

Congratulations on reading to the end of this tutorial! The ValueError: all the input arrays must have the same number of dimensions occurs when you try to concatenate arrays that have different numbers of dimensions.

The ValueError: all the input array dimensions for the concatenation axis must match exactly occurs when you try to perform concatenation, but the size of the arrays along the concatenation axis are mismatching.

For the first ValueError, you can expand the array dimensions, use numpy.column_stack, or numpy.c_.

To solve the second ValueError, ensure when you are using concatenate, column_stack or c_ that the arrays have the same size along the concatenate axis.

For further reading on NumPy, go to the article: How to Solve Python TypeError: unhashable type: ‘numpy.ndarray’.

Go to the Python online courses page to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨