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

Des*_* pv 6 scala machine-learning apache-spark apache-spark-ml apache-spark-mllib

我对BinaryClassificationMetrics(Mllib)输入感到困惑.按照Apache的火花1.6.0,我们需要传递predictedandlabel类型的(RDD[(Double,Double)])从转化的数据帧将具有预测概率(矢量)rawPrediction(矢量).

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

它是准备以正确的方式PredictedandLabel或者我需要使用rawPrediction列或概率列,而不是预测列?

小智 2

像这样准备:

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)))
Run Code Online (Sandbox Code Playgroud)

并评价:

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

new BinaryClassificationMetrics(preds, 10).roc
Run Code Online (Sandbox Code Playgroud)

如果预测只有 0 或 1 个,则存储桶的数量可能会更低,就像您的情况一样。尝试更复杂的数据,如下所示:

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