Handling NULL values in Spark StringIndexer

本秂侑毒 提交于 2019-12-06 00:32:55
zero323

Spark >= 2.2

Since Spark 2.2 NULL values can be handled with standard handleInvalid Param:

import org.apache.spark.ml.feature.StringIndexer

val df = Seq((0, "foo"), (1, "bar"), (2, null)).toDF("id", "label")
val indexer = new StringIndexer().setInputCol("label")

By default (error) it will throw an exception:

indexer.fit(df).transform(df).show
org.apache.spark.SparkException: Failed to execute user defined function($anonfun$9: (string) => double)
  at org.apache.spark.sql.catalyst.expressions.ScalaUDF.eval(ScalaUDF.scala:1066)
...
Caused by: org.apache.spark.SparkException: StringIndexer encountered NULL value. To handle or skip NULLS, try setting StringIndexer.handleInvalid.
  at org.apache.spark.ml.feature.StringIndexerModel$$anonfun$9.apply(StringIndexer.scala:251)
...

but configured to skip

indexer.setHandleInvalid("skip").fit(df).transform(df).show
+---+-----+---------------------------+
| id|label|strIdx_46a78166054c__output|
+---+-----+---------------------------+
|  0|    a|                        0.0|
|  1|    b|                        1.0|
+---+-----+---------------------------+

or to keep

indexer.setHandleInvalid("keep").fit(df).transform(df).show
+---+-----+---------------------------+
| id|label|strIdx_46a78166054c__output|
+---+-----+---------------------------+
|  0|    a|                        0.0|
|  1|    b|                        1.0|
|  3| null|                        2.0|
+---+-----+---------------------------+

Spark < 2.2

As for now (Spark 1.6.1) this problem hasn't been resolved but there is an opened JIRA (SPARK-11569). Unfortunately it is not easy to find an acceptable behavior. SQL NULL represents a missing / unknown value so any indexing is kind of meaningless.

Probably the best thing you can do is to use NA actions and either drop:

df.na.drop("column_to_be_indexed" :: Nil)

or fill:

df2.na.fill("__HEREBE_DRAGONS__", "column_to_be_indexed" :: Nil)

before you use indexer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!