All the input arrays must have same number of dimensions

Borislav Hadzhiev

Last updated: Apr 12, 2024

Reading timeยท4 min

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

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

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.

`numpy.column_stack()`

method to solve the errorOne 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)`

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.

`numpy.row_stack()`

method to solve the errorYou 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)`

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).

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

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

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

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]`

`numpy.c_`

class to solve the errorYou 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)`

The code for this article is available on GitHub

The `numpy.c_`

class translates slice objects to concatenation along the second
axis.

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

- Pandas: Select Rows between two values in DataFrame
- Pandas: How to Filter a DataFrame by value counts
- Pandas: Convert a DataFrame to a List of Dictionaries
- Pandas: GroupBy columns with NaN (missing) values
- Pandas: Split a Column of Lists into Multiple Columns
- Matplotlib: No artists with labels found to put in legend
- ValueError: Expected object or value with
`pd.read_json()`

- Mixing dicts with non-Series may lead to ambiguous ordering