Comments (2)
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 = <SumDcg> / <SumIdcg></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.
Thanks! Works perfectly.
from cf4j.
Related Issues (12)
- is there a way to specify the testset from a file ? HOT 1
- What is the format of the file text? HOT 1
- Out Of memory HOT 8
- Mismatch in number of items HOT 2
- Adjusted cosine similarity HOT 2
- % value in the console HOT 3
- DatasetSplitters HOT 1
- Cosine Similarity Implementation HOT 3
- Release is a fat jar with bundled dependencies HOT 2
- Potential secutiry vulnerability in the shared library which cf4j depends on. Can you help upgrade to patch versions?
- Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 4D6F7665 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cf4j.