sklearn.preprocessing.LabelEncoder?
class sklearn.preprocessing.LabelEncoder
對目標標簽進行編碼,其值介于0和n_classes-1之間。
該轉換器應用于編碼目標值,即y,而不是輸入X。
在用戶指南中閱讀更多內容
版本0.12中的新功能。
屬性 | 說明 |
---|---|
classes_ | array of shape (n_class,) 持有每個類別的標簽。 |
另見:
sklearn.preprocessing.OrdinalEncoder
使用序數編碼方案對分類特征進行編碼。
sklearn.preprocessing.OneHotEncoder
將分類特征編碼為一個one-hot 數字數組。
示例:
LabelEncoder可用于標準化標簽。
>>> from sklearn import preprocessing
>>> le = preprocessing.LabelEncoder()
>>> le.fit([1, 2, 2, 6])
LabelEncoder()
>>> le.classes_
array([1, 2, 6])
>>> le.transform([1, 1, 2, 6])
array([0, 0, 1, 2]...)
>>> le.inverse_transform([0, 0, 1, 2])
array([1, 1, 2, 6])
它也可以用于將非數字標簽(只要它們是hashable和可比較的)轉換為數字標簽。
>>> le = preprocessing.LabelEncoder()
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
>>> list(le.classes_)
['amsterdam', 'paris', 'tokyo']
>>> le.transform(["tokyo", "tokyo", "paris"])
array([2, 2, 1]...)
>>> list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']
方法
fit (self, y) |
適合標簽編碼器 |
---|---|
fit_transform (self, y) |
適合標簽編碼器并返回編碼的標簽 |
get_params (self[, deep]) |
獲取此估計量的參數。 |
inverse_transform (self, y) |
將標簽轉換回原始編碼。 |
set_params (self, **params) |
設置此估算器的參數。 |
transform (self, y) |
將標簽轉換為標準化的編碼。 |
__init__(self, /, *args, **kwargs)
初始化self。有關準確的簽名,請參見help(type(self))。
fit(self, y)
適合標簽編碼器
參數 | 說明 |
---|---|
y | array-like of shape (n_samples,) 目標值。 |
返回值 | 說明 |
---|---|
self | returns an instance of self. |
fit_transform(self, y)
適合標簽編碼器并返回編碼的標簽
參數 | 說明 |
---|---|
y | array-like of shape (n_samples,) 目標值。 |
返回值 | 說明 |
---|---|
self | array-like of shape [n_samples] |
get_params(self, deep=True)
獲取此估計量的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
inverse_transform(self, y)
將標簽轉換回原始編碼。
參數 | 說明 |
---|---|
y | numpy array of shape [n_samples] 目標值。 |
返回值 | 說明 |
---|---|
y | numpy array of shape [n_samples] 參數名稱映射到其值。 |
set_params(self, **params)
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者的參數形式為<component>__<parameter>這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算器參數。 |
返回值 | 說明 |
---|---|
self | object 估算器實例。 |
transform(self, y)
參數 | 說明 |
---|---|
y | array-like of shape [n_samples] 目標值。 |
返回值 | 說明 |
---|---|
y | array-like of shape [n_samples] |