I have the following code and the part of data in ultimate is also provided as you can see. When I run this code in vs code in my computer I get this error:
ultimate = np.array(ultimate) ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (81, 2) + inhomogeneous part.
However, when. I run this code on another computer. It doesn’t show any error and it runs totally correct. Can anyone suggest me what’s wrong with my code?
for q in nonoutliersPos: ultimate.append(cluster0Data[q]) print('ultimate:',ultimate) ultimate = np.array(ultimate) ultimate: [(array([3.72660978e+08, 4.16826783e+00, 6.76000000e-04, 0.00000000e+00, 0.00000000e+00]), 199), (array([3.72660954e+08, 4.16826783e+00, 6.76000000e-04, 0.00000000e+00, 0.00000000e+00]), 201), (array([3.72660978e+08, 4.16826783e+00, 6.76000000e-04, 0.00000000e+00, 0.00000000e+00]), 202), (array([3.72660954e+08, 4.16826783e+00, 6.76000000e-04, 0.00000000e+00, 0.00000000e+00]), 203), (array([ 2.63184231e+08, 4.16799028e+00, -7.13000000e-05, -4.55000000e-13, 0.00000000e+00]), 211), (array([ 2.63184237e+08, 4.16799028e+00, -7.13000000e-05, -4.55000000e-13, 0.00000000e+00]), 212)]
I wrote pip list in command line. Here is the answer:
Package Version --------------- ------------ contourpy 1.2.0 cycler 0.12.1 et-xmlfile 1.1.0 fonttools 4.44.3 joblib 1.3.2 kiwisolver 1.4.5 matplotlib 3.8.2 networkx 3.2.1 numpy 1.24.3 openpyxl 3.1.2 packaging 23.2 pandas 2.0.3 Pillow 10.1.0 pip 22.3.1 plotly 5.18.0 pyparsing 3.1.1 python-dateutil 2.8.2 pytz 2023.3.post1 ruptures 1.1.8 scikit-fuzzy 0.4.2 scikit-learn 1.3.2 scipy 1.11.4 setuptools 65.5.0 six 1.16.0 tenacity 8.2.3 threadpoolctl 3.2.0 tzdata 2023.3
The error you’re encountering indicates that you’re trying to create a NumPy array from a list of elements where each element is a tuple containing an array and an integer. NumPy is complaining because it’s expecting a homogeneous array, and the shapes of the arrays in your tuples seem to be inconsistent.
In the code you provided, ultimate is a list of tuples, where each tuple contains an array and an integer. When you try to convert this list to a NumPy array using np.array(ultimate), it expects the arrays in each tuple to have the same shape, but it seems that they don’t.
ultimate
np.array(ultimate)
One approach to handle this situation is to concatenate the arrays in each tuple separately and then create a NumPy array. Here’s an example:
import numpy as np ultimate = [(np.array([1, 2, 3]), 4), (np.array([5, 6, 7]), 8)] # Concatenate the arrays in each tuple concatenated_arrays = [np.concatenate((arr, [value])) for arr, value in ultimate] # Convert the list of concatenated arrays to a NumPy array result = np.array(concatenated_arrays) print(result)
In your case, you can modify your code as follows:
ultimate = [np.concatenate((arr, [value])) for arr, value in ultimate] ultimate = np.array(ultimate)
This should help you avoid the error you’re encountering.