apache-spark - 使用 Apache Spark ML,您如何转换(用于预测)没有标签的数据集?

标签 apache-spark apache-spark-mllib apache-spark-ml

我确信我在理解 Spark ML 的管道方面存在差距。

我有一个针对一组数据进行训练的管道,其模式为“标签”、“评论”(两个字符串)。我的管道转换“标签”,添加“indexedLabel”,并通过标记化对“评论”进行矢量化,然后是 HashingTF(以“vectorizedComment”结尾)管道以 LogisticRegression 结束,带有标签列“indexedLabel”和特征列“vectorizedComment”。

而且效果很好!我可以适应我的管道并得到一个管道模型,它整天用“标签”、“评论”转换数据集! 但是,我的目标是能够抛出仅包含“评论”的数据集,因为“标签”仅用于训练模型目的。

我确信我在理解管道预测的工作原理方面存在差距 - 有人可以为我指出吗?

最佳答案

标签的转换可以在管道外完成(即之前)。该标签仅在训练期间是必需的,而不是在管道/模型的实际使用期间。通过在管道中执行标签转换,任何数据框都需要有一个不需要的标签列。

小例子:

val indexer = new StringIndexer()
  .setInputCol("label")
  .setOutputCol("indexedLabel")

val df2 = indexer.fit(df).transform(df)

// Create pipeline with other stages and use df2 to fit it

或者,您可以有两个独立的管道。一种包括在训练期间使用的标签转换,一种不包括它。确保其他阶段在两个管道中引用相同的对象。

val indexer = new StringIndexer()
  .setInputCol("label")
  .setOutputCol("indexedLabel")

// Create feature transformers and add to the pipelines

val pipelineTraining = new Pipeline().setStages(Array(indexer, ...))
val pipelineUsage = new Pipeline().setStages(Array(...))

关于apache-spark - 使用 Apache Spark ML,您如何转换(用于预测)没有标签的数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732768/

相关文章:

apache-spark - 如何在 Spark SQL 中找到分组向量列的平均值?

apache-spark - Spark ML 和 MLLIB 包有什么区别

scala - 在 Apache Spark 中连接到 SQLite

java - Spark 的 OnlineLDAOptimizer 在 Java 中导致 IndexOutOfBoundsException

apache-spark - 为什么广播连接收集数据到驱动程序以洗牌数据?

pyspark - 如何在 pyspark 中可视化决策树模型/对象?

python - 在 pyspark 中转换 ALS 的输入数据

apache-spark - Spark.ml LogisticRegression 是否仅假设数值特征?

bash - Hadoop Yarn 上的 Spark 安装

apache-spark - yarn 上 Spark 流的动态分配不会缩小执行器规模