星火分类数据编码 [英] Spark Categorical Data Encoding
本文介绍了星火分类数据编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有星火一个函数来做到分类数据编码。
例如:
VAR1,VAR2,VAR3
1,2,一
2,3,B
3,2,C
要
VAR1,VAR2,VAR3
1,2,0
2,3,1
3,2,2一个 - > 0,B-→1,C-→2
解决方案
使用此功能进行分类数据编码。
的Python
高清get_mapping(RDD,IDX):
返回rdd.map(拉姆达X:X [idx的])。不同()zipWithIndex()collectAsMap()
斯卡拉
VAL类别= rdd.map(R = GT; R(2))。distinct.collect.zipWithIndex.toMap
Is there a function in Spark to do Categorical data encoding. Ex:
Var1,Var2,Var3
1,2,a
2,3,b
3,2,c
To
var1,var2,var3
1,2,0
2,3,1
3,2,2
a -> 0, b->1, c->2
解决方案
Use this function for Categorical data encoding.
python
def get_mapping(rdd, idx):
return rdd.map(lambda x: x[idx]).distinct().zipWithIndex().collectAsMap()
Scala
val categories = rdd.map(r => r(2)).distinct.collect.zipWithIndex.toMap
这篇关于星火分类数据编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文