在进行回归分析时,我试图完全理解分类数据和序数数据之间的差异。目前,已经明确的是:
分类特征和数据示例:
颜色: 红,白,黑
为什么分类:red < white < black
逻辑上是不正确
序数特征和数据示例:
状况: 旧的、翻新的、新的
为什么序数:old < renovated < new
逻辑上是correct
类别到数字和序数到数字的编码方法:
分类数据的 One-Hot 编码
序数数据的任意数字
分类示例:
data = {'color': ['blue', 'green', 'green', 'red']}
One-Hot编码后的数字格式:
color_blue color_green color_red
0 1 0 0
1 0 1 0
2 0 1 0
3 0 0 1
序数示例:
data = {'con': ['old', 'new', 'new', 'renovated']}
使用映射后的数字格式:旧
0 0
1 2
2 2
3 1
根据我的数据,随着条件从“旧”变为“新”,价格会上涨。数字中的“旧”被编码为“0”。数字中的“新”被编码为“2”。因此,随着状况的改善,价格也会上涨。正确的。
现在让我们看看“颜色”功能。就我而言,不同的颜色也会影响价格。例如,“黑色”会比“白色”更贵。但从上面提到的分类数据的数字表示来看,我没有看到像“条件”功能那样日益增加的依赖性。这是否意味着如果使用 one-hot 编码,颜色的变化不会影响回归模型中的价格?如果无论如何都不影响价格,为什么要使用 one-hot 编码进行回归呢?你能澄清一下吗?
UPDATE TO QUESTION:
First I introduce formula for linear regression:
Let have a look at data representations for color:
Let's predict price for 1-st and 2-nd item using formula for both data representations:
One-hot encoding:
In this case different thetas for different colors will exist and prediction will be:
Price (1 item) = 0 + 20*1 + 50*0 + 100*0 = 20$ (thetas are assumed for example)
Price (2 item) = 0 + 20*0 + 50*1 + 100*0 = 50$ (thetas are assumed for example)
颜色的序数编码:在这种情况下,所有颜色都有共同的 theta,但乘数不同:
Price (1 item) = 0 + 20*10 = 200$ (theta assumed for example)
Price (2 item) = 0 + 20*20 = 400$ (theta assumed for example)
在我的模型中,价格为白色