categorical feature 进行one hot的原因

categorical feature 进行one hot的原因

如上图,进行one hot之前,假如有以下数据

x=1,y=1

x=2,y=0

x=3,y=1

那么线性模型没有办法很好的拟合数据

----------------------------------------------------

进行one hot之后的数据如下:

x=(1,0,0), y=1

x=(0,1,0), y=0

x=(0,0,1), y=1

此时线性模型y = w1x1 + w2x2 + w3x3

w1 = 1,w2 = 0,w3 = 1恰好拟合数据。

也就是说,one hot是一种特征转换。

------------------------------------------------

一般使用树的模型是不需要one hot编码的,不过sklearn 中不支持categorical feature,只能先one hot