#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