在看「XGBoost: A Scalable Tree Boosting System」论文时,里面提到 one-ho encoding 方法,其实就是常说的 bit-map。
There are multiple possible causes for sparsity: 1) presence of missing values in the data; 2) frequent zero entries in the statistics; and, 3) artifacts of feature engineering such as one-hot encoding.
有如下属性:
-
性别:["男", "女"]
-
喜欢的游戏: ["LOL", "Dota", "仙剑"]
-
职业:["程序员", "产品", "设计师"]
那么属性为男([1, 0]),喜欢 LOL([1, 0, 0]),程序员([1, 0, 0])的人,特征向量表示为:
1 0 1 0 0 1 0 0
sklean 示例
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
enc.fit([[0,0,3],[1,1,0],[0,2,1],[1,0,2]])
array = enc.transform([[0,1,3]]).toarray()
print array
第 1 维 第 2 维 第 3 维
[0, 0, 3]
[1, 1, 0]
[0, 2, 1]
[1, 0, 2]
enc.fit([[0,0,3],[1,1,0],[0,2,1],[1,0,2]]) 表示了三维特征,第一维最大为 1,第二维最大为 2,第一三维最大为 3,所以分别用二位,三位,四位 bit 来表示。
打印结果
[[ 1. 0. 0. 1. 0. 0. 0. 0. 1.]]
参考
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html