Giter Site home page Giter Site logo

nDCG about cf4j HOT 2 CLOSED

marciabarros avatar marciabarros commented on September 27, 2024
nDCG

from cf4j.

Comments (2)

ferortega avatar ferortega commented on September 27, 2024

Yes, we have implemented it for the version 2. However, here you have an implementation for the version 1.

import cf4j.TestUser;
import cf4j.qualityMeasures.QualityMeasure;
import cf4j.utils.Methods;

/**
 * <p>This class calculates the NDCG of the recommender system. It is calculated as 
 * follows:</p>
 * 
 * <p>NDCG = &lt;SumDcg&gt; / &lt;SumIdcg&gt;</p>
 *
 * <p>This class puts the "NDCG" key at the Kernel map containing a double with the 
 * NDCG value.</p>
 * 
 * @author Bo Zhu
 */
public class Ndcg extends QualityMeasure {
	
	private final static String NAME = "NDCG";
	
	/**
	 * Number of recommended items, NDCG@N
	 */
	private int numberOfRecommendations;
	

	/**
	 * Constructor of Ndcg
	 * @param numberOfRecommendations Number of recommendations
	 */
	public Ndcg (int numberOfRecommendations) {
		super(NAME);
		this.numberOfRecommendations = numberOfRecommendations;
	}

	@Override
	public double getMeasure (TestUser testUser) {
		
		double [] testRatings = testUser.getTestRatings();
				
        // Compute DCG
		
		double [] predictions = testUser.getPredictions();
		int [] recommendations = Methods.findTopN(predictions, this.numberOfRecommendations);
		
		double dcg = 0d;
		
		for (int i = 0; i < recommendations.length; i++) {
			int testItemIndex = recommendations[i];
			if (testItemIndex == -1) break;
			
			dcg += (Math.pow(2, testRatings[testItemIndex]) - 1) / (Math.log(i + 2) / Math.log(2));
		}
		
		// Compute IDCG
		
		int [] idealRecommendations = Methods.findTopN(testRatings, this.numberOfRecommendations);

		double idcg = 0d;	
		
		for (int i = 0; i < idealRecommendations.length; i++) {
			int testItemIndex = idealRecommendations[i];
			if (testItemIndex == -1) break;
			
			idcg += (Math.pow(2, testRatings[testItemIndex]) - 1) / (Math.log(i + 2) / Math.log(2));
		}
		
		// Compute NDCG
		
		double ndcg = dcg / idcg;
		
		return ndcg;
	}
}

from cf4j.

marciabarros avatar marciabarros commented on September 27, 2024

Thanks! Works perfectly.

from cf4j.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.