Search code examples
javamahout

Illegal precision with GenericRecommenderIRStatsEvaluator in mahout?


I am following the code example as below:

    RandomUtils.useTestSeed();
    DataModel model = new FileDataModel(new File(file));
    RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
    RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
        public Recommender buildRecommender(DataModel model)
                throws TasteException {
            UserSimilarity similarity = new PearsonCorrelationSimilarity(
                    model);
            UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,
                    similarity, model);
            return new GenericUserBasedRecommender(model, neighborhood,
                    similarity);
        }
    };
    IRStatistics stats = evaluator.evaluate(recommenderBuilder, null,
            model, null, 4, 4, 0.5);
    System.out.println(stats.getPrecision());
    System.out.println(stats.getRecall());

and the data in the file is:

 1,101,5.0
 1,102,3.0
 1,103,2.5
 2,101,2.0
 2,102,2.5
 2,103,5.0
 2,104,2.0
 3,101,2.5
 3,104,4.0
 3,105,4.5
 3,107,5.0
 4,101,5.0
 4,103,3.0
 4,104,4.5
 4,106,4.0
 5,101,4.0
 5,102,3.0
 5,103,2.0
 5,104,4.0
 5,105,3.5
 5,106,4.0

When running the code above on the data file, I got the

java.lang.IllegalArgumentException: Illegal precision: NaN Why did this happen? I find something related here, but it is posted in 2009 and cannot explain my confusion.


Solution

  • Ultimately, the error means that no precision or recall could be computed, and this is because the test data set is so small that no meaningful tests were possible. I don't think it will be enough, but, change 0.5 to 1.0 and lower your threshold from 4 to 3.

    The IllegalArgumentException is just a cosmetic bug that shouldn't occur. It was fixed in SVN a while ago. But it would just then show you that the precision and recall were undefined.