8.8.2. sklearn.feature_extraction.FeatureHasher¶
- class sklearn.feature_extraction.FeatureHasher(n_features=1048576, input_type='dict', dtype=<type 'numpy.float64'>, non_negative=False)¶
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.
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.
Parameters: n_features : integer, optional
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.
dtype : NumPy type, optional
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.
input_type : string, optional
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).
non_negative : boolean, optional
Whether output matrices should contain non-negative values only; effectively calls abs on the matrix prior to returning it. When True, output values will be multinomially distributed. When False, output values will be normally distributed (Gaussian) with mean 0, assuming a good hash function.
Methods
fit([X, y]) No-op. fit_transform(X[, y]) Fit to data, then transform it get_params([deep]) Get parameters for the estimator set_params(**params) Set the parameters of the estimator. transform(raw_X[, y]) Transform a sequence of instances to a scipy.sparse matrix. - __init__(n_features=1048576, input_type='dict', dtype=<type 'numpy.float64'>, non_negative=False)¶
- fit(X=None, y=None)¶
No-op.
This method doesn’t do anything. It exists purely for compatibility with the scikit-learn transformer API.
Returns: self : FeatureHasher
- fit_transform(X, y=None, **fit_params)¶
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: X : numpy array of shape [n_samples, n_features]
Training set.
y : numpy array of shape [n_samples]
Target values.
Returns: X_new : numpy array of shape [n_samples, n_features_new]
Transformed array.
- get_params(deep=True)¶
Get parameters for the estimator
Parameters: deep: boolean, optional :
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- set_params(**params)¶
Set the parameters of the estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Returns: self :
- transform(raw_X, y=None)¶
Transform a sequence of instances to a scipy.sparse matrix.
Parameters: raw_X : iterable over iterable over raw features, length = n_samples
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.
y : (ignored)
Returns: X : scipy.sparse matrix, shape = (n_samples, self.n_features)
Feature matrix, for use with estimators or further transformers.