立即注册 登录
About云-梭伦科技 返回首页

rsgg03的个人空间 https://www.aboutyun.com/?6 [收藏] [复制] [分享] [RSS]

日志

mahout实现查准率和查全率评估的配置与运行

已有 1870 次阅读2015-5-5 00:23 | 查全率, 查准率, 评估

/* 
 * 查准率和查全率评估的配置与运行 
 *  
 * */  
package byuser;  
  
import java.io.File;  
  
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 RecommenderIRStatsEvaluatorTest {  
  
    public RecommenderIRStatsEvaluatorTest(){  
        try{  
            RandomUtils.useTestSeed();  
            DataModel model = new FileDataModel(new File("E:\\mahout项目\\examples\\intro.csv"));  
            RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();  
            RecommenderBuilder recommenderBuilder = 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 stats = evaluator.evaluate(recommenderBuilder, null, model, null, 2, GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);  
            System.out.println("推荐两个结果的查准率 :" + stats.getPrecision());  
            System.out.println("推荐两个结果的查全率 :" + stats.getRecall());  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        RecommenderIRStatsEvaluatorTest test = new RecommenderIRStatsEvaluatorTest();  
    }  
  
}  

结果:




路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条