mahout计算一个简单的推荐程序的准确率和召回率

package test.mahout.recommendation;

import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.IRStatistics;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.apache.mahout.common.RandomUtils;

public class PreRecallEvaluation {
    public static void main(String[] args) throws IOException, TasteException
    {
        RandomUtils.useTestSeed();//确保每次结果一致
        DataModel model = new FileDataModel(new File("F:/mahout/mahout_test/intro.csv"));
        RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
        RecommenderBuilder builder = new RecommenderBuilder(){

            @Override
            public Recommender buildRecommender(DataModel model)
                    throws TasteException {
                // TODO Auto-generated method stub
                UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
                UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);
                
                return new  GenericUserBasedRecommender(model, neighborhood, similarity);
            }
            
        };
        /*
         * IRStatistics evaluate(RecommenderBuilder recommenderBuilder,
                      DataModelBuilder dataModelBuilder,
                      DataModel dataModel,
                      IDRescorer rescorer,
                      int at, Precision at 5 意思就是当推荐2个结果时的查准率
                      double relevanceThreshold,因为上述推荐引擎计算的推荐结果都是一个分值,
                          那么分值达到多少认为用户感兴趣呢,就需要一个阈值,如果没有指定值,框架会为每个用户取一个阈值,它等于该用户的平均偏好值加上标准方差
                      double evaluationPercentage)
                      throws TasteException
         */
        IRStatistics stats = evaluator.evaluate(builder, null, model, null, 2, GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);
        System.out.println(stats.getPrecision());
        System.out.println(stats.getRecall());
    }

}

结果为

0.75

1


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章