qualityMeasures.Precision Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cf4j-recsys Show documentation
Show all versions of cf4j-recsys Show documentation
A Java's Collaborative Filtering library to carry out experiments in research of Collaborative Filtering based Recommender Systems. The library has been designed from researchers to researchers.
The newest version!
package cf4j.qualityMeasures;
import cf4j.TestUser;
import cf4j.utils.Methods;
/**
* This class calculates the Precision of the recommender system. It is calculated as
* follows:
*
* precision = <relevant recommended items> / <number of recommended items>
*
* This class puts the "Precision" key at the Kernel map containing a double with the
* precision value.
*
* @author Fernando Ortega
*/
public class Precision extends QualityMeasure {
private final static String NAME = "Precision";
/**
* Number of recommended items
*/
private int numberOfRecommendations;
/**
* Relevant rating threshold
*/
private double relevantThreshold;
/**
* Constructor of Precision
* @param numberOfRecommendations Number of recommendations
* @param relevantThreshold Minimum rating to consider a rating as relevant
*/
public Precision (int numberOfRecommendations, double relevantThreshold) {
super(NAME);
this.numberOfRecommendations = numberOfRecommendations;
this.relevantThreshold = relevantThreshold;
}
@Override
public double getMeasure (TestUser testUser) {
// Items that has been recommended and was relevant to the active user
double [] predictions = testUser.getPredictions();
int [] recommendations = Methods.findTopN(predictions, this.numberOfRecommendations);
int recommendedAndRelevant = 0, recommended = 0;
for (int testItemIndex : recommendations) {
if (testItemIndex == -1) break;
if (testUser.getTestRatings()[testItemIndex] >= this.relevantThreshold) {
recommendedAndRelevant++;
}
recommended++;
}
double precision = (double) recommendedAndRelevant / (double) recommended;
return precision;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy