Return-Path: X-Original-To: apmail-spark-reviews-archive@minotaur.apache.org Delivered-To: apmail-spark-reviews-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9289B17FF5 for ; Fri, 17 Oct 2014 22:22:37 +0000 (UTC) Received: (qmail 89952 invoked by uid 500); 17 Oct 2014 22:22:37 -0000 Delivered-To: apmail-spark-reviews-archive@spark.apache.org Received: (qmail 89932 invoked by uid 500); 17 Oct 2014 22:22:37 -0000 Mailing-List: contact reviews-help@spark.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list reviews@spark.apache.org Received: (qmail 89921 invoked by uid 99); 17 Oct 2014 22:22:37 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Oct 2014 22:22:37 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id D19489C7027; Fri, 17 Oct 2014 22:22:36 +0000 (UTC) From: mengxr To: reviews@spark.apache.org Reply-To: reviews@spark.apache.org References: In-Reply-To: Subject: [GitHub] spark pull request: SPARK-3568 [mllib] add ranking metrics Content-Type: text/plain Message-Id: <20141017222236.D19489C7027@tyr.zones.apache.org> Date: Fri, 17 Oct 2014 22:22:36 +0000 (UTC) Github user mengxr commented on a diff in the pull request: https://github.com/apache/spark/pull/2667#discussion_r19046954 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/evaluation/RankingMetrics.scala --- @@ -0,0 +1,115 @@ +/* + * 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 scala.reflect.ClassTag + +import org.apache.spark.SparkContext._ +import org.apache.spark.annotation.Experimental +import org.apache.spark.rdd.RDD + +/** + * ::Experimental:: + * Evaluator for ranking algorithms. + * + * @param predictionAndLabels an RDD of (predicted ranking, ground truth set) pairs. + */ +@Experimental +class RankingMetrics[T: ClassTag](predictionAndLabels: RDD[(Array[T], Array[T])]) { + + /** + * Compute the average precision of all the queries, truncated at ranking position k. + * If for a query, the ranking algorithm returns n (n < k) results, + * the precision value will be computed as #(relevant items retrived) / k. + * See the following paper for detail: + * + * IR evaluation methods for retrieving highly relevant documents. + * K. Jarvelin and J. Kekalainen + * + * @param k the position to compute the truncated precision + * @return the average precision at the first k ranking positions + */ + def precisionAt(k: Int): Double = predictionAndLabels.map { case (pred, lab) => + val labSet = lab.toSet + val n = math.min(pred.length, k) + var i = 0 + var cnt = 0 + + while (i < n) { + if (labSet.contains(pred(i))) { + cnt += 1 + } + i += 1 + } + cnt.toDouble / k + }.mean + + /** + * Returns the mean average precision (MAP) of all the queries + */ + lazy val meanAveragePrecision: Double = predictionAndLabels.map { case (pred, lab) => + val labSet = lab.toSet + var i = 0 + var cnt = 0 + var precSum = 0.0 + val n = pred.length + + while (i < n) { + if (labSet.contains(pred(i))) { + cnt += 1 + precSum += cnt.toDouble / (i + 1) + } + i += 1 + } + precSum / labSet.size + }.mean + + /** + * Compute the average NDCG value of all the queries, truncated at ranking position k. + * If for a query, the ranking algorithm returns n (n < k) results, the NDCG value at + * at position n will be used. See the following paper for detail: + * + * IR evaluation methods for retrieving highly relevant documents. + * K. Jarvelin and J. Kekalainen + * + * @param k the position to compute the truncated ndcg + * @return the average ndcg at the first k ranking positions + */ + def ndcgAt(k: Int): Double = predictionAndLabels.map { case (pred, lab) => + val labSet = lab.toSet + val labSetSize = labSet.size + val n = math.min(math.max(pred.length, labSetSize), k) + var maxDcg = 0.0 + var dcg = 0.0 + var i = 0 + + while (i < n) { + // Calculate 1/log2(i + 2) --- End diff -- the comment doesn't provide any extra information --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org For additional commands, e-mail: reviews-help@spark.apache.org