-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -53,6 +54,26 @@ def test_feature_hasher_pairs(): | |
assert_equal([1, 3, 4], x2_nz) | ||
|
||
|
||
def test_feature_hasher_pairs_with_string_values(): | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for my ignorance, but how do we know these values? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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))] | ||
|
There was a problem hiding this comment.
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?