8000 "setting an array element with a sequence" error could be improved. · Issue #6584 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

"setting an array element with a sequence" error could be improved. #6584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amueller opened this issue Oct 29, 2015 · 17 comments
Closed

"setting an array element with a sequence" error could be improved. #6584

amueller opened this issue Oct 29, 2015 · 17 comments

Comments

@amueller
Copy link

This is a bit of a nitpick, but I think this would improve user-friendlyness.
Currently np.asarray([[1, 2], [2, 3, 4]], dtype=np.float) yields "setting an array element with a sequence."

I think it would be more helpful if it yielded something like "Can't create array from lists. Column length %d of column %d doesn't match column length %d."

I imagine the error bubbles up from somewhere deep in the code and makes perfect sense where it is raised. It is just something that comes up a lot, and a more friendly and / or informative message could be helpful. So maybe we could catch it further up and include more information if possible.

@argriffing
Copy link
Contributor

This is a duplicate of #5303.

@amueller
Copy link
Author

indeed, sorry for the noise.

@argriffing
Copy link
Contributor

Re-opening in light of #5303 (comment).

There are a few different but related issues. One issue is explicit vs. implicit dtype=object array creation. The github issue for that is #5353 (also #6070, I see). Another issue is the possibility to create ndarrays of objects with conformable shapes without automatically peeking into those objects and trying to figure out what the user wants. That issue is #5933.

We could keep this issue open, for improving the error message for specific cases like np.asarray([[1, 2], [2, 3, 4]], dtype=np.float). If the error message could be special-cased for np.asarray(a, dtype=b) even for really specific kinds of a and b, this could be useful. For example, maybe a special case could apply when a is a Python list recursively containing more lists and numeric values (and maybe only when the all leaves of the tree defined by the list all share the same depth?) and where b is some simple numeric dtype.

Here are a few more cases that give the same error message, but which may or may not be in the scope of this issue.

>>> np.array([[[1, 2], [3, 4], [5, 6]], [[1], [2], [3]]], dtype=int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
>>> np.array([[1, 2, 3], 4], dtype=int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
>>> np.array([(1, 2), (2, 3, 4)], dtype=int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

@amueller
Copy link
Author

Thanks for your summary. I think we are on the same page.
Btw, my motivation was that this might be inside a library, and it might be very non-obvious to the user why a line is failing. I probably won't have time to work on this right now :-/

@argriffing
Copy link
Contributor

Here's an analogous example with structured arrays, giving a different error message.

>>> np.array([[(1, 1), (2, 2)], [(2, 2), (3, 3), (4, 4)]], dtype=[('foo', '>i4'), ('bar', '>i4')])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object

When the lists are conformable there is no error.

>>> np.array([[(1, 1), (2, 2), (3, 3)], [(2, 2), (3, 3), (4, 4)]], dtype=[('foo', '>i4'), ('bar', '>i4')])
array([[(1, 1), (2, 2), (3, 3)],
       [(2, 2), (3, 3), (4, 4)]], 
      dtype=[('foo', '>i4'), ('bar', '>i4')])

@amueller
Copy link
Author

I wanna bump this in sight of the BoF in terms of directions. I think better error messages in general would be great, with this my "favorite" examples.

@rgommers rgommers added this to the 1.16.0 release milestone Jul 12, 2018
@rgommers
Copy link
Member

Thanks @amueller. This has 31 thumbs up, clearly valuable - tagging for the next release to make sure it doesn't drift down the list again.

A new issue/strategy for better error messages in general would also be useful. Could be good to discuss during the sprint.

@amueller
Copy link
Author

I tried to think about others and/or come up with general strategies but drew a blank. Maybe looking at highly voted stack overflow issues might help to find common problems? (Gonna do that for sklearn right now lol).

@amueller
Copy link
Author
amueller commented Jul 12, 2018

Hmm most errors are install errors or this one lol https://stackoverflow.com/search?q=%5Bnumpy%5D+error&tab=votes

@charris
Copy link
Member
charris commented Nov 17, 2018

Might be good to discuss this at next BIDS meeting. @stefanv, @rgommers, Ping.

@WarrenWeckesser
Copy link
Member

I removed the 1.18.0 milestone. We're not forgetting about this! There are new designs in progress that will affect this issue, but they will not be implemented for 1.18.

@seberg
Copy link
Member
seberg commented Jun 17, 2020

I have a branch, where the first example will give this:

In [1]: np.asarray([[1, 2], [2, 3, 4]], dtype=np.float64)                                                               
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

I could try to make it more useful, any suggestions?

@amueller
Copy link
Author
amueller commented Jun 17, 2020

Thanks for working on this, this is already much better!

Do you keep the first part for backward compatibility? It's not really meaningful to a user, right?
I'm not sure how tricky it would be but I think the ideal would be to give the first inhomogeneous part, say "shape (2,) in [1] and shape(3,) in [1]" (or some much better phrasing of this).

@seberg
Copy link
Member
seberg commented Jun 17, 2020

Yeah, I thought so, plus there are some stranger paths, where we give this error but do not have the context. Although, I suppose there may not be a need to align those, because in those other paths the message is more logic, because it ends up being things like arr[0] = [1,2,3]

Thats true, that would be nice. I could give the first inhomogeneous part, but it may be hard to report the element where the mismatch occurred. Maybe something for later, rephrasing I can do now. I also have a "failed shape", but I would need two values for the mismatched dimension, and ideally know where exactly the mismatch was.

I won't look at it right now, but I think after my PR is in, it may be possible (or at least easier) to expand the message!

@amueller
Copy link
Author

I think this is already a big improvement, there's definitely a trade-off in how much detail to provide.

@seberg
Copy link
Member
seberg commented Jul 9, 2020

I am going to close the issue now, since gh-16200 is in, and the error is now better. But, I admit there is probably still a lot of room for improvement (if just rephrasing), and that is now much easier to do, so I invite contributions or suggestions!

@seberg seberg closed this as completed Jul 9, 2020
@bhavaygg
Copy link

I have a 3-D numpy array and im getting the same error if i try to change its dataype to float. Also, if i try using the same data without changing anything with sklearn I get the same error while if I use it with keras, I get ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)..
I am trying to do a machine learning task and my current dataset is a numpy array of shape (556,1) and for every sample it have 27 features so (27,1) and each feature has variable length and the same feature might have a different shape for different samples.
Any help on how I get fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
0