Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1270#discussion_r17395490
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala 
---
    @@ -0,0 +1,147 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.evaluation
    +
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.SparkContext._
    +
    +/**
    + * Evaluator for multilabel classification.
    + * @param predictionAndLabels an RDD of (predictions, labels) pairs, both 
are non-null sets.
    + */
    +class MultilabelMetrics(predictionAndLabels: RDD[(Set[Double], 
Set[Double])]) {
    +
    +  private lazy val numDocs: Long = predictionAndLabels.count
    +
    +  private lazy val numLabels: Long = predictionAndLabels.flatMap { case 
(_, labels) =>
    +    labels}.distinct.count
    +
    +  /**
    +   * Returns subset accuracy
    +   * (for equal sets of labels)
    +   */
    +  lazy val subsetAccuracy: Double = predictionAndLabels.filter { case 
(predictions, labels) =>
    +    predictions == labels}.count.toDouble / numDocs
    +
    +  /**
    +   * Returns accuracy
    +   */
    +  lazy val accuracy: Double = predictionAndLabels.map { case (predictions, 
labels) =>
    +    labels.intersect(predictions).size.toDouble / 
labels.union(predictions).size}.sum / numDocs
    +
    +  /**
    +   * Returns Hamming-loss
    +   */
    +  lazy val hammingLoss: Double = (predictionAndLabels.map { case 
(predictions, labels) =>
    +    labels.diff(predictions).size + predictions.diff(labels).size}.
    +    sum).toDouble / (numDocs * numLabels)
    +
    +  /**
    +   * Returns document-based precision averaged by the number of documents
    +   */
    +  lazy val precision: Double = (predictionAndLabels.map { case 
(predictions, labels) =>
    +    if (predictions.size > 0) {
    +      predictions.intersect(labels).size.toDouble / predictions.size
    +    } else 0
    +  }.sum) / numDocs
    +
    +  /**
    +   * Returns document-based recall averaged by the number of documents
    +   */
    +  lazy val recall: Double = (predictionAndLabels.map { case (predictions, 
labels) =>
    +    labels.intersect(predictions).size.toDouble / labels.size}.sum) / 
numDocs
    +
    +  /**
    +   * Returns document-based f1-measure averaged by the number of documents
    +   */
    +  lazy val f1Measure: Double = (predictionAndLabels.map { case 
(predictions, labels) =>
    +    2.0 * predictions.intersect(labels).size / (predictions.size + 
labels.size)}.sum) / numDocs
    +
    +
    +  private lazy val tpPerClass = predictionAndLabels.flatMap { case 
(predictions, labels) =>
    +    predictions.intersect(labels).map(category => (category, 
1))}.reduceByKey(_ + _).collectAsMap()
    --- End diff --
    
    ~~~
    flatMap { case predictions, labels) => 
      predictions.intersect(labels)
    }.countByValue()`
    ~~~


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to