8.26.7. sklearn.preprocessing.OneHotEncoder

class sklearn.preprocessing.OneHotEncoder(n_values='auto', dtype=<type 'float'>)

Encode categorical integer features using a one-hot aka one-of-K scheme.

The input to this transformer should be a matrix of integers, denoting the values taken on by categorical (discrete) features. The output will be a sparse matrix were each column corresponds to one possible value of one feature. It is assumed that input features take on values in the range [0, n_values).

This encoding is needed for feeding categorical data to scikit-learn estimators.

Parameters :

n_values : ‘auto’, int or array of int

Number of values per feature. ‘auto’ : determine value range from training data. int : maximum value for all features. array : maximum value per feature.

dtype : number type, default=np.float

Desired dtype of output.

See also

LabelEncoder
performs a one-hot encoding on arbitrary class labels.
sklearn.feature_extraction.DictVectorizer
performs a one-hot encoding of dictionary items (also handles string-valued features).

Examples

Given a dataset with three features and two samples, we let the encoder find the maximum value per feature and transform the data to a binary one-hot encoding.

>>> from sklearn.preprocessing import OneHotEncoder
>>> enc = OneHotEncoder()
>>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
OneHotEncoder(dtype=<type 'float'>, n_values='auto')
>>> enc.n_values_
array([2, 3, 4])
>>> enc.feature_indices_
array([0, 2, 5, 9])
>>> enc.transform([[0, 1, 1]]).toarray()
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.]])

Attributes

active_features_ array Indices for active features, meaning values that actually occur in the training set. Only available when n_values is 'auto'.
feature_indices_ array of shape (n_features,) Indices to feature ranges. Feature i in the original data is mapped to features from feature_indices_[i] to feature_indices_[i+1] (and then potentially masked by active_features_ afterwards)
n_values_ array of shape (n_features,) Maximum number of values per feature.

Methods

fit
fit_transform
get_params
set_params
transform
__init__(n_values='auto', dtype=<type 'float'>)
fit(X, y=None)

Fit OneHotEncoder to X.

Parameters :

X : array-like, shape=(n_samples, n_feature)

Input array of type int.

Returns :

self :

fit_transform(X, y=None)

Fit OneHotEncoder to X, then transform X.

Equivalent to self.fit(X).transform(X), but more convenient and more efficient. See fit for the parameters, transform for the return value.

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(X)

Transform X using one-hot encoding.

Parameters :

X : array-like, shape=(n_samples, feature_indices_[-1])

Input array of type int.

Returns :

X_out : sparse matrix, dtype=int

Transformed input.

Previous
Next