sklearn.feature_extraction.
DictVectorizer
(
dtype=<type 'numpy.float64'>,
separator='=',
sparse=True,
sort=True
)
[source]
Transforms lists of feature-value mappings to vectors.
This transformer turns lists of mappings (dict-like objects) of feature names to feature values into Numpy arrays or scipy.sparse matrices for use with scikit-learn estimators.
When feature values are strings, this transformer will do a binary one-hot (aka one-of-K) coding: one boolean-valued feature is constructed for each of the possible string values that the feature can take on. For instance, a feature “f” that can take on the values “ham” and “spam” will become two features in the output, one signifying “f=ham”, the other “f=spam”.
However, note that this transformer will only do a binary one-hot encoding when feature values are of type string. If categorical features are represented as numeric values such as int, the DictVectorizer can be followed by OneHotEncoder to complete binary one-hot encoding.
Features that do not occur in a sample (mapping) will have a zero value in the resulting array/matrix.
Read more in the User Guide.
See also
- performs vectorization using only a hash function.
- handles nominal/categorical features encoded as columns of integers.
FeatureHasher
sklearn.preprocessing.OneHotEncoder
Examples
Methods
__init__
(
dtype=<type 'numpy.float64'>,
separator='=',
sparse=True,
sort=True
)
[source]
-
Learn a list of feature name -> indices mappings.
fit
(
X,
y=None
)
[source]
-
Learn a list of feature name -> indices mappings and transform X.
Like fit(X) followed by transform(X), but does not require materializing X in memory.
fit_transform
(
X,
y=None
)
[source]
-
Returns a list of feature names, ordered by their indices.
If one-of-K coding is applied to categorical features, this will include the constructed feature names but not the original ones.
get_feature_names
(
)
[source]
-
Get parameters for this estimator.
get_params
(
deep=True
)
[source]
-
Transform array or sparse matrix X back to feature mappings.
X must have been produced by this DictVectorizer’s transform or fit_transform method; it may only have passed through transformers that preserve the number of features and their order.
In the case of one-hot/one-of-K coding, the constructed feature names and values are returned rather than the original ones.
inverse_transform
(
X,
dict_type=<type 'dict'>
)
[source]
-
Restrict the features to those in support using feature selection.
This function modifies the estimator in-place.
Examples
restrict
(
support,
indices=False
)
[source]
-
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.
set_params
(
**params
)
[source]
-
Transform feature->value dicts to array or sparse matrix.
Named features not encountered during fit or fit_transform will be silently ignored.
transform
(
X,
y=None
)
[source]