net.librec.eval.ranking.AverageReciprocalHitRankEvaluator Maven / Gradle / Ivy
Show all versions of librec-core Show documentation
package net.librec.eval.ranking;
import net.librec.eval.AbstractRecommenderEvaluator;
import net.librec.recommender.item.KeyValue;
import net.librec.recommender.item.RecommendedList;
import java.util.List;
/**
* HitRateEvaluator
*
* Xia Ning and George Karypis, SLIM: Sparse Linear Methods for Top-N Recommender Systems, ICDM 2011.
*
* They apply a leave-one-out validation method to evaluate the algorithm performance. In each run, each of the datasets
* is split into a training set and a testing set by randomly selecting one of the non-zero entries of each user and
* placing it into the testing set.
*
* @author Keqiang Wang
*/
public class AverageReciprocalHitRankEvaluator extends AbstractRecommenderEvaluator {
/**
* Evaluate on the test set with the list of recommended items.
*
* @param groundTruthList the given ground truth list
* @param recommendedList the list of recommended items
* @return evaluate result
*/
public double evaluate(RecommendedList groundTruthList, RecommendedList recommendedList) {
double reciprocalRank = 0.0;
int numContext = groundTruthList.size();
int nonZeroContext = 0;
for (int contextIdx = 0; contextIdx < numContext; ++contextIdx) {
List> testListByContext = groundTruthList.getKeyValueListByContext(contextIdx);
if (testListByContext.size() > 0) {
List> recommendListByContext = recommendedList.getKeyValueListByContext(contextIdx);
int trueKeyIdx = testListByContext.get(0).getKey();
int topK = this.topN <= recommendListByContext.size() ? this.topN : recommendListByContext.size();
for (int indexOfKey = 0; indexOfKey < topK; ++indexOfKey) {
if (recommendListByContext.get(indexOfKey).getKey() == trueKeyIdx) {
reciprocalRank += 1.0 / (indexOfKey + 1.0);
break;
}
}
nonZeroContext++;
}
}
return nonZeroContext > 0 ? reciprocalRank / nonZeroContext : 0.0d;
}
}