<s> from __future__ import unicode_literals <EOL> import numpy as np <EOL> from sklearn . feature_extraction import FeatureHasher <EOL> from nose . tools import assert_raises , assert_true <EOL> from numpy . testing import assert_array_equal , assert_equal <EOL> def test_feature_hasher_dicts ( ) : <EOL> h = FeatureHasher ( n_features = <NUM_LIT:16> ) <EOL> assert_equal ( "<STR_LIT>" , h . input_type ) <EOL> raw_X = [ { "<STR_LIT:foo>" : "<STR_LIT:bar>" , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:foo>" : "<STR_LIT>" , "<STR_LIT>" : u"<STR_LIT>" } ] <EOL> X1 = FeatureHasher ( n_features = <NUM_LIT:16> ) . transform ( raw_X ) <EOL> gen = ( iter ( d . items ( ) ) for d in raw_X ) <EOL> X2 = FeatureHasher ( n_features = <NUM_LIT:16> , input_type = "<STR_LIT>" ) . transform ( gen ) <EOL> assert_array_equal ( X1 . toarray ( ) , X2 . toarray ( ) ) <EOL> def test_feature_hasher_strings ( ) : <EOL> raw_X = [ [ "<STR_LIT:foo>" , "<STR_LIT:bar>" , "<STR_LIT>" , "<STR_LIT:foo>" . encode ( "<STR_LIT:ascii>" ) ] , <EOL> [ "<STR_LIT:bar>" . encode ( "<STR_LIT:ascii>" ) , "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> for lg_n_features in ( <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:11> , <NUM_LIT:16> , <NUM_LIT> ) : <EOL> n_features = <NUM_LIT:2> ** lg_n_features <EOL> it = ( x for x in raw_X ) <EOL> h = FeatureHasher ( n_features , non_negative = True , input_type = "<STR_LIT:string>" ) <EOL> X = h . transform ( it ) <EOL> assert_equal ( X . shape [ <NUM_LIT:0> ] , len ( raw_X ) ) <EOL> assert_equal ( X . shape [ <NUM_LIT:1> ] , n_features ) <EOL> assert_true ( np . all ( X . data > <NUM_LIT:0> ) ) <EOL> assert_equal ( X [ <NUM_LIT:0> ] . sum ( ) , <NUM_LIT:4> ) <EOL> assert_equal ( X [ <NUM_LIT:1> ] . sum ( ) , <NUM_LIT:3> ) <EOL> assert_equal ( X . nnz , <NUM_LIT:6> ) <EOL> def test_feature_hasher_pairs ( ) : <EOL> raw_X = ( iter ( d . items ( ) ) for d in [ { "<STR_LIT:foo>" : <NUM_LIT:1> , "<STR_LIT:bar>" : <NUM_LIT:2> } , <EOL> { "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:4> , "<STR_LIT:foo>" : - <NUM_LIT:1> } ] ) <EOL> h = FeatureHasher ( n_features = <NUM_LIT:16> , input_type = "<STR_LIT>" ) <EOL> x1 , x2 = h . transform ( raw_X ) . toarray ( ) <EOL> x1_nz = sorted ( np . abs ( x1 [ x1 != <NUM_LIT:0> ] ) ) <EOL> x2_nz = sorted ( np . abs ( x2 [ x2 != <NUM_LIT:0> ] ) ) <EOL> assert_equal ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , x1_nz ) <EOL> assert_equal ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] , x2_nz ) <EOL> def test_feature_hasher_pairs_with_string_values ( ) : <EOL> raw_X = ( iter ( d . items ( ) ) for d in [ { "<STR_LIT:foo>" : <NUM_LIT:1> , "<STR_LIT:bar>" : "<STR_LIT:a>" } , <EOL> { "<STR_LIT>" : u"<STR_LIT:abc>" , "<STR_LIT>" : <NUM_LIT:4> , "<STR_LIT:foo>" : - <NUM_LIT:1> } ] ) <EOL> h = FeatureHasher ( n_features = <NUM_LIT:16> , input_type = "<STR_LIT>" ) <EOL> x1 , x2 = h . transform ( raw_X ) . toarray ( ) <EOL> x1_nz = sorted ( np . abs ( x1 [ x1 != <NUM_LIT:0> ] ) ) <EOL> x2_nz = sorted ( np . abs ( x2 [ x2 != <NUM_LIT:0> ] ) ) <EOL> assert_equal ( [ <NUM_LIT:1> , <NUM_LIT:1> ] , x1_nz ) <EOL> assert_equal ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:4> ] , x2_nz ) <EOL> raw_X = ( iter ( d . items ( ) ) for d in [ { "<STR_LIT>" : "<STR_LIT:abc>" } , <EOL> { "<STR_LIT>" : "<STR_LIT:abc>" } ] ) <EOL> x1 , x2 = h . transform ( raw_X ) . toarray ( ) <EOL> x1_nz = np . abs ( x1 [ x1 != <NUM_LIT:0> ] ) <EOL> x2_nz = np . abs ( x2 [ x2 != <NUM_LIT:0> ] ) <EOL> assert_equal ( [ <NUM_LIT:1> ] , x1_nz ) <EOL> assert_equal ( [ <NUM_LIT:1> ] , x2_nz ) <EOL> assert_equal ( x1 , x2 ) <EOL> def test_hash_empty_input ( ) : <EOL> n_features = <NUM_LIT:16> <EOL> raw_X = [ [ ] , ( ) , iter ( range ( <NUM_LIT:0> ) ) ] <EOL> h = FeatureHasher ( n_features = n_features , input_type = "<STR_LIT:string>" ) <EOL> X = h . transform ( raw_X ) <EOL> assert_array_equal ( X . A , np . zeros ( ( len ( raw_X ) , n_features ) ) ) <EOL> def test_hasher_invalid_input ( ) : <EOL> assert_raises ( ValueError , FeatureHasher , input_type = "<STR_LIT>" ) <EOL> assert_raises ( ValueError , FeatureHasher , n_features = - <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , FeatureHasher , n_features = <NUM_LIT:0> ) <EOL> assert_raises ( TypeError , FeatureHasher , n_features = '<STR_LIT>' ) <EOL> h = FeatureHasher ( n_features = np . uint16 ( <NUM_LIT:2> ** <NUM_LIT:6> ) ) <EOL> assert_raises ( ValueError , h . transform , [ ] ) <EOL> assert_raises ( Exception , h . transform , [ [ <NUM_LIT> ] ] ) <EOL> assert_raises ( Exception , h . transform , [ [ None ] ] ) <EOL> def test_hasher_set_params ( ) : <EOL> hasher = FeatureHasher ( ) <EOL> hasher . set_params ( n_features = np . inf ) <EOL> assert_raises ( TypeError , hasher . fit ) <EOL> def test_hasher_zeros ( ) : <EOL> X = FeatureHasher ( ) . transform ( [ { '<STR_LIT:foo>' : <NUM_LIT:0> } ] ) <EOL> assert_equal ( X . data . shape , ( <NUM_LIT:0> , ) ) </s>
