Skip to content
Related Articles

Related Articles

How to Fix: All input arrays must have same number of dimensions

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 11 Feb, 2022

In this article, we will fix the error: All input arrays must have the same number of dimensions in Python.

Cases of this error occurrence:

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 2 * 2
np_array1 = np.array([[2, 3],[2,4]])
 
# Creating a numpy array with dimension 2 * 3
np_array2 = np.array([[8, 9,10], [10,11,12]])
 
# Concatenating the two arrays
np_array3 = np.concatenate([np_array1,np_array2])
print(np_array3)


Output:

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 3

Reason for the error : 

When we are using concatenate function in the NumPy library for NumPy arrays the dimensions of both the arrays must match while concatenating. In this above example, the dimensions of np_array1 are 2 * 2 and the dimensions of np_array2 are 2 * 3 which is not compatible with concatenate function to concatenate the two arrays. 

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 2 * 2
np_array1 = np.array([[2, 3],[2,4]])
 
# Creating a numpy array with dimension 2 * 3
np_array2 = np.array([[8, 9,10], [10,11,12]])
 
print("Dimensions of np_array1 " + str(np_array1.shape)
       +" != "+"Dimensions of np_array2 "+ str(np_array2.shape))


Output:

Dimensions of np_array1 (2, 2) != Dimensions of np_array2 (2, 3)

Fixing the error: 

This error can be fixed by making the dimensions of both the arrays the same if we want to use concatenate function only.

Method 1: Using the concatenate function 

numpy.concatenate() function concatenate a sequence of arrays along an existing axis. 

Syntax: 

 np.concatenate([array1,array2]) 

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 2 * 2
np_array1 = np.array([[2, 3],[2,4]])
 
# Creating a numpy array with dimension 2 * 2
np_array2 = np.array([[8, 9], [10,11]])
 
# Concatenating the two arrays
np_array3 = np.concatenate([np_array1,np_array2])
print(np_array3)


Output:

In the output, the np_array1 gets appended or stacked on the top of the array np_array2.

[[ 2  3]
 [ 2  4]
 [ 8  9]
 [10 11]]

Method 2: Using row_stack() or column_stack() function

Another way to fix this error using the row_stack() or column_stack() function if the column dimension of both the arrays is the same then the row_stack() function can be used and if the column dimension of one array and the row dimension of the second array is the same then the column_stack() function can be used to fix the error and the arrays get stacked to the other array accordingly.

Syntax:

np.column_stack((array1,array2))

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 2 * 2
np_array1 = np.array([[2, 3],[2,4]])
 
# Creating a numpy array with dimension 2 * 3
np_array2 = np.array([[8, 9,10], [10,11,12]])
 
# Using the column_stack() function
np_array3 = np.column_stack((np_array1,np_array2))
print(np_array3)


Output:

In the output, the np_array1 gets appended or stacked in the left to the array np_array2.

[[ 2  3  8  9 10]
 [ 2  4 10 11 12]]

Syntax for row_stack() function:

np.row_stack((array1,array2))

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 1 * 3
 
np_array1 = np.array([2, 3,4])
 
# Creating a numpy array with dimension 2 * 3
np_array2 = np.array([[8, 9,10], [10,11,12]])
 
# Using the row_stack() function
np_array3 = np.row_stack((np_array1,np_array2))
print(np_array3)


Output: 

In the output, the np_array1 gets appended or stacked on the top of the array np_array2.

[[ 2  3  4]
 [ 8  9 10]
 [10 11 12]]

Method 3: Using the np.c() function

The other way to fix the error is to use the np. c_() function which works the same as the np.column_stack() function.

Syntax for column_stack() function:

np.c_[array1,array2] 

Python3




# Importing numpy library
import numpy as np
 
# Creating a numpy array with dimension 2 * 2
np_array1 = np.array([[2, 3],[2,4]])
 
# Creating a numpy array with dimension 2 * 3
np_array2 = np.array([[8, 9,10], [10,11,12]])
 
# Using the np.c_() function
np_array3 = np.c_[np_array1,np_array2]
print(np_array3)


 
Output:

In the output, the np_array1 gets appended or stacked in the left to the array np_array2.

[[ 2  3  8  9 10]
 [ 2  4 10 11 12]] 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!