Class: 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.

For an efficiency comparison of the different feature extractors, see FeatureHasher and DictVectorizer Comparison.

Read more in the User Guide.

Python Reference

Constructors

new FeatureHasher()

new FeatureHasher(opts?): FeatureHasher

Parameters

ParameterTypeDescription
opts?object-
opts.alternate_sign?booleanWhen 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.
opts.dtype?anyThe 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?stringChoose 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).
opts.n_features?numberThe 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.

Returns FeatureHasher

Defined in generated/feature_extraction/FeatureHasher.ts:31

Properties

PropertyTypeDefault valueDefined in
_isDisposedbooleanfalsegenerated/feature_extraction/FeatureHasher.ts:29
_isInitializedbooleanfalsegenerated/feature_extraction/FeatureHasher.ts:28
_pyPythonBridgeundefinedgenerated/feature_extraction/FeatureHasher.ts:27
idstringundefinedgenerated/feature_extraction/FeatureHasher.ts:24
optsanyundefinedgenerated/feature_extraction/FeatureHasher.ts:25

Accessors

py

Get Signature

get py(): PythonBridge

Returns PythonBridge

Set Signature

set py(pythonBridge): void

Parameters

ParameterType
pythonBridgePythonBridge

Returns void

Defined in generated/feature_extraction/FeatureHasher.ts:62

Methods

dispose()

dispose(): Promise<void>

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Returns Promise<void>

Defined in generated/feature_extraction/FeatureHasher.ts:114


fit()

fit(opts): Promise<any>

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.

Parameters

ParameterTypeDescription
optsobject-
opts.X?anyNot used, present here for API consistency by convention.
opts.y?anyNot used, present here for API consistency by convention.

Returns Promise<any>

Defined in generated/feature_extraction/FeatureHasher.ts:133


fit_transform()

fit_transform(opts): Promise<any[]>

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters

ParameterTypeDescription
optsobject-
opts.fit_params?anyAdditional fit parameters.
opts.X?ArrayLike[]Input samples.
opts.y?ArrayLikeTarget values (undefined for unsupervised transformations).

Returns Promise<any[]>

Defined in generated/feature_extraction/FeatureHasher.ts:172


get_metadata_routing()

get_metadata_routing(opts): Promise<any>

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Parameters

ParameterTypeDescription
optsobject-
opts.routing?anyA MetadataRequest encapsulating routing information.

Returns Promise<any>

Defined in generated/feature_extraction/FeatureHasher.ts:216


init()

init(py): Promise<void>

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Parameters

ParameterType
pyPythonBridge

Returns Promise<void>

Defined in generated/feature_extraction/FeatureHasher.ts:75


set_output()

set_output(opts): Promise<any>

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters

ParameterTypeDescription
optsobject-
opts.transform?"default" | "pandas" | "polars"Configure output of transform and fit_transform.

Returns Promise<any>

Defined in generated/feature_extraction/FeatureHasher.ts:252


set_transform_request()

set_transform_request(opts): Promise<any>

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Parameters

ParameterTypeDescription
optsobject-
opts.raw_X?string | booleanMetadata routing for raw_X parameter in transform.

Returns Promise<any>

Defined in generated/feature_extraction/FeatureHasher.ts:288


transform()

transform(opts): Promise<any[]>

Transform a sequence of instances to a scipy.sparse matrix.

Parameters

ParameterTypeDescription
optsobject-
opts.raw_X?anySamples. 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:322