All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qualityMeasures.F1 Maven / Gradle / Ivy

Go to download

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 F1 score of the recommender system. It is as follows:

* *

F1 = 2 * precision * recall / (precision + recall)

* *

This class puts the "F1" key at the Kernel map containing a double with the precision * value and the recall value.

* * @author Fernando Ortega */ public class F1 extends QualityMeasure { private final static String NAME = "F1"; /** * Number of recommended items */ private int numberOfRecommendations; /** * Relevant rating threshold */ private double relevantThreshold; /** * Constructor of F1 * @param numberOfRecommendations Number of recommendations * @param relevantThreshold Minimum rating to consider a rating as relevant */ public F1 (int numberOfRecommendations, double relevantThreshold) { super(NAME); this.numberOfRecommendations = numberOfRecommendations; this.relevantThreshold = relevantThreshold; } @Override public double getMeasure (TestUser testUser) { // Items rated as relevant (in test) by the active user int relevant = 0; for (double rating : testUser.getTestRatings()) { if (rating >= this.relevantThreshold) { relevant++; } } // 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++; } // Precision y Recall double precision = (double) recommendedAndRelevant / (double) recommended; double recall = (double) recommendedAndRelevant / (double) relevant; // F1 score double f1 = 2 * precision * recall / (precision + recall); return f1; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy