class sklearn.preprocessing.OneHotEncoder(n_values=None, categorical_features=None, categories=None, sparse=True, dtype=<class ‘numpy.float64’>, handle_unknown=’error’)
[source]
Encode categorical integer features as a one-hot numeric array.
The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are encoded using a one-hot (aka ‘one-of-K’ or ‘dummy’) encoding scheme. This creates a binary column for each category and returns a sparse matrix or dense array.
By default, the encoder derives the categories based on the unique values in each feature. Alternatively, you can also specify the categories
manually. The OneHotEncoder previously assumed that the input features take on values in the range [0, max(values)). This behaviour is deprecated.
This encoding is needed for feeding categorical data to many scikit-learn estimators, notably linear models and SVMs with the standard kernels.
Note: a one-hot encoding of y labels should use a LabelBinarizer instead.
Read more in the User Guide.
Parameters: |
|
---|---|
Attributes: |
|
See also
sklearn.preprocessing.OrdinalEncoder
sklearn.feature_extraction.DictVectorizer
sklearn.feature_extraction.FeatureHasher
sklearn.preprocessing.LabelBinarizer
sklearn.preprocessing.MultiLabelBinarizer
Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding.
>>> from sklearn.preprocessing import OneHotEncoder >>> enc = OneHotEncoder(handle_unknown='ignore') >>> X = [['Male', 1], ['Female', 3], ['Female', 2]] >>> enc.fit(X) ... OneHotEncoder(categorical_features=None, categories=None, dtype=<... 'numpy.float64'>, handle_unknown='ignore', n_values=None, sparse=True)
>>> enc.categories_ [array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)] >>> enc.transform([['Female', 1], ['Male', 4]]).toarray() array([[1., 0., 1., 0., 0.], [0., 1., 0., 0., 0.]]) >>> enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]]) array([['Male', 1], [None, 2]], dtype=object) >>> enc.get_feature_names() array(['x0_Female', 'x0_Male', 'x1_1', 'x1_2', 'x1_3'], dtype=object)
fit (X[, y]) | Fit OneHotEncoder to X. |
fit_transform (X[, y]) | Fit OneHotEncoder to X, then transform X. |
get_feature_names ([input_features]) | Return feature names for output features. |
get_params ([deep]) | Get parameters for this estimator. |
inverse_transform (X) | Convert the back data to the original representation. |
set_params (**params) | Set the parameters of this estimator. |
transform (X) | Transform X using one-hot encoding. |
__init__(n_values=None, categorical_features=None, categories=None, sparse=True, dtype=<class ‘numpy.float64’>, handle_unknown=’error’)
[source]
active_features_
DEPRECATED: The active_features_
attribute was deprecated in version 0.20 and will be removed 0.22.
feature_indices_
DEPRECATED: The feature_indices_
attribute was deprecated in version 0.20 and will be removed 0.22.
fit(X, y=None)
[source]
Fit OneHotEncoder to X.
Parameters: |
|
---|---|
Returns: |
|
fit_transform(X, y=None)
[source]
Fit OneHotEncoder to X, then transform X.
Equivalent to fit(X).transform(X) but more convenient.
Parameters: |
|
---|---|
Returns: |
|
get_feature_names(input_features=None)
[source]
Return feature names for output features.
Parameters: |
|
---|---|
Returns: |
|
get_params(deep=True)
[source]
Get parameters for this estimator.
Parameters: |
|
---|---|
Returns: |
|
inverse_transform(X)
[source]
Convert the back data to the original representation.
In case unknown categories are encountered (all zero’s in the one-hot encoding), None
is used to represent this category.
Parameters: |
|
---|---|
Returns: |
|
n_values_
DEPRECATED: The n_values_
attribute was deprecated in version 0.20 and will be removed 0.22.
set_params(**params)
[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.
Returns: |
|
---|
transform(X)
[source]
Transform X using one-hot encoding.
Parameters: |
|
---|---|
Returns: |
|
sklearn.preprocessing.OneHotEncoder
© 2007–2018 The scikit-learn developers
Licensed under the 3-clause BSD License.
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html