独热向量编码/One-Hot-Encoding (Dummy variables)
颜色:红、黄、紫[1,0,0] [0,1,0] [0,0,1] LR = theta*X
红色 蓝色 黄色 紫色 咖啡色 白色… => 红色 蓝色 黄色 rare
sklearn OneHotEncoder;pandas get_dummies
# create a dataframe with an integer feature and a categorical string feature
import pandas as pd
demo_df = pd.DataFrame({'Integer Feature': [0, 1, 2, 1], 'Categorical Feature': ['socks', 'fox', 'socks', 'box']})
demo_df
- 1
- 2
- 3
- 4
pd.get_dummies(demo_df) #get_dummies对“整数特征”无变化,对“类别特征”one-hot编码
- 1
demo_df['Integer Feature'] = demo_df['Integer Feature'].astype(str)
pd.get_dummies(demo_df) #将“整数特征”变成“字符型类别”进行one-hot编码
- 1
- 2