Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Traceback (most recent call last):
File "C:\Users\leonardo.schettini\Documents\recrutai\recrutai\testenv\lib\site-packages\numpy\core\fromnumeric.py", line 1240, in squeeze
squeeze = a.squeeze
AttributeError: 'list' object has no attribute 'squeeze'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\leonardo.schettini\Documents\recrutai\recrutai\testenv\lib\site-packages\numpy\core\fromnumeric.py", line 1242, in squeeze
return _wrapit(a, 'squeeze')
File "C:\Users\leonardo.schettini\Documents\recrutai\recrutai\testenv\lib\site-packages\numpy\core\fromnumeric.py", line 42, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
File "C:\Users\leonardo.schettini\Documents\recrutai\recrutai\testenv\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence
Why does this happen?!
EDIT:
what versions?
python == 3.6
numpy == 1.14.3
why would you want to squeeze such a list?
I'm using an in-house dictionary to get the lemma of synonyms and acronyms. Some times, when the input word isn't in my dictionary, I choose to keep it as a lemma, some other times I prefer to drop the word. A lemma is defined as an array of n words.
In order to keep track of the lemma's real position in the input text, I wanted to add in my return list the lemma of 0 words, an empty list. At a first glance I thought squeeze would drop empty lists by its own...
np.squeeze
is really just a wrapper for a.squeeze()
, an array method. Your argument is a list, which doesn't have that method. So the function tries to make an array from the list:
In [325]: np.array(['', []])
ValueError: setting an array element with a sequence
You are trying to make an array from a mix of objects, a string and list.
In [326]: np.array(['', []],object)
Out[326]: array(['', list([])], dtype=object)
If you tell it to make an object dtype array, it can.
But with out that specification, it first tries to make string dtype array. But then has problems fitting the list into a string slot - hence the error.
Other than being a curiosity, why would you want to squeeze
such a list?
If the list were first, np.array
can make an object array:
In [327]: np.array([[],''])
Out[327]: array([list([]), ''], dtype=object)
This is just one of the quirks of how np.array
works when given a list of items of different sizes and or types. It's primary task is to make an array from a list of neatly nested lists of numbers or strings.
In [328]: np.array([[1,2,3,4]])
Out[328]: array([[1, 2, 3, 4]])
In [329]: np.squeeze([[1,2,3,4]])
Out[329]: array([1, 2, 3, 4])
Given a mix of items, it has to 'guess' as to what you really want; sometimes it guesses wrong, other times it gives up.
–
The problem is that you can't turn ['', []]
into an array because of the lack of a properly defined dimensionality. This can be seen by the fact the simply calling np.array(['', []])
leads to the error:
>>> np.array(['', []])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence
From the docs, it says that the argument input data a
must be array_like
, and ['', []]
is not (not all lists are array_like
). Unfortunately, there isn't a really easy failproof way to determine if it is array-like, as explained here:
In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).
You could do: np.squeeze([[''], []])
if you wanted though, as ([[''], []])
has properly defined dimensionality and can therefore be converted to a numpy
array:
>>> np.squeeze([[''], []])
array([list(['']), list([])], dtype=object)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.