Last updated: Apr 12, 2024
Reading timeยท4 min
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.
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.
arr1 = np.array([1, 2]) print(arr1.shape) # ๐๏ธ (2,)
The second array has 2 rows and 2 columns.
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
.
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.
numpy.row_stack()
method to solve the errorYou can also solve the error by using the numpy.row_stack() method.
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).
Another way to solve the error is to extend the 1-D array to 2-D when calling numpy.concatenate().
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.
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.
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.
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()
.
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.
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.
You can learn more about the related topics by checking out the following tutorials:
pd.read_json()