將 muti-class labels 轉換成 binary labels
fit 後可得到3個參數
classes_ 列出分類
y_type_ 物件型態
sparse_input_ 不確定
傳入一維字串
['a' , 'b' , 'c']
=>
[[1,0,0]
,[0,1,0]
,[0,0,1]]
傳入二維陣列
二維無法傳入字串
也須使用 np.array
np.array([[0,0,0,0,0]])
將等效於
[0,1,2,3,4]
code
import numpy as npout
import pandas as pd
from sklearn.preprocessing import LabelBinarizer
data = ['a', 'h', 'io', 'po']
lb = LabelBinarizer(sparse_output=False)
lb.fit(data)
print("LabelBinarizer.classes_ \n" , lb.classes_)
print("LabelBinarizer.y_type_ \n" , lb.y_type_)
print("LabelBinarizer.sparse_input_ \n" , lb.sparse_input_)
print("LabelBinarizer : \n" , lb.fit_transform(data))
muldata = np.array([[0,0,0,0,0]])
lb = LabelBinarizer(sparse_output=False)
lb.fit(muldata)
print("LabelBinarizer.classes_ \n" , lb.classes_)
print("LabelBinarizer.y_type_ \n" , lb.y_type_)
print("LabelBinarizer.sparse_input_ \n" , lb.sparse_input_)
print("LabelBinarizer : \n" , lb.transform([0, 1, 2, 1,3,4]))
LabelBinarizer.classes_
['a' 'h' 'io' 'po']
LabelBinarizer.y_type_
multiclass
LabelBinarizer.sparse_input_
False
LabelBinarizer :
[[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]]
LabelBinarizer.classes_
[0 1 2 3 4]
LabelBinarizer.y_type_
multilabel-indicator
LabelBinarizer.sparse_input_
False
LabelBinarizer :
[[1 0 0 0 0]
[0 1 0 0 0]
[0 0 1 0 0]
[0 1 0 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]
source code
沒有留言:
張貼留言