"setting an array element with a sequence" error could be improved.
See original GitHub issueThis 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.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:87
- Comments:17 (9 by maintainers)
Top Results From Across the Web
How to Fix: ValueError: setting an array element with a ...
Here we have seen that this error is cause because we are assigning array as a element to array which accept string data-type....
Read more >ValueError: setting an array element with a sequence
Possible reason 1: trying to create a jagged array. You may be creating an array from a list that isn't shaped like a...
Read more >[FIXED] ValueError: setting an array element with a sequence
Solution: To solve this error, you have to set the data type (dtype) ...
Read more >ValueError: setting an array element with a sequence - STechies
While programming in Python, especially Numpy a library in Python, programmers encounter an error called ValueError: setting an array element with a sequence....
Read more >Valueerror Setting an Array Element With a Sequence.: Fix It ...
The easiest way to fix the valueerror setting an array element with a sequence code is to remove the arrays with different dimensions....
Read more >Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Re-opening in light of https://github.com/numpy/numpy/issues/5303#issuecomment-152354367.
There are a few different but related issues. One issue is explicit vs. implicit
dtype=object
array creation. The github issue for that is https://github.com/numpy/numpy/issues/5353 (also https://github.com/numpy/numpy/issues/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 https://github.com/numpy/numpy/issues/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 fornp.asarray(a, dtype=b)
even for really specific kinds ofa
andb
, this could be useful. For example, maybe a special case could apply whena
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 whereb
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.
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?