scala – 如何在Naive Bayes模型的BinaryClassificationMetrics评估中给出预测和标签列

我对BinaryClassificationMetrics(Mllib)输入感到困惑.根据 Apache Spark 1.6.0,我们需要从已经预测的变换的DataFrame中传递类型的预测和标签(RDD [(Double,Double)]),概率(向量)& rawPrediction(向量).

我已经从Predicted和label列创建了RDD [(Double,Double)].在NavieBayesModel上执行BinaryClassificationMetrics评估后,我能够检索ROC,PR等.但是值有限,我无法使用从中生成的值绘制曲线. Roc包含4个值,PR包含3个值.

它是准备PredictedandLabel的正确方法还是我需要使用rawPrediction列或Probability列而不是Predicted列?

解决方法

准备这样:

import org.apache.spark.mllib.linalg.Vector
import org.apache.spark.mllib.classification.{NaiveBayes,NaiveBayesModel}

val df = sqlContext.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")
val predictions = new NaiveBayes().fit(df).transform(df)

val preds = predictions.select("probability","label").rdd.map(row => 
  (row.getAs[Vector](0)(0),row.getAs[Double](1)))

并评估:

import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics

new BinaryClassificationMetrics(preds,10).roc

如果预测只有0或1个桶可以像你的情况一样更低.尝试更复杂的数据,如下所示:

val anotherPreds = df1.select(rand(),$"label").rdd.map(row => (row.getDouble(0),row.getDouble(1)))
new BinaryClassificationMetrics(anotherPreds,10).roc

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...