我想散列功能'流派'分为6列和单独功能'出版商'到另外6列。我想要下面这样的东西:
Genre Publisher 0 1 2 3 4 5 0 1 2 3 4 5
0 Platform Nintendo 0.0 2.0 2.0 -1.0 1.0 0.0 0.0 2.0 2.0 -1.0 1.0 0.0
1 Racing Noir -1.0 0.0 0.0 0.0 0.0 -1.0 -1.0 0.0 0.0 0.0 0.0 -1.0
2 Sports Laura -2.0 2.0 0.0 -2.0 0.0 0.0 -2.0 2.0 0.0 -2.0 0.0 0.0
3 Roleplaying John -2.0 2.0 2.0 0.0 1.0 0.0 -2.0 2.0 2.0 0.0 1.0 0.0
4 Puzzle John 0.0 1.0 1.0 -2.0 1.0 -1.0 0.0 1.0 1.0 -2.0 1.0 -1.0
5 Platform Noir 0.0 2.0 2.0 -1.0 1.0 0.0 0.0 2.0 2.0 -1.0 1.0 0.0
下面的代码可以执行我想做的事情
^{pr2}$这适用于上面两个特性,但是如果我有40个分类特性,那么这个方法将是乏味的。还有别的办法吗?在
哈希(更新)
假设新的类别可能会出现在一些特性中,散列是一种方法。只需2个注意事项:
一个热点向量
如果每个特性的类别数量是固定的且不太大,则使用一个热编码。在
我建议使用以下两种方法之一:
sklearn.preprocessing.OneHotEncoder
pandas.get_dummies
示例
^{pr2}$res_0
:res_1
:res_2
:相关问题 更多 >
编程相关推荐