DictVectorizer
¶
-
class
ibex.sklearn.feature_extraction.
DictVectorizer
(dtype=<class 'numpy.float64'>, separator='=', sparse=True, sort=True)¶ Bases:
sklearn.feature_extraction.dict_vectorizer.DictVectorizer
,ibex._base.FrameMixin
Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
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.
- dtype : callable, optional
- The type of feature values. Passed to Numpy array/scipy.sparse matrix constructors as the dtype argument.
- separator : string, optional
- Separator string used when constructing new features for one-hot coding.
- sparse : boolean, optional.
- Whether transform should produce scipy.sparse matrices. True by default.
- sort : boolean, optional.
- Whether
feature_names_
andvocabulary_
should be sorted when fitting. True by default.
- vocabulary_ : dict
- A dictionary mapping feature names to feature indices.
- feature_names_ : list
- A list of length n_features containing the feature names (e.g., “f=ham” and “f=spam”).
>>> from sklearn.feature_extraction import DictVectorizer >>> v = DictVectorizer(sparse=False) >>> D = [{'foo': 1, 'bar': 2}, {'foo': 3, 'baz': 1}] >>> X = v.fit_transform(D) >>> X array([[ 2., 0., 1.], [ 0., 1., 3.]]) >>> v.inverse_transform(X) == [{'bar': 2.0, 'foo': 1.0}, {'baz': 1.0, 'foo': 3.0}] True >>> v.transform({'foo': 4, 'unseen_feature': 3}) array([[ 0., 0., 4.]])
FeatureHasher : performs vectorization using only a hash function. sklearn.preprocessing.OneHotEncoder : handles nominal/categorical features
encoded as columns of integers.-
fit
(X, y=None)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Learn a list of feature name -> indices mappings.
- X : Mapping or iterable over Mappings
- Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).
y : (ignored)
self
- A parameter
-
fit_transform
(X, y=None)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
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.
- X : Mapping or iterable over Mappings
- Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).
y : (ignored)
- Xa : {array, sparse matrix}
- Feature vectors; always 2-d.
- A parameter
-
inverse_transform
(X, dict_type=<class 'dict'>)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
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.
- X : {array-like, sparse matrix}, shape = [n_samples, n_features]
- Sample matrix.
- dict_type : callable, optional
- Constructor for feature mappings. Must conform to the collections.Mapping API.
- D : list of dict_type objects, length = n_samples
- Feature mappings for the samples in X.
- A parameter
-
transform
(X)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Transform feature->value dicts to array or sparse matrix.
Named features not encountered during fit or fit_transform will be silently ignored.
- X : Mapping or iterable over Mappings, length = n_samples
- Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).
- Xa : {array, sparse matrix}
- Feature vectors; always 2-d.
- A parameter
- A parameter