Skip to content

Commit

Permalink
Merge pull request apache#1 from mengxr/avulanov-master
Browse files Browse the repository at this point in the history
minor updates
  • Loading branch information
avulanov committed Jul 15, 2014
2 parents 79c3555 + 5ebeb08 commit 2eae80f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class MulticlassMetrics(predictionAndLabels: RDD[(Double, Double)]) {
.collectAsMap()
private lazy val confusions = predictionAndLabels
.map { case (prediction, label) =>
((prediction, label), 1)
}.reduceByKey(_ + _).collectAsMap()
((label, prediction), 1)
}.reduceByKey(_ + _)
.collectAsMap()

/**
* Returns confusion matrix:
Expand All @@ -57,19 +58,18 @@ class MulticlassMetrics(predictionAndLabels: RDD[(Double, Double)]) {
* as in "labels"
*/
def confusionMatrix: Matrix = {
val transposedFlatMatrix = Array.ofDim[Double](labels.size * labels.size)
val n = labels.size
var i, j = 0
while(i < n){
j = 0
while(j < n){
transposedFlatMatrix(i * labels.size + j)
= confusions.getOrElse((labels(i), labels(j)), 0).toDouble
val values = Array.ofDim[Double](n * n)
var i = 0
while (i < n) {
var j = 0
while (j < n) {
values(i + j * n) = confusions.getOrElse((labels(i), labels(j)), 0).toDouble
j += 1
}
i += 1
}
Matrices.dense(labels.size, labels.size, transposedFlatMatrix)
Matrices.dense(n, n, values)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

package org.apache.spark.mllib.evaluation

import org.scalatest.FunSuite

import org.apache.spark.mllib.linalg.Matrices
import org.apache.spark.mllib.util.LocalSparkContext
import org.scalatest.FunSuite

class MulticlassMetricsSuite extends FunSuite with LocalSparkContext {
test("Multiclass evaluation metrics") {
Expand Down

0 comments on commit 2eae80f

Please sign in to comment.