以下代码博主所写,不一定准确,请勿乱用,如有参考,请注明地址。
import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics import org.apache.spark.rdd.RDD /** * Created by cf on 17-8-31. */ class MyBinaryClassificationMetrics(scoreAndLabels:RDD[(Double, Double)]) extends Serializable { val metrics = new BinaryClassificationMetrics(scoreAndLabels) val auROC = metrics.areaUnderROC() val auPR = metrics.areaUnderPR() val testSize = scoreAndLabels.count val TP = scoreAndLabels.filter{case (prediction, label) =>(prediction > 0.5) && (label > 0.5)}.count() val TN = scoreAndLabels.filter{case (prediction, label) =>(prediction <= 0.5) && (label <= 0.5)}.count() val FP = scoreAndLabels.filter{case (prediction, label) =>(prediction > 0.5) && (label <= 0.5)}.count() val FN = scoreAndLabels.filter{case (prediction, label) =>(prediction <= 0.5) && (label > 0.5)}.count() val accuracy = if(testSize == 0) 0 else (TP+TN)/(TP+TN+FP+FN).toDouble val precision = if(TP+TN == 0) 0 else TP/(TP+TN).toDouble val f1 = if(accuracy+precision == 0.0) 0 else 2 * accuracy * precision / (accuracy + precision) }