scala - Spark 1.5.1,MLLib 随机森林概率

标签 scala apache-spark random-forest apache-spark-mllib

我正在使用带有 MLLib 的 Spark 1.5.1。我使用 MLLib 构建了一个随机森林模型,现在使用该模型进行预测。我可以使用 .predict 函数找到预测类别(0.0 或 1.0)。但是,我找不到检索概率的功能(请参阅随附的屏幕截图)。我认为 spark 1.5.1 随机森林会提供概率,我在这里遗漏了什么吗?

enter image description here

最佳答案

很遗憾,该功能在旧版 Spark MLlib 1.5.1 中不可用。

但是,您可以在 Spark MLlib 2.x 中最近的 Pipeline API 中找到它作为 RandomForestClassifier:

import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, VectorIndexer}
import org.apache.spark.mllib.util.MLUtils

// Load and parse the data file, converting it to a DataFrame.
val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF

// Index labels, adding metadata to the label column.
// Fit on whole dataset to include all labels in index.
val labelIndexer = new StringIndexer()
  .setInputCol("label")
  .setOutputCol("indexedLabel").fit(data)

// Automatically identify categorical features, and index them.
// Set maxCategories so features with > 4 distinct values are treated as continuous.
val featureIndexer = new VectorIndexer()
  .setInputCol("features")
  .setOutputCol("indexedFeatures")
  .setMaxCategories(4).fit(data)

// Split the data into training and test sets (30% held out for testing)
val Array(trainingData, testData) = data.randomSplit(Array(0.7, 0.3))

// Train a RandomForest model.
val rf = new RandomForestClassifier()
  .setLabelCol(labelIndexer.getOutputCol)
  .setFeaturesCol(featureIndexer.getOutputCol)
  .setNumTrees(10)

// Convert indexed labels back to original labels.
val labelConverter = new IndexToString()
  .setInputCol("prediction")
  .setOutputCol("predictedLabel")
  .setLabels(labelIndexer.labels)

// Chain indexers and forest in a Pipeline
val pipeline = new Pipeline()
  .setStages(Array(labelIndexer, featureIndexer, rf, labelConverter))

// Fit model. This also runs the indexers.
val model = pipeline.fit(trainingData)

// Make predictions.
val predictions = model.transform(testData)
// predictions: org.apache.spark.sql.DataFrame = [label: double, features: vector, indexedLabel: double, indexedFeatures: vector, rawPrediction: vector, probability: vector, prediction: double, predictedLabel: string]

predictions.show(10)
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// |label|            features|indexedLabel|     indexedFeatures|rawPrediction|probability|prediction|predictedLabel|
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// |  0.0|(692,[124,125,126...|         1.0|(692,[124,125,126...|   [0.0,10.0]|  [0.0,1.0]|       1.0|           0.0|
// |  0.0|(692,[124,125,126...|         1.0|(692,[124,125,126...|    [1.0,9.0]|  [0.1,0.9]|       1.0|           0.0|
// |  0.0|(692,[129,130,131...|         1.0|(692,[129,130,131...|    [1.0,9.0]|  [0.1,0.9]|       1.0|           0.0|
// |  0.0|(692,[154,155,156...|         1.0|(692,[154,155,156...|    [1.0,9.0]|  [0.1,0.9]|       1.0|           0.0|
// |  0.0|(692,[154,155,156...|         1.0|(692,[154,155,156...|    [1.0,9.0]|  [0.1,0.9]|       1.0|           0.0|
// |  0.0|(692,[181,182,183...|         1.0|(692,[181,182,183...|    [1.0,9.0]|  [0.1,0.9]|       1.0|           0.0|
// |  1.0|(692,[99,100,101,...|         0.0|(692,[99,100,101,...|    [4.0,6.0]|  [0.4,0.6]|       1.0|           0.0|
// |  1.0|(692,[123,124,125...|         0.0|(692,[123,124,125...|   [10.0,0.0]|  [1.0,0.0]|       0.0|           1.0|
// |  1.0|(692,[124,125,126...|         0.0|(692,[124,125,126...|   [10.0,0.0]|  [1.0,0.0]|       0.0|           1.0|
// |  1.0|(692,[125,126,127...|         0.0|(692,[125,126,127...|   [10.0,0.0]|  [1.0,0.0]|       0.0|           1.0|
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// only showing top 10 rows

注意:这个例子来自Spark MLlib的官方文档ML - Random forest classifier .

这里是一些输出列的一些解释:

  • predictionCol 表示预测的标签。
  • rawPredictionCol 表示长度为 # 个类别的向量,其中包含进行预测的树节点处的训练实例标签计数(仅适用于分类)。
  • probabilityCol 表示长度 # 个类别等于 rawPrediction 的概率向量,归一化为多项分布(仅适用于分类)。

关于scala - Spark 1.5.1,MLLib 随机森林概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33401437/

相关文章:

scala - 使用 JavaMail + Scala 从 Gmail 检索邮件时出错

java - 在java中处理来自mysql的数百万条记录并将结果存储在另一个数据库中

apache-spark - 如何在Spark SQL中控制分区大小

scala - 在 Apache Spark 中,RandomForestClassifier 的输入带有无效标签列错误

performance - 在 scala 中的代码块导入成本很高

scala - 向 Scala 案例类添加方法是个好主意吗

randomForest模型大小取决于训练集大小: A way to avoid?

python-3.x - 在随机森林中使用predict()与predict_proba()计算时ROC_AUC_SCORE是不同的

scala - 当processElement依赖于广播数据时,如何在flink中对BroadcastProcessFunction进行单元测试

apache-spark - 了解 Spark 中的 shuffle 管理器