close

[Solved] ValueError: setting an array element with a sequence

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: setting an array element with a sequence in Python. So Here I am Explain to you all the possible solutions here.

Without Wasting your time, Lets start This Article to Solve This Error.

How To Solve ValueError: setting an array element with a sequence Error ?

Question: How To Solve ValueError: setting an array element with a sequence Error ?
Answer: To Solve ValueError: setting an array element with a sequence Error Just refer to our example. the problem was another. I was trying to convert lists of lists of int to the array. The problem was that there was one list with a different length than others. If you want to prove it, you must do.

Solution 1

For example

numpy.array([[1,2], [2, 3, 4]])

or

numpy.array([[1,2], [2, [3, 4]]])

will yield this error message, because the shape of the input list isn’t a (generalised) “box” that can be turned into a multidimensional array. So probably UnFilteredDuringExSummaryOfMeansArray contains sequences of different lengths.

Solution 2

Another possible cause for this error message is trying to use a string as an element in an array of type float:

numpy.array([1.2, "abc"], dtype=float)

That is what you are trying according to your edit. If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which enables the array to hold arbitrary Python objects:

numpy.array([1.2, "abc"], dtype=object)

Without knowing what your code shall accomplish, I can’t judge if this is what you want.

Solution 3

for those who are having trouble with similar problems in Numpy, a very simple solution would be: defining dtype=object when defining an array for assigning values to it. for instance:

out = np.empty_like(lil_img, dtype=object)

Solution 4

the problem was another. I was trying convert lists of lists of int to array. The problem was that there was one list with a different length than others. If you want to prove it, you must do:

print([i for i,x in enumerate(list) if len(x) != 560])

In my case, the length reference was 560.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment