开发者社区> 问答> 正文

Apache Spark ML Pipeline:过滤数据集中的空行

在我的Spark ML Pipeline(Spark 2.3.0)中我使用RegexTokenizer如下:

val regexTokenizer = new RegexTokenizer()

  .setInputCol("text")
  .setOutputCol("words")
  .setMinTokenLength(3)

它转换DataFrame为具有单词数组的那个,例如:

text | words

a the | [the]
a of to | []
big small | [big,small]
如何使用空[]数组过滤行?我应该创建自定义变换器并将其传递给管道吗?

展开
收起
社区小助手 2018-12-12 13:55:33 2304 0
1 条回答
写回答
取消 提交回答
  • 社区小助手是spark中国社区的管理员,我会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关spark的问题及回答。

    你可以使用SQLTransformer:

    import org.apache.spark.ml.feature.SQLTransformer

    val emptyRemover = new SQLTransformer().setStatement(
    "SELECT * FROM THIS WHERE size(words) > 0"
    )
    这个可以直接申请

    val df = Seq(
    ("a the", Seq("the")), ("a of the", Seq()),
    ("big small", Seq("big", "small"))
    ).toDF("text", "words")

    emptyRemover.transform(df).show
    text words
    a the [the]
    big small [big, small]

    或用于Pipeline。

    在Spark ML过程中使用它之前我会考虑两次。通常在下游使用的工具,例如CountVectorizer,可以很好地处理空输入:

    import org.apache.spark.ml.feature.CountVectorizer

    val vectorizer = new CountVectorizer()
    .setInputCol("words")
    .setOutputCol("features")
    +---------+------------+-------------------+

    text words features
    a the [the] (3,[2],[1.0])
    a of the [] (3,[],[])
    big small [big, small] (3,[0,1],[1.0,1.0])
    2019-07-17 23:20:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Apache Flink技术进阶 立即下载
Apache Spark: Cloud and On-Prem 立即下载
Hybrid Cloud and Apache Spark 立即下载

相关镜像