zoukankan      html  css  js  c++  java
  • 推荐系统-04-评价技术

    下面简单通过在测试集上验证错误值 (JAVA)

    package xyz.pl8.evaluatorintro;
    
    import org.apache.mahout.cf.taste.common.TasteException;
    import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
    import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
    import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
    import org.apache.mahout.cf.taste.impl.eval.RMSRecommenderEvaluator;
    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 java.io.File;
    
    
    public class EvaluatorIntro {
        public static void main(String[] args){
            try{
                DataModel model = new FileDataModel(new File("/home/hadoop/ua.base"));
                System.out.println(model);
    
                Recommender recommender = null;
                RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
                    public Recommender buildRecommender(DataModel model) throws TasteException {
                        UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
                        UserNeighborhood neighborhood = new NearestNUserNeighborhood(100,  similarity, model );
                        return new GenericUserBasedRecommender(model, neighborhood, similarity);
                    }
                };
                RecommenderEvaluator avgEvaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
                RecommenderEvaluator rmsEvaluator = new RMSRecommenderEvaluator();
                // 平均绝对值误差
                double avgScore = avgEvaluator.evaluate(recommenderBuilder, null, model, 0.7, 1.0);
                // 根方差错误
                double rmsScore = rmsEvaluator.evaluate(recommenderBuilder, null, model, 0.7, 1.0);
    
                System.out.println(avgScore);
                System.out.println(rmsScore);
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    以下是通过信息检索, 进行多维度的评价模型的优劣度(java)

    package xyz.pl8.irevaluatorintro;
    
    
    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.RecommenderEvaluator;
    import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
    import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
    import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
    import org.apache.mahout.cf.taste.impl.eval.RMSRecommenderEvaluator;
    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 java.io.File;
    
    public class IREvaluatorIntro {
        public static void main(String[] args){
            try{
                DataModel model = new FileDataModel(new File("/home/hadoop/ua.base"));
    
                Recommender recommender = null;
                RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
                    public Recommender buildRecommender(DataModel model) throws TasteException {
                        UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
                        UserNeighborhood neighborhood = new NearestNUserNeighborhood(100,  similarity, model );
                        return new GenericUserBasedRecommender(model, neighborhood, similarity);
                    }
                };
                // 构造信息检索评估器
                RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
    
                // 进行评估
                IRStatistics stats = evaluator.evaluate(recommenderBuilder, null, model, null, 5, 0.7, 1.0);
    
                // 输出精确度,召回率, F1测度
                System.out.println(stats.getPrecision());
                System.out.println(stats.getRecall());
                System.out.println(stats.getF1Measure());
    
    
    
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    android开发架构理解
    检测到LoaderLock”的解决办法(新办法,在vs2008中)
    1-2SPRING-BOOT-JPA添加lombok-管理getter/setter
    PHP数组使用
    处理sublime中文乱码
    c#遍历datatable,dataset
    sublime3更换主题,主题推荐
    laravel voyager 安装
    array_filter,匿名函数
    git 记住密码
  • 原文地址:https://www.cnblogs.com/freebird92/p/9047536.html
Copyright © 2011-2022 走看看