8000 [MRG+1] Fix: FeatureHasher now accepts string values by devashishd12 · Pull Request #6173 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+1] Fix: FeatureHasher now accepts string values #6173

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

Merged
merged 1 commit into from
Mar 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ New features
Enhancements
............

- :class:`feature_extraction.FeatureHasher` now accepts string values.
(`#6173 <https://github.com/scikit-learn/scikit-learn/pull/6173>`_) By `Ryad Zenine`_
and `Devashish Deshpande`_.

- The cross-validation iterators are replaced by cross-validation splitters
available from :mod:`model_selection`. These expose a ``split`` method
that takes in the data and yields a generator for the different splits.
Expand Down Expand Up @@ -4121,3 +4125,5 @@ David Huard, Dave Morrill, Ed Schofield, Travis Oliphant, Pearu Peterson.
.. _Jonathan Arfa: https://github.com/jarfa

.. _Anish Shah: https://github.com/AnishShah

.. _Ryad Zenine: https://github.com/ryadzenine
10 changes: 9 additions & 1 deletion sklearn/feature_extraction/_hashing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from libc.stdlib cimport abs
cimport numpy as np
import numpy as np

from ..externals.six import string_types

from sklearn.utils.murmurhash cimport murmurhash3_bytes_s32

np.import_array()
Expand Down Expand Up @@ -43,7 +45,12 @@ def transform(raw_X, Py_ssize_t n_features, dtype):

for x in raw_X:
for f, v in x:
value = v
if isinstance(v, string_types):
f = "%s%s%s" % (f, '=', v)
value = 1
else:
value = v

if value == 0:
continue

Expand All @@ -53,6 +60,7 @@ def transform(raw_X, Py_ssize_t n_features, dtype):
# all exceptions. Add "except *" there?
elif not isinstance(f, bytes):
raise TypeError("feature names must be strings")

h = murmurhash3_bytes_s32(f, 0)

array.resize_smart(indices, len(indices) + 1)
Expand Down
23 changes: 22 additions & 1 deletion sklearn/feature_extraction/tests/test_feature_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def test_feature_hasher_dicts():
h = FeatureHasher(n_features=16)
assert_equal("dict", h.input_type)

raw_X = [{"dada": 42, "tzara": 37}, {"gaga": 17}]
raw_X = [{"foo": "bar", "dada": 42, "tzara": 37},
{"foo": "baz", "gaga": u"string1"}]
X1 = FeatureHasher(n_features=16).transform(raw_X)
gen = (iter(d.items()) for d in raw_X)
X2 = FeatureHasher(n_features=16, input_type="pair").transform(gen)
Expand Down Expand Up @@ -53,6 +54,26 @@ def test_feature_hasher_pairs():
assert_equal([1, 3, 4], x2_nz)


def test_feature_hasher_pairs_with_string_values():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this test too then? I'll just add this to the existing test?

raw_X = (iter(d.items()) for d in [{"foo": 1, "bar": "a"},
{"baz": u"abc", "quux": 4, "foo": -1}])
h = FeatureHasher(n_features=16, input_type="pair")
x1, x2 = h.transform(raw_X).toarray()
x1_nz = sorted(np.abs(x1[x1 != 0]))
x2_nz = sorted(np.abs(x2[x2 != 0]))
assert_equal([1, 1], x1_nz)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my ignorance, but how do we know these values?

96F2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we'll transform the first row (stored in x1), the "a" will be hashed as 1. Here x1 will be: [ 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] and x2 will be: [ 0. 0. 0. 1. 4. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -1.].

assert_equal([1, 1, 4], x2_nz)

raw_X = (iter(d.items()) for d in [{"bax": "abc"},
{"bax": "abc"}])
x1, x2 = h.transform(raw_X).toarray()
x1_nz = np.abs(x1[x1 != 0])
x2_nz = np.abs(x2[x2 != 0])
assert_equal([1], x1_nz)
assert_equal([1], x2_nz)
assert_equal(x1, x2)


def test_hash_empty_input():
n_features = 16
raw_X = [[], (), iter(range(0))]
Expand Down
0