Home »
Python »
Python Programs
How to fix 'ValueError: The requested array has an inhomogeneous shape after 1 dimensions'?
Learn, how can we fix 'ValueError: The requested array has an inhomogeneous shape after 1 dimensions'?
Submitted by Pranit Sharma, on March 08, 2023
'ValueError: The requested array has an inhomogeneous shape after 1 dimensions' – How to fix?
Suppose we are working on N-D arrays and we are creating different rows for an N-D array. Many times, we face this ValueError that says that the requested array has an inhomogeneous shape after 1 dimensions.
This type of error occurs because we do not put an equal number of elements in each row. To solve this problem, we must put an equal number of values in all the rows of an ND numpy array.
Let us understand with the help of an example,
Example
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([[1,2,3,4,5],[6,7,8,9]])
# Display original array
print("Original array:\n",arr,"\n")
Output
Python code to fix "The requested array has an inhomogeneous shape after 1 dimensions" Error
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([[1,2,3,4,5],[6,7,8,9,10]])
# Display original array
print("Original array:\n",arr,"\n")
Output
Python NumPy Programs »