機器學習sklearn(十九): 特征工程(十)特征編碼(四)類別特征編碼(二)標簽編碼 OrdinalEncoder


在機器學習中,特征經常不是連續的數值型的而是標稱型的(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.

dtype number 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. In inverse_transform, an unknown category will be denoted as None.

New in version 0.24.

unknown_value int 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, the dtype 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 of transform). This does not include categories that weren’t seen during fit.

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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM