@@ -45,8 +45,9 @@ class MulticlassMetrics(predictionAndLabels: RDD[(Double, Double)]) {
4545 (prediction, if (prediction != label) 1 else 0 )
4646 }.reduceByKey(_ + _)
4747 .collectAsMap()
48- private lazy val confusions = predictionAndLabels.map {
49- case (prediction, label) => ((prediction, label), 1 )
48+ private lazy val confusions = predictionAndLabels
49+ .map { case (prediction, label) =>
50+ ((prediction, label), 1 )
5051 }.reduceByKey(_ + _).collectAsMap()
5152
5253 /**
@@ -55,11 +56,18 @@ class MulticlassMetrics(predictionAndLabels: RDD[(Double, Double)]) {
5556 * they are ordered by class label ascending,
5657 * as in "labels"
5758 */
58- lazy val confusionMatrix : Matrix = {
59+ def confusionMatrix : Matrix = {
5960 val transposedFlatMatrix = Array .ofDim[Double ](labels.size * labels.size)
60- for (i <- 0 to labels.size - 1 ; j <- 0 to labels.size - 1 ) {
61- transposedFlatMatrix(i * labels.size + j)
62- = confusions.getOrElse((labels(i), labels(j)), 0 ).toDouble
61+ val n = labels.size
62+ var i, j = 0
63+ while (i < n){
64+ j = 0
65+ while (j < n){
66+ transposedFlatMatrix(i * labels.size + j)
67+ = confusions.getOrElse((labels(i), labels(j)), 0 ).toDouble
68+ j += 1
69+ }
70+ i += 1
6371 }
6472 Matrices .dense(labels.size, labels.size, transposedFlatMatrix)
6573 }
0 commit comments