在机器学习中,特征经常不是连续的数值型的而是标称型的(categorical)。举个示例,一个人的样本具有特征["male", "female"]
, ["from Europe", "from US", "from Asia"]
, ["uses Firefox", "uses Chrome", "uses Safari", "uses Internet Explorer"]
等。 这些特征能够被有效地编码成整数,比如 ["male", "from US", "uses Internet Explorer"]
可以被表示为 [0, 1, 3]
,["female", "from Asia", "uses Chrome"]
表示为 [1, 2, 1]
。
要把标称型特征(categorical features) 转换为这样的整数编码(integer codes), 我们可以使用 OrdinalEncoder 。 这个估计器把每一个categorical feature变换成 一个新的整数数字特征 (0 到 n_categories - 1):
>>> enc = preprocessing.OrdinalEncoder() >>> X = [['male', 'from US', 'uses Safari'], ['female', 'from Europe', 'uses Firefox']] >>> enc.fit(X) OrdinalEncoder(categories='auto', dtype=<... 'numpy.float64'>) >>> enc.transform([['female', 'from US', 'uses Safari']]) array([[0., 1., 1.]])
这样的整数特征表示并不能在scikit-learn的估计器中直接使用,因为这样的连续输入,估计器会认为类别之间是有序的,但实际却是无序的。(例如:浏览器的类别数据是任意排序的)。
class sklearn.preprocessing.
OrdinalEncoder
(*, categories='auto', dtype=<class 'numpy.float64'>, handle_unknown='error', unknown_value=None)
Encode categorical features as an integer 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 converted to ordinal integers. This results in a single column of integers (0 to n_categories - 1) per feature.
Read more in the User Guide.
New in version 0.20.
- Parameters
- categories‘auto’ or a list of array-like, default=’auto’
-
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, and should be sorted in case of numeric values.
The used categories can be found in the
categories_
attribute. -
- dtypenumber type, default np.float64
-
Desired dtype of output.
- handle_unknown{‘error’, ‘use_encoded_value’}, default=’error’
-
When set to ‘error’ an error will be raised in case an unknown categorical feature is present during transform. When set to ‘use_encoded_value’, the encoded value of unknown categories will be set to the value given for the parameter
unknown_value
. Ininverse_transform
, an unknown category will be denoted as None.New in version 0.24.
- unknown_valueint or np.nan, default=None
-
When the parameter handle_unknown is set to ‘use_encoded_value’, this parameter is required and will set the encoded value of unknown categories. It has to be distinct from the values used to encode any of the categories in
fit
. If set to np.nan, thedtype
parameter must be a float dtype.New in version 0.24.
- Attributes
- categories_list of arrays
-
The categories of each feature determined during
fit
(in order of the features in X and corresponding with the output oftransform
). This does not include categories that weren’t seen duringfit
.
Examples
Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to an ordinal encoding.
>>> from sklearn.preprocessing import OrdinalEncoder >>> enc = OrdinalEncoder() >>> X = [['Male', 1], ['Female', 3], ['Female', 2]] >>> enc.fit(X) OrdinalEncoder() >>> enc.categories_ [array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)] >>> enc.transform([['Female', 3], ['Male', 1]]) array([[0., 2.], [1., 0.]])
>>> enc.inverse_transform([[1, 0], [0, 1]]) array([['Male', 1], ['Female', 2]], dtype=object)