sklearn.preprocessing.LabelBinarizer?
class sklearn.preprocessing.LabelBinarizer(*, neg_label=0, pos_label=1, sparse_output=False)
以一對一的方式對標簽進行二值化
scikit-learn中提供了幾種回歸和二進制分類算法。將這些算法擴展到多類分類的一種簡單方法是使用所謂的“一對多”方案。
在學習時,這僅僅是在每個類中學習一個回歸器或二進制分類器。為此,需要將多類標簽轉換為二進制標簽(屬于或不屬于該類)。LabelBinarizer使用轉換方法使此過程變得簡單。
在預測時,分配一個類別,相應的模型為其提供最大的信心。LabelBinarizer使用“逆變換”方法使這一點變得簡單。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
neg_label | int (default: 0) 負標簽必須編碼的值。 |
pos_label | int (default: 1) 必須對正標簽進行編碼的值。 |
sparse_output | boolean (default: False) 如果希望從轉換返回的數組為稀疏CSR格式,則為true。 |
屬性 | 說明 |
---|---|
classes_ | array of shape [n_class] 持有每個類別的標簽。 |
y_type_ | str, 表示由utils.multiclass.type_of_target評估的目標數據的類型。可能的類型是“連續”,“連續多輸出”,“二進制”,“多類”,“多類多輸出”,“多標簽指示符”和“未知”。 |
sparse_input_ | boolean, 如果要轉換的輸入數據是稀疏矩陣,則為True,否則為False。 |
另見:
函數以固定的類執行LabelBinarizer的轉換操作。
sklearn.preprocessing.OneHotEncoder
分類特征使用one hot aka-of-K方案進行編碼。
示例
>>> from sklearn import preprocessing
>>> lb = preprocessing.LabelBinarizer()
>>> lb.fit([1, 2, 6, 4, 2])
LabelBinarizer()
>>> lb.classes_
array([1, 2, 4, 6])
>>> lb.transform([1, 6])
array([[1, 0, 0, 0],
[0, 0, 0, 1]])
二元目標轉換為列向量
>>> lb = preprocessing.LabelBinarizer()
>>> lb.fit_transform(['yes', 'no', 'no', 'yes'])
array([[1],
[0],
[0],
[1]])
傳遞2D矩陣進行多標簽分類
>>> import numpy as np
>>> lb.fit(np.array([[0, 1, 1], [1, 0, 0]]))
LabelBinarizer()
>>> lb.classes_
array([0, 1, 2])
>>> lb.transform([0, 1, 2, 1])
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 1, 0]])
方法
方法 | 說明 |
---|---|
fit (self, y) |
將標簽調整為二進制 |
fit_transform (self, y) |
安裝標簽二值化器并將多類標簽轉換為二進制標簽。 |
get_params (self[, deep]) |
獲取此估計量的參數。 |
inverse_transform (self, Y[, threshold]) |
將二進制標簽轉換回多類標簽 |
set_params (self, **params) |
設置此估算器的參數。 |
transform (self, y) |
將多類標簽轉換為二進制標簽 |
__init__(self, *, neg_label=0, pos_label=1, sparse_output=False)
初始化self,有關準確的簽名,請參見help(type(self))。
fit(self, y)
適合標簽二值化器
參數 | 說明 |
---|---|
y | array of shape [n_samples,] or [n_samples, n_classes] 目標值。二維矩陣只能包含0和1,代表多標簽分類。 |
返回值 | 說明 |
---|---|
self | returns an instance of self. |
fit_transform(self, y)
適合標簽二值化器并將多類標簽轉換為二進制標簽。
變換的輸出有時被稱為1-of-K編碼方案。
參數 | 說明 |
---|---|
y | array or sparse matrix of shape [n_samples,] or [n_samples, n_classes] 目標值。二維矩陣只能包含0和1,代表多標簽分類。稀疏矩陣可以是CSR,CSC,COO,DOK或LIL。 |
返回值 | 說明 |
---|---|
Y | array or CSR matrix of shape [n_samples, n_classes] 對于二進制問題,形狀將為[n_samples,1]。 |
get_params(self, deep=True)
獲取此估計量的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
inverse_transform(self, Y, threshold=None)
將二進制標簽轉換回多類標簽
參數 | 說明 |
---|---|
Y | numpy array or sparse matrix with shape [n_samples, n_classes] 目標值。在逆變換之前,所有稀疏矩陣都將轉換為CSR。 |
threshold | float or None 二進制和多標簽情況下使用的閾值。 當Y包含Decision_function(分類器)的輸出時,請使用0。當Y包含predict_proba的輸出時,使用0.5。 如果為None,則閾值假定為介于neg_label和pos_label之間。 |
返回值 | 說明 |
---|---|
y | numpy array or CSR matrix of shape [n_samples] Target values. |
注釋
在二進制標簽為小數(概率)的情況下,inverse_transform選擇具有最大值的類。
通常,這允許將線性模型的Decision_function方法的輸出直接用作inverse_transform的輸入。
set_params(self, **params)
[源碼] 設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者的參數形式為<component>__<parameter>這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算器參數。 |
返回值 | 說明 |
---|---|
self | object 估算器實例。 |
transform(self, y)
將多類標簽轉換為二進制標簽
變換的輸出有時被一些作者稱為1-of-K編碼方案。
參數 | 說明 |
---|---|
y | array or sparse matrix of shape [n_samples,] or [n_samples, n_classes] 目標值。二維矩陣只能包含0和1,代表多標簽分類。稀疏矩陣可以是CSR,CSC,COO,DOK或LIL。 |
返回值 | 說明 |
---|---|
Y | numpy array or CSR matrix of shape [n_samples, n_classes] 對于二進制問題,形狀將為[n_samples,1]。 |