mahout 推荐系统 之 评估查全率与查准率

从更全面的看待推荐系统:通过偏好值来生成推荐结果并非绝对必要。给出一个从优到劣排序的推荐结果在很多场景就够用了,而不用必须包含估计的偏好值;而事实上,有时候精确的列表顺序也不是那么必要,有几个好的结果就可以了。

从这种普遍的视角,可以运用经典的信息检索(information retrieval)度量标准来评估推荐系统:查准率(precision)、查全率(recall)

一个评估查全率与查准率的简单demo:

package com.xh.recommender;

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;

import java.io.File;
import java.net.URL;

/**
 * @author xiaohe
 * @version V1.0.0
 * @Description: 评估 查全率 与 查准率
 * @date: 2018-8-25 18:52
 * @Copyright:
 */
public class IREvaluatorIntro {

    public static void main(String[] args) throws Exception {
        /**
         *
         * 强制 每次选择相同的随机值
         * 只是为了获取可重复的结果
         * 可以用在demo和测试用例,不能用在实际代码中
         *
         */
        RandomUtils.useTestSeed();
        final String filePath = "intro.csv";
        URL url = RecommenderIntro.class.getClassLoader()
                .getResource(filePath);
        File modelFile = new File(url.getFile());
        if(!modelFile.exists()) {
            System.err.println("Please, specify name of file, or put file 'input.csv' into current directory!");
            System.exit(1);
        }
        // 装载数据文件
        DataModel model = new FileDataModel(modelFile);

        RecommenderIRStatsEvaluator evaluator =
                new GenericRecommenderIRStatsEvaluator();
        // 构建  推荐 示例
        RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
            /**
             *
             * Recommender 是由新的 DataModel 构建的
             *
             */
            @Override
            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, 2,
                GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD,
                1.0);
        // 查准率  有多少结果是好的
        System.out.println(stats.getPrecision());
        // 查全率  有多少好的推荐包含在里面
        System.out.println(stats.getRecall());
    }

}

项目结构:
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值