FeatureHasher
Implements feature hashing, aka the hashing trick.
This class turns sequences of symbolic feature names (strings) into scipy.sparse matrices, using a hash function to compute the matrix column corresponding to a name. The hash function employed is the signed 32-bit version of Murmurhash3.
Feature names of type byte string are used as-is. Unicode strings are converted to UTF-8 first, but no Unicode normalization is done. Feature values must be (finite) numbers.
This class is a low-memory alternative to DictVectorizer and CountVectorizer, intended for large-scale (online) learning and situations where memory is tight, e.g. when running prediction code on embedded devices.
Read more in the User Guide.
Python Reference (opens in a new tab)
Constructors
constructor()
Signature
new FeatureHasher(opts?: object): FeatureHasher;
Parameters
Name | Type | Description |
---|---|---|
opts? | object | - |
opts.alternate_sign? | boolean | When true , an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection. Default Value true |
opts.dtype? | any | The type of feature values. Passed to scipy.sparse matrix constructors as the dtype argument. Do not set this to bool, np.boolean or any unsigned integer type. |
opts.input_type? | string | Choose a string from {‘dict’, ‘pair’, ‘string’}. Either “dict” (the default) to accept dictionaries over (feature_name, value); “pair” to accept pairs of (feature_name, value); or “string” to accept single strings. feature_name should be a string, while value should be a number. In the case of “string”, a value of 1 is implied. The feature_name is hashed to find the appropriate column for the feature. The value’s sign might be flipped in the output (but see non_negative, below). Default Value 'dict' |
opts.n_features? | number | The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners. Default Value 2 |
Returns
Defined in: generated/feature_extraction/FeatureHasher.ts:29 (opens in a new tab)
Properties
_isDisposed
boolean
=false
Defined in: generated/feature_extraction/FeatureHasher.ts:27 (opens in a new tab)
_isInitialized
boolean
=false
Defined in: generated/feature_extraction/FeatureHasher.ts:26 (opens in a new tab)
_py
PythonBridge
Defined in: generated/feature_extraction/FeatureHasher.ts:25 (opens in a new tab)
id
string
Defined in: generated/feature_extraction/FeatureHasher.ts:22 (opens in a new tab)
opts
any
Defined in: generated/feature_extraction/FeatureHasher.ts:23 (opens in a new tab)
Accessors
py
Signature
py(): PythonBridge;
Returns
PythonBridge
Defined in: generated/feature_extraction/FeatureHasher.ts:60 (opens in a new tab)
Signature
py(pythonBridge: PythonBridge): void;
Parameters
Name | Type |
---|---|
pythonBridge | PythonBridge |
Returns
void
Defined in: generated/feature_extraction/FeatureHasher.ts:64 (opens in a new tab)
Methods
dispose()
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Signature
dispose(): Promise<void>;
Returns
Promise
<void
>
Defined in: generated/feature_extraction/FeatureHasher.ts:115 (opens in a new tab)
fit()
Only validates estimator’s parameters.
This method allows to: (i) validate the estimator’s parameters and (ii) be consistent with the scikit-learn transformer API.
Signature
fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | any | Not used, present here for API consistency by convention. |
opts.y? | any | Not used, present here for API consistency by convention. |
Returns
Promise
<any
>
Defined in: generated/feature_extraction/FeatureHasher.ts:134 (opens in a new tab)
fit_transform()
Fit to data, then transform it.
Fits transformer to X
and y
with optional parameters fit\_params
and returns a transformed version of X
.
Signature
fit_transform(opts: object): Promise<any[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | Input samples. |
opts.fit_params? | any | Additional fit parameters. |
opts.y? | ArrayLike | Target values (undefined for unsupervised transformations). |
Returns
Promise
<any
[]>
Defined in: generated/feature_extraction/FeatureHasher.ts:174 (opens in a new tab)
init()
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Signature
init(py: PythonBridge): Promise<void>;
Parameters
Name | Type |
---|---|
py | PythonBridge |
Returns
Promise
<void
>
Defined in: generated/feature_extraction/FeatureHasher.ts:73 (opens in a new tab)
set_output()
Set output container.
See Introducing the set_output API for an example on how to use the API.
Signature
set_output(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.transform? | "default" | "pandas" | Configure output of transform and fit\_transform . |
Returns
Promise
<any
>
Defined in: generated/feature_extraction/FeatureHasher.ts:223 (opens in a new tab)
transform()
Transform a sequence of instances to a scipy.sparse matrix.
Signature
transform(opts: object): Promise<any[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.raw_X? | any | Samples. Each sample must be iterable an (e.g., a list or tuple) containing/generating feature names (and optionally values, see the input_type constructor argument) which will be hashed. raw_X need not support the len function, so it can be the result of a generator; n_samples is determined on the fly. |
Returns
Promise
<any
[]>
Defined in: generated/feature_extraction/FeatureHasher.ts:256 (opens in a new tab)