獨熱編碼 OneHotEncoder()

#coding=utf-8
__author__ = 'acer'
from sklearn import preprocessing

enc = preprocessing.OneHotEncoder()
enc.fit([[0,0,3],[1,1,0],[0,2,1],[1,0,2]])#[[ 1.  0.  0.  1.  0.  0.  0.  0.  1.]]
#enc.fit([[0,0,3],[1,1,0]])#[[ 1.  0.  0.  1.  0.  1.]]
enc.fit([[0,0,0],[1,1,1],[0,2,2],[0,0,3],[1,2,2]])# 這個是樣本矩陣,第一個特徵有2個取值,
第二個有3,第三個有4,所有至少是4個樣本
array = enc.transform([[0,1,3]]).toarray() #[[ 1.  0.  0.  1.  0.  0.  0.  0.  1.]]

print array
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章