# All the input arrays must have same number of dimensions

Last updated: Apr 12, 2024
4 min

## #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
```Copied!```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)
``````

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
```Copied!```arr1 = np.array([1, 2])

print(arr1.shape)  # ๐๏ธ (2,)
``````

The second array has 2 rows and 2 columns.

main.py
```Copied!```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
```Copied!```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)
``````

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
```Copied!```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)
``````

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
```Copied!```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)
``````

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

main.py
```Copied!```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
```Copied!```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
```Copied!```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)
``````

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

main.py
```Copied!```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
```Copied!```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)
``````

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