Logistic回归的召回率和精度计算

时间:2020-02-15 22:12:34

标签: scala apache-spark

我对分类问题实施了逻辑回归。我得到的精度,召回率和F1分数等于1.的值相同。

val model = pipeline.fit(train)
val predicted = model.transform(test)
val predictionAndLabels = predicted.
select($"label",$"prediction").
as[(Double, Double)].
rdd

// Instantiate metrics object
val metrics = new BinaryClassificationMetrics(predictionAndLabels)
  // If threshold is 0.5 as what we want, then get the precision and append it to the string. Idea is if score is <0.5 class 0, else class 1.
// Precision by threshold
val precision = metrics.precisionByThreshold
precision.foreach { case (t, p) =>
println(s"Threshold: $t, Precision: $p")
}

// Recall by threshold
val recall = metrics.recallByThreshold
recall.foreach { case (t, r) =>
println(s"Threshold: $t, Recall: $r")
}

// Precision-Recall Curve
val PRC = metrics.pr

// F-measure
val f1Score = metrics.fMeasureByThreshold
f1Score.foreach { case (t, f) =>
println(s"Threshold: $t, F-score: $f, Beta = 1")
}

val beta = 0.5
val fScore = metrics.fMeasureByThreshold(beta)
f1Score.foreach { case (t, f) =>
println(s"Threshold: $t, F-score: $f, Beta = 0.5")
}

// AUPRC
val auPRC = metrics.areaUnderPR
println(s"Area under precision-recall curve = $auPRC")

// Compute thresholds used in ROC and PR curves
val thresholds = precision.map(_._1)

// ROC Curve
val roc = metrics.roc

// AUROC
val auROC = metrics.areaUnderROC
println(s"Area under ROC = $auROC")
val testErr = predictionAndLabels.filter(r => r._1 != r._2).count.toDouble / test.count()
println("Accuracy = " + (1-testErr) * 100 + " %")

阈值:1.0,精度:1.0阈值:0.0,精度: 0.500809498111171阈值:1.0,调用:1.0阈值:0.0,调用:1.0

如何正确计算?

0 个答案:

没有答案