分类字段的一致索引和分类

Consistent indexing and categorizing of categorical fields

假设我有以下 Scala 代码:

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

val df = spark.createDataFrame(Seq(
  (0, "a"),
  (1, "b"),
  (2, "c"),
  (3, "a"),
  (4, "a"),
  (5, "c")
)).toDF("id", "category")

val indexer = new StringIndexer()
  .setInputCol("category")
  .setOutputCol("categoryIndex")
  .fit(df)
val indexed = indexer.transform(df)

现在,假设我创建了一个使用此索引器的 org.apache.spark.mllib.tree.model.DecisionTreeModel 并将模型保存到文件中。

如果以后对新数据做预测,如何保证索引器与原始数据上构建模型时使用的原始索引器一致?

坚持并重新加载索引器顶部