6.9 轉換預測目標(y)?
這些轉換器不打算用于特征,而只能用于有監督的學習目標。如果要轉換預測目標以進行學習,但請在原始(未轉換)空間中評估模型,另請參見在回歸中轉換目標。
6.9.1 標簽二值化
LabelBinarizer
是一個實用程序類,可從多類標簽列表中創建標簽指示矩陣:
>>> 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]])
對于多標簽實例,請使用MultiLabelBinarizer
:
>>> lb = preprocessing.MultiLabelBinarizer()
>>> lb.fit_transform([(1, 2), (3,)])
array([[1, 1, 0],
[0, 0, 1]])
>>> lb.classes_
array([1, 2, 3])
6.9.2 標簽編碼
LabelEncoder
是一個實用程序類,可幫助標準化標簽,使其僅包含0到n_classes-1之間的值。有時對于編寫有效的Cython例程很有用。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])
它也可以用于將非數字標簽(只要它們是可哈希的和可比較的)轉換為數字標簽:
>>> 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']