You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: DictVectorizer throws exception for empty/unknown feature dict #1903
The DictVectorizer throws a ValueError when no features in the dict are known instead of returning an empty vector.
>>> from sklearn.feature_extraction import DictVectorizer
>>> a = DictVectorizer()
>>> data = [{"j":1, "n":1}]
>>> a.fit(data)
DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sparse=True)
>>> a.transform([{"j":1}])
<1x2 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>
>>> a.transform([])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/tobi/projectname/eggs/scikit_learn-0.13.1-py2.7-linux-i686.egg/sklearn/feature_extraction/dict_vectorizer.py", line 218, in transform
indices = np.frombuffer(indices, dtype=np.int32)
ValueError: offset must be non-negative and smaller than buffer lenth (0)
>>> a.transform([{}])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/tobi/projectname/eggs/scikit_learn-0.13.1-py2.7-linux-i686.egg/sklearn/feature_extraction/dict_vectorizer.py", line 218, in transform
indices = np.frombuffer(indices, dtype=np.int32)
ValueError: offset must be non-negative and smaller than buffer lenth (0)
>>> a.transform([{"x":1}])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/tobi/projectname/eggs/scikit_learn-0.13.1-py2.7-linux-i686.egg/sklearn/feature_extraction/dict_vectorizer.py", line 218, in transform
indices = np.frombuffer(indices, dtype=np.int32)
ValueError: offset must be non-negative and smaller than buffer lenth (0)
should raise an exception, although the message could be friendlier.
The no-features case is actually a bug in older NumPy versions. CountVectorizer contains (or contained, prior to the last refactoring) a workaround that can be applied to DictVectorizer as well.
On current master, the second two return empty arrays, which I think is fine.
The first raises an "invalid shape" in the sparse matrix that is created....
Activity
amueller commentedon Apr 27, 2013
Thanks for reporting.
larsmans commentedon Apr 28, 2013
should raise an exception, although the message could be friendlier.
The no-features case is actually a bug in older NumPy versions.
CountVectorizercontains (or contained, prior to the last refactoring) a workaround that can be applied toDictVectorizeras well.amueller commentedon May 4, 2013
On current master, the second two return empty arrays, which I think is fine.
The first raises an "invalid shape" in the sparse matrix that is created....
tobigue commentedon May 4, 2013
That sounds good, empty array is what you would expect from a dict with unknown/no features. :)
TST extend DictVectorizer tests to empty sample