net.librec.eval.ranking.RecallEvaluator Maven / Gradle / Ivy
Show all versions of librec-core Show documentation
/**
* Copyright (C) 2016 LibRec
*
* This file is part of LibRec.
* LibRec is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LibRec is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LibRec. If not, see .
*/
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;
import java.util.Set;
/**
* RecallEvaluator, calculate recall@n
* wikipedia, Recall
*
* @author Keqiang Wang
*/
public class RecallEvaluator 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 totalRecall = 0.0;
int numContext = groundTruthList.size();
int nonZeroContext = 0;
for (int contextIdx = 0; contextIdx < numContext; ++contextIdx) {
Set testSetByContext = groundTruthList.getKeySetByContext(contextIdx);
if (testSetByContext.size() > 0) {
List> recommendListByContext = recommendedList.getKeyValueListByContext(contextIdx);
int numHits = 0;
int topK = this.topN <= recommendListByContext.size() ? this.topN : recommendListByContext.size();
for (int indexOfKey = 0; indexOfKey < topK; indexOfKey++) {
int key = recommendListByContext.get(indexOfKey).getKey();
if (testSetByContext.contains(key)) {
numHits++;
}
}
totalRecall += numHits / (testSetByContext.size() + 0.0);
nonZeroContext++;
}
}
return nonZeroContext > 0 ? totalRecall / nonZeroContext : 0.0d;
}
}