All the input arrays must have same number of dimensions

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. All the input arrays must have same number of dimensions
  2. Use the numpy.column_stack() method to solve the error
  3. Using the numpy.row_stack() method to solve the error
  4. Solve the error by extending the 1-D array to 2-D
  5. Flattening the 2-D array to solve the error
  6. Using the numpy.c_ class to solve the error

# All the input arrays must have same number of dimensions

The NumPy ValueError "all the input arrays must have same number of dimensions, but the array at index 0 has X dimension(s) and the array at index 1 has Y dimension(s)" occurs when the arrays you pass to the numpy.concatenate() method have different shapes.

To solve the error, make sure sure the arrays you are concatenating have the same shape or use the numpy.column_stack() or numpy.row_stack() methods.

Here is an example of how the error occurs.

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) # โ›”๏ธ ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s) arr3 = np.concatenate([arr1, arr2]) print(arr3)

all input arrays must have same number of dimensions

You can use the array.shape attribute to get the shape of an array.

The array.shape() method returns a tuple containing the array's dimensions.

The first array in the example is 1-dimensional and has 1 row and 2 columns.

main.py
arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,)

The second array has 2 rows and 2 columns.

main.py
arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2)

Trying to concatenate arrays of different shapes causes the error.

# Use the numpy.column_stack() method to solve the error

One way to solve the error is to use the numpy.column_stack method instead of using numpy.concatenate.

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) arr3 = np.column_stack((arr1, arr2)) # [[1 3 4] # [2 5 6]] print(arr3)

using numpy column stack to solve the error

The code for this article is available on GitHub

The numpy.column_stack() method stacks 1-D arrays as columns into a 2-D array.

The only argument the method takes is a tuple containing a sequence of 1-D or 2-D arrays that you want to stack.

All of the arrays must have the same first dimension.

2-D arrays are stacked as-is and 1-D arrays are turned into 2-D columns first.

# Using the numpy.row_stack() method to solve the error

You can also solve the error by using the numpy.row_stack() method.

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) arr3 = np.row_stack([arr1, arr2]) # [[1 2] # [3 4] # [5 6]] print(arr3)

using numpy row stack method to solve the error

The code for this article is available on GitHub

The numpy.row_stack() method stacks the supplied arrays vertically (row-wise).

This is equivalent to concatenation along the first axis after 1-D arrays of shape (N,) have been reshaped to (1, N).

# Solve the error by extending the 1-D array to 2-D

Another way to solve the error is to extend the 1-D array to 2-D when calling numpy.concatenate().

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) arr3 = np.concatenate((arr1[:, None], arr2), axis=1) # [[1 3 4] # [2 5 6]] print(arr3)

extend 1 d array to 2d before concatenating

The code for this article is available on GitHub

We first had to extend the 1-D array to a 2-D array.

main.py
import numpy as np arr1 = np.array([1, 2]) # [[1] # [2]] print(arr1[:, None])

And then we used 1 as the axis when concatenating the two 2-D arrays.

main.py
arr3 = np.concatenate((arr1[:, None], arr2), axis=1) # [[1 3 4] # [2 5 6]] print(arr3)

# Flattening the 2-D array to solve the error

Another way to solve the error is to flatten the 2-D array when concatenating.

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) arr3 = np.concatenate((arr1, arr2.flatten())) # [1 2 3 4 5 6] print(arr3)

flatten the 2 d array to solve the error

The code for this article is available on GitHub

The code sample uses the ndarray.flatten() method to flatten the 2-D array to a one-dimensional array before calling concatenate().

main.py
import numpy as np arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) print(arr2.flatten()) # ๐Ÿ‘‰๏ธ [3 4 5 6]

# Using the numpy.c_ class to solve the error

You can also use the numpy.c_ class to solve the error.

main.py
import numpy as np arr1 = np.array([1, 2]) print(arr1.shape) # ๐Ÿ‘‰๏ธ (2,) arr2 = np.array([ [3, 4], [5, 6] ]) print(arr2.shape) # ๐Ÿ‘‰๏ธ (2, 2) arr3 = np.c_[arr1, arr2] # [[1 3 4] # [2 5 6]] print(arr3)

using numpy c class to solve the error

The code for this article is available on GitHub

The numpy.c_ class translates slice objects to concatenation along the second axis.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev