【问题标题】:Spark random forest binary classifier metricsSpark 随机森林二元分类器指标
【发布时间】:2016-09-30 16:24:05
【问题描述】:

在 Spark Mllib 中训练随机森林二元分类器模型时如何获得模型指标(F 分数、AUROC、AUPRC 等)?

问题在于BinaryClassificationMetrics 采用概率,而 RandomForest 分类器的 predict 方法返回离散值 0 或 1。

见:https://spark.apache.org/docs/latest/mllib-evaluation-metrics.html#binary-classification

RandomForest.trainClassifier 没有任何 clearThreshold 方法可以使其返回概率而不是离散的 0 或 1 标签。

【问题讨论】:

  • @eliasah 实际上不是重复的问题,但那里的答案提供了问题的解决方案。在您发表评论之前,我已经在答案中添加了这一点。
  • 没关系。没问题 !因此使用“可能”这个词
  • @eliasah 这个问题实际上并不重复,因为它没有询问指标。那里的答案确实指向新的ml API,它有助于找到解决方案。请参阅调整后的 apache 文档示例以适合此问题的更新答案。

标签: scala apache-spark apache-spark-mllib


【解决方案1】:

我们需要使用新的基于ml DataFrames 的API 来获取概率,而不是使用基于RDD 的mllib API。

更新

以下是 Spark 文档中的更新示例,以使用 BinaryClassificationEvaluator 并显示指标:Area Under Receiver Operating Characteristic (AUROC) 和 Area Under Precision Recall Curve (AUPRC)。

import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, VectorIndexer}

// Load and parse the data file, converting it to a DataFrame.
val data = sqlContext.read.format("libsvm").load("D:/Sources/spark/data/mllib/sample_libsvm_data.txt")

// 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("indexedLabel")
  .setFeaturesCol("indexedFeatures")
  .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))

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

// Make predictions.
val predictions = model.transform(testData)

// Select example rows to display.
predictions
  .select("indexedLabel", "rawPrediction", "prediction")
  .show()

val binaryClassificationEvaluator = new BinaryClassificationEvaluator()
  .setLabelCol("indexedLabel")
  .setRawPredictionCol("rawPrediction")

def printlnMetric(metricName: String): Unit = {
  println(metricName + " = " + binaryClassificationEvaluator.setMetricName(metricName).evaluate(predictions))
}

printlnMetric("areaUnderROC")
printlnMetric("areaUnderPR")

【讨论】:

  • 如果投反对票的人能解释原因以便改进答案,那将非常有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2018-05-20
  • 2019-02-01
  • 2018-03-05
  • 2017-10-20
  • 1970-01-01
  • 2019-09-05
相关资源
最近更新 更多