码迷,mamicode.com
首页 > 其他好文 > 详细

mahout实现基于用户的Mahout推荐程序

时间:2015-05-02 19:34:16      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 这里做的是一个基于用户的Mahout推荐程序    
 * 这里利用已经准备好的数据。        
 * */
package byuser;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
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.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class RecommenderIntro {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            //进行数据的装载
            DataModel model = new FileDataModel(new File("E:\\mahout项目\\examples\\intro.csv"));
            
            UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity(model);
            UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);
            
            //生成推荐引擎
            Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
            
            //为用户已推荐一件商品recommend( , );其中参数的意思是:第几个人,然后推荐几件商品
            List<RecommendedItem> recommendations = recommender.recommend(1, 1);
            for(RecommendedItem recommendation : recommendations){
                System.out.println("根据您的浏览,为您推荐的商品是:" + recommendation);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TasteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}




结果:


技术分享

mahout实现基于用户的Mahout推荐程序

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/45441347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!