OneHotEncoder
- class sklearn.preprocessing.OneHotEncoder(*, categories='auto', drop=None, sparse_output=True, dtype=<class 'numpy.float64'>, handle_unknown='error', min_frequency, ,
Encode categorical 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 (depending on the sparse_output
parameter).
By default, the encoder derives the categories based on the unique values
in each feature. Alternatively, you can also specify the categories
manually.
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. For a comparison of different encoders, refer to: Comparing Target Encoder with Other Encoders.
Categories (unique values) per feature:
‘auto’ : Determine categories automatically from the training data.
list : categories[i] holds the categories expected in the ith
column. The passed categories should not mix strings and numeric
values within a single feature, and should be sorted in case of
numeric values.
The used categories can be found in the categories_ attribute.
Specifies a methodology to use to drop one of the categories per feature. This is useful in situations where perfectly collinear features cause problems, such as when feeding the resulting data into an unregularized linear regression model.
However, dropping one category breaks the symmetry of the original representation and can therefore induce a bias in downstream models, for instance for penalized linear classification or regression models.
None : retain all features (the default).
‘first’ : drop the first category in each feature. If only one category is present, the feature will be dropped entirely.
‘if_binary’ : drop the first category in each feature with two categories. Features with 1 or more than 2 categories are left intact.
array : drop[i] is the category in feature X[:, i] that
should be dropped.
When max_categories or min_frequency is configured to group
infrequent categories, the dropping behavior is handled after the
grouping.
When True, it returns a SciPy sparse matrix/array
in “Compressed Sparse Row” (CSR) format.
Desired dtype of output.
Specifies the way unknown categories are handled during transform.
‘error’ : Raise an error if an unknown category is present during transform.
‘ignore’ : When an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature will be all zeros. In the inverse transform, an unknown category will be denoted as None.
‘infrequent_if_exist’ : When an unknown category is encountered
during transform, the resulting one-hot encoded columns for this
feature will map to the infrequent category if it exists. The
infrequent category will be mapped to the last position in the
encoding. During inverse transform, an unknown category will be
mapped to the category denoted 'infrequent' if it exists. If the
'infrequent' category does not exist, then transform and
inverse_transform will handle an unknown category as with
. Infrequent categories exist based on
and . Read more in the
.
Specifies the minimum frequency below which a category will be considered infrequent.
If int, categories with a smaller cardinality will be considered
infrequent.
If float, categories with a smaller cardinality than
min_frequency * n_samples will be considered infrequent.
Specifies an upper limit to the number of output features for each input
feature when considering infrequent categories. If there are infrequent
categories, max_categories includes the category representing the
infrequent categories along with the frequent categories. If None,
there is no limit to the number of output features.
Callable with signature def callable(input_feature, category) that returns a
string. This is used to create feature names to be returned by
get_feature_names_out.
"concat" concatenates encoded feature name and category with
feature + "_" + str(category).E.g. feature X with values 1, 6, 7 create
feature names X_1, X_6, X_7.
The categories of each feature determined during fitting
(in order of the features in X and corresponding with the output
of transform). This includes the category specified in drop
(if any).
drop_idx_[i] is the index in categories_[i] of the category
to be dropped for each feature.
drop_idx_[i] = None if no category is to be dropped from the
feature with index i, e.g. when and the
feature isn’t binary.
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
One can discard categories not seen during fit:
>>> enc = OneHotEncoder(handle_unknown='ignore')
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OneHotEncoder(handle_unknown='ignore')
>>> enc
One can always drop the first column for each feature:
>>> drop_enc = OneHotEncoder(drop='first').fit(X)
>>> drop_enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> drop_enc.transform([['Female', 1], ['Male', 2]]).toarray()
Or drop a column for feature only having 2 categories:
>>> drop_binary_enc = OneHotEncoder(drop='if_binary').fit(X)
>>> drop_binary_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 1., 0., 0.],
[1., 0., 1., 0.]])
One can change the way feature names are created.
>>> def custom_combiner(feature, category):
... return str(feature) + "_" + type(category).__name__ + "_" + str(category)
>>> custom_fnames_enc = OneHotEncoder(feature_name_combiner
Infrequent categories are enabled by setting max_categories or min_frequency.
>>> import numpy as np
>>> X = np.array([["a"] * 5 + ["b"] * 20 + ["c"] * 10 + ["d"] * 3], dtype
Fit OneHotEncoder to X.
The data to determine the categories of each feature.
Ignored. This parameter exists only for compatibility with
Pipeline.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Get output feature names for transformation.
Input features.
If input_features is None, then feature_names_in_ is
used as feature names in. If is not defined,
then the following input feature names are generated:
.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Convert the data back to the original representation.
When unknown categories are encountered (all zeros in the
one-hot encoding), None is used to represent this category. If the
feature with the unknown category has a dropped category, the dropped
category will be its inverse.
For a given input feature, if there is an infrequent category, ‘infrequent_sklearn’ will be used to represent the infrequent category.
The transformed data.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Transform X using one-hot encoding.
If sparse_output=True (default), it returns a SciPy sparse in CSR format.
If there are infrequent categories for a feature, set by specifying
max_categories or min_frequency, the infrequent categories are
grouped into a single category.
The data to encode.
Common pitfalls in the interpretation of coefficients of linear models
Partial Dependence and Individual Conditional Expectation Plots
Added in version 0.21: The parameter drop was added in 0.21.
handle_unknown='ignore'min_frequencymax_categories‘warn’ : When an unknown category is encountered during transform
a warning is issued, and the encoding then proceeds as described for
handle_unknown="infrequent_if_exist".
drop_idx_ = None if all the transformed features will be
retained.
If infrequent categories are enabled by setting min_frequency or
max_categories to a non-default value and drop_idx[i] corresponds
to an infrequent category, then the entire infrequent category is
dropped.
infrequent_categories_ : list of ndarrayInfrequent categories for each feature.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Callable with signature def callable(input_feature, category) that returns a
string. This is used to create feature names to be returned by
get_feature_names_out.
sklearn.feature_extraction.FeatureHasherPerforms an approximate one-hot encoding of dictionary items or strings.
Binarizes labels in a one-vs-all fashion.
Transforms between iterable of iterables and a multilabel format, e.g. a (samples x classes) binary matrix indicating the presence of a class label.
Fitted encoder.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Transformed array.
["x0", "x1", ..., "x(n_features_in_ - 1)"]If input_features is an array-like, then input_features must
match feature_names_in_ if feature_names_in_ is defined.
Transformed feature names.
Inverse transformed array.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
Transformed input. If sparse_output=True, a sparse matrix will be
returned.