How to Fix: All input arrays must have same number of dimensions
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
import numpy as np
np_array1 = np.array([[ 2 , 3 ],[ 2 , 4 ]])
np_array2 = np.array([[ 8 , 9 , 10 ], [ 10 , 11 , 12 ]])
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
import numpy as np
np_array1 = np.array([[ 2 , 3 ],[ 2 , 4 ]])
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
import numpy as np
np_array1 = np.array([[ 2 , 3 ],[ 2 , 4 ]])
np_array2 = np.array([[ 8 , 9 ], [ 10 , 11 ]])
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]]
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
import numpy as np
np_array1 = np.array([[ 2 , 3 ],[ 2 , 4 ]])
np_array2 = np.array([[ 8 , 9 , 10 ], [ 10 , 11 , 12 ]])
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
import numpy as np
np_array1 = np.array([ 2 , 3 , 4 ])
np_array2 = np.array([[ 8 , 9 , 10 ], [ 10 , 11 , 12 ]])
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
import numpy as np
np_array1 = np.array([[ 2 , 3 ],[ 2 , 4 ]])
np_array2 = np.array([[ 8 , 9 , 10 ], [ 10 , 11 , 12 ]])
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]]
Similar Reads
How to Fix in R: incorrect number of dimensions
In this article, we will discuss how we can fix the "incorrect number of dimensions" error in R Programming Language. One common error that one may face in R is: Error in [x, 10] : incorrect number of dimensions The R compiler produces such an error when one tries to refer to an object by providing
2 min read
How to Fix: ValueError: All arrays must be of the same length
In this article we will fix the error: All arrays must be of the same length. We get this error when we create a pandas data frame with columns of different lengths but when we are creating pandas dataframe the columns should be equal instead there can be NaN in the deficient cell of the column. Err
2 min read
Minimum number of steps to make all elements of array equal
Given two arrays A[] and B[] of the same length, the task is to find the minimum number of steps required to make all the elements of the array equal by replacing the element with the difference of the corresponding element from array B. Note: If this is not possible print -1.Examples: Input: A[] =
8 min read
How to Fix in R: Arguments imply differing number of rows
In this article, we are going to see how to fix Arguments that imply differing numbers of rows in R Programming Language. One error that we may encounter in R is: arguments imply differing number of rows: 6, 5 The compiler produces such an error when we try to create a data frame and the number of r
2 min read
How to get the number of dimensions of a matrix using NumPy in Python?
In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available
3 min read
What is meant by dimensionality of an Array?
The dimension of an array can simply be defined as the number of subscripts or indices required to specify a particular element of the array. Dimension has its own meaning in the real world too and the dimension of an array can be associated with it like:- 1-dimension array can be viewed as 1-axis i
3 min read
Multi-Dimensional Arrays in Objective-C
Arrays are basically defined as linear data structures that are used to store similar types of data elements. Arrays can be single-dimensional or multi-dimensional. Â As the name suggests that multi-dimensional means it is a data structure that is used to store similar types of data like(integers, fl
5 min read
How to Fix: ValueError: setting an array element with a sequence
In this article, we will discuss how to fix ValueError: setting array element with a sequence using Python. Error which we basically encounter when we using Numpy library is ValueError: setting array element with a sequence. We face this error basically when we creating array or dealing with numpy.a
3 min read
How to create an array in R
The array is the fundamental data structure in R used to store multiple elements of the same data type. In this article, we will explore two different approaches to creating an array in R Programming Language. Creating an array in RBelow are the approaches for creating an array in R. Using array() f
4 min read
How to Fix: TypeError: ânumpy.floatâ object is not callable?
In this article, we are going to see how to fix TypeError: ânumpy.floatâ object is not callable in Python. There is only one case in which we can see this error: If we try to call a NumPy array as a function, we are most likely to get such an error. Example: C/C++ Code import numpy as np a = np.arra
1 min read