Skip to content

Commit

Permalink
Replacing fold(_ + _) with sum as suggested by srowen
Browse files Browse the repository at this point in the history
  • Loading branch information
avulanov committed Jul 2, 2014
1 parent ca46765 commit 79e8476
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,37 @@ class MultilabelMetrics(predictionAndLabels:RDD[(Set[Double], Set[Double])]) ext
* @return Accuracy.
*/
lazy val accuracy = predictionAndLabels.map{ case(predictions, labels) =>
labels.intersect(predictions).size.toDouble / labels.union(predictions).size}.
fold(0.0)(_ + _) / numDocs
labels.intersect(predictions).size.toDouble / labels.union(predictions).size}.sum / numDocs

/**
* Returns Hamming-loss
* @return hammingLoss.
*/
lazy val hammingLoss = (predictionAndLabels.map{ case(predictions, labels) =>
labels.diff(predictions).size + predictions.diff(labels).size}.
fold(0)(_ + _)).toDouble / (numDocs * numLabels)
sum).toDouble / (numDocs * numLabels)

/**
* Returns Document-based Precision averaged by the number of documents
* @return macroPrecisionDoc.
*/
lazy val macroPrecisionDoc = (predictionAndLabels.map{ case(predictions, labels) =>
if(predictions.size >0)
predictions.intersect(labels).size.toDouble / predictions.size else 0}.fold(0.0)(_ + _)) /
numDocs
predictions.intersect(labels).size.toDouble / predictions.size else 0}.sum) / numDocs

/**
* Returns Document-based Recall averaged by the number of documents
* @return macroRecallDoc.
*/
lazy val macroRecallDoc = (predictionAndLabels.map{ case(predictions, labels) =>
labels.intersect(predictions).size.toDouble / labels.size}.fold(0.0)(_ + _)) / numDocs
labels.intersect(predictions).size.toDouble / labels.size}.sum) / numDocs

/**
* Returns Document-based F1-measure averaged by the number of documents
* @return macroRecallDoc.
*/
lazy val macroF1MeasureDoc = (predictionAndLabels.map{ case(predictions, labels) =>
2.0 * predictions.intersect(labels).size /
(predictions.size + labels.size)}.fold(0.0)(_ + _)) / numDocs
2.0 * predictions.intersect(labels).size / (predictions.size + labels.size)}.sum) / numDocs

/**
* Returns micro-averaged document-based Precision
Expand Down

0 comments on commit 79e8476

Please sign in to comment.