简体   繁体   English

Spark - StringIndexer 与 OneHotEncoderEstimator

[英]Spark - StringIndexer Vs OneHotEncoderEstimator

I am learning Spark and I have below code in one of the tutorial.我正在学习 Spark,并且在其中一个教程中有以下代码。 I understand the dataframe is one hot encoded in below code but what I don't understand is why StringIndexer is used?我知道数据帧是下面代码中的一种热编码,但我不明白的是为什么使用 StringIndexer? Is StringIndexer should be used in conjunction with OneHotEncoderEstimator? StringIndexer 是否应该与 OneHotEncoderEstimator 结合使用? val si = new StringIndexer() .setHandleInvalid("keep") .setInputCol(ProcuctTypeCol) .setOutputCol(ProcuctTypeSIOutCol) val si = new StringIndexer() .setHandleInvalid("keep") .setInputCol(ProcuctTypeCol) .setOutputCol(ProcuctTypeSIOutCol)

val ohe = new OneHotEncoderEstimator()
      .setHandleInvalid("keep")
      .setInputCols(Array(si.getOutputCol))
      .setOutputCols(Array(ProductTypeOHEOutCol))

val pipeline = new Pipeline()
  .setStages(Array(si, ohe))

Thanks谢谢

SI transform the string value to integer and OHE make integer in ohe encoding, if your column is in int like 1,2,3 you can apply OHE directly. SI 将字符串值转换为整数,OHE 在 ohe 编码中生成整数,如果您的列在 int 中,例如 1,2,3,您可以直接应用 OHE。 But if your label in string like A,B,C you have to use SI first then chain OHE但是如果你的标签在像 A,B,C 这样的字符串中,你必须先使用 SI 然后链接 OHE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM