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'?
By Pranit Sharma Last updated : December 26, 2023
Problem statement
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.
'ValueError: The requested array has an inhomogeneous shape after 1 dimensions' – How to fix?
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
To fix this issue, put an equal number of elements in each row and you must specify 'dtype=object' when creating the ndarray.
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([[1,2,3,4,5],[6,7,8,9,10]], dtype="object")
# Display original array
print("Original array:\n",arr,"\n")
Output
In this example, we have used the following Python basic topics that you should learn:
Python NumPy Programs »
Advertisement
Advertisement