分享

Mahout构建图书推荐系统

本帖最后由 52Pig 于 2014-11-5 23:24 编辑
阅读导读:
1.Mahout中推荐过滤算法支持哪两种算法?
2.用java代码如何计算男性用户打分过的图书?
3.itemEuclidean,userEuclideanNoPref分别是什么算法?



1. 项目背景

Amazon是最早的电子商务网站之一,以网上图书起家,最后发展成为音像,电子消费品,游戏,生活用品等的综合性电子商务平台。Amazon的推荐系统,是互联网上最早的商品推荐系统,它为Amazon带来了至少30%的流量,和可观的销售利润。

如今推荐系统已经成为电子商务网站的标配,如果还没有推荐系统都不好意思,说自己是做电商的。

2. 需求分析

推荐系统如此重要,我们应该如果理解?
打开Amazon的Mahout In Action图书页面:
http://www.amazon.com/Mahout-Action-Sean-Owen/dp/1935182684/ref=pd_sim_b_1?ie=UTF8&refRID=0H4H2NSSR8F34R76E2TP

网页上的元素:
  • 广告位:广告商投放广告的位置,网站可以靠网络广告赚钱,一般是网页最好的位置。
  • 平均分:用户对图书的打分
  • 关联规则:通过关联规则,推荐位
  • 协同过滤:通过基于物品的协同过滤算法的,推荐位
  • 图书属性:包括页数,出版社,ISBN,语言等
  • 作者介绍:有关作者的介绍,和作者的其他著作
  • 用户评分:用户评分行为
  • 用户评论:用户评论的内容

amazon-book.png
结合上面2张截图,我们不难发现,推荐对于Amazon的重要性。除了最明显的广告位给了能直接带来利润的广告商,网页中有4处推荐位,分别从不同的维度,用不同的推荐算法,猜用户喜欢的商品。
3. 数据说明
2个数据文件:
  • rating.csv :用户评分行为数据
  • users.csv :用户属性数据
1). book-ratings.csv
  • 3列数据:用户ID,图书ID, 用户对图书的评分
  • 记录数: 4000次的图书评分
  • 用户数: 200个
  • 图书数: 1000个
  • 评分:1-10
数据示例
  1. 1,565,3
  2. 1,807,2
  3. 1,201,1
  4. 1,557,9
  5. 1,987,10
  6. 1,59,5
  7. 1,305,6
  8. 1,153,3
  9. 1,139,7
  10. 1,875,5
  11. 1,722,10
  12. 2,977,4
  13. 2,806,3
  14. 2,654,8
  15. 2,21,8
  16. 2,662,5
  17. 2,437,6
  18. 2,576,3
  19. 2,141,8
  20. 2,311,4
  21. 2,101,3
  22. 2,540,9
  23. 2,87,3
  24. 2,65,8
  25. 2,501,6
  26. 2,710,5
  27. 2,331,9
  28. 2,542,4
  29. 2,757,9
  30. 2,590,7
复制代码
2). users.csv
  • 3列数据:用户ID,用户性别,用户年龄
  • 用户数: 200个
  • 用户性别: M为男性,F为女性
  • 用户年龄: 11-80岁之间
数据示例
  1. 1,M,40
  2. 2,M,27
  3. 3,M,41
  4. 4,F,43
  5. 5,F,16
  6. 6,M,36
  7. 7,F,36
  8. 8,F,46
  9. 9,M,50
  10. 10,M,21
  11. 11,F,11
  12. 12,M,42
  13. 13,F,40
  14. 14,F,28
  15. 15,M,25
  16. 16,M,68
  17. 17,M,53
  18. 18,F,69
  19. 19,F,48
  20. 20,F,56
  21. 21,F,36
复制代码
4. 算法模型

本文主要介绍Mahout的基于物品的协同过滤模型,其他的算法模型将不再这里解释。

针对上面的数据,我将用7种算法组合进行测试:有关Mahout算法组合的详细解释,请参考文章:从源代码剖析Mahout推荐引擎

7种算法组合
  • userCF1: EuclideanSimilarity+ NearestNUserNeighborhood+ GenericUserBasedRecommender
  • userCF2: LogLikelihoodSimilarity+ NearestNUserNeighborhood+ GenericUserBasedRecommender
  • userCF3: EuclideanSimilarity+ NearestNUserNeighborhood+ GenericBooleanPrefUserBasedRecommender
  • itemCF1: EuclideanSimilarity + GenericItemBasedRecommender
  • itemCF2: LogLikelihoodSimilarity + GenericItemBasedRecommender
  • itemCF3: EuclideanSimilarity + GenericBooleanPrefItemBasedRecommender
  • slopeOne:SlopeOneRecommender
对上面的算法进行算法评估,有关于算法评估的详细解释,请参考文章:Mahout推荐算法API详解
  • 查准率:
  • 召回率(查全率):
5. 程序开发
系统架构:Mahout中推荐过滤算法支持单机算法和分步式算法两种。

单机算法: 在单机内存计算,支持多种算法推荐算法,部署运行简单,修正处理数据量有限
分步式算法: 基于Hadoop集群运行,支持有限的几种推荐算法,部署运行复杂,支持海量数据
mahout-recommend-job-architect.png

开发环境
  • Win7 64bit
  • Java 1.6.0_45
  • Maven3
  • Eclipse Juno Service Release 2
  • Mahout-0.8
  • Hadoop-1.1.2
开发环境mahout版本为0.8。 请参考文章:用Maven构建Mahout项目

新建Java类:
  • BookEvaluator.java, 选出“评估推荐器”验证得分较高的算法
  • BookResult.java, 对指定数量的结果人工比较
  • BookFilterGenderResult.java,只保留男性用户的图书列表
1). BookEvaluator.java, 选出“评估推荐器”验证得分较高的算法

源代码
  1. package org.conan.mymahout.recommendation.book;
  2. import java.io.IOException;
  3. import org.apache.mahout.cf.taste.common.TasteException;
  4. import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
  5. import org.apache.mahout.cf.taste.model.DataModel;
  6. import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
  7. import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
  8. import org.apache.mahout.cf.taste.similarity.UserSimilarity;
  9. public class BookEvaluator {
  10.     final static int NEIGHBORHOOD_NUM = 2;
  11.     final static int RECOMMENDER_NUM = 3;
  12.     public static void main(String[] args) throws TasteException, IOException {
  13.         String file = "datafile/book/rating.csv";
  14.         DataModel dataModel = RecommendFactory.buildDataModel(file);
  15.         userEuclidean(dataModel);
  16.         userLoglikelihood(dataModel);
  17.         userEuclideanNoPref(dataModel);
  18.         itemEuclidean(dataModel);
  19.         itemLoglikelihood(dataModel);
  20.         itemEuclideanNoPref(dataModel);
  21.         slopeOne(dataModel);
  22.     }
  23.     public static RecommenderBuilder userEuclidean(DataModel dataModel) throws TasteException, IOException {
  24.         System.out.println("userEuclidean");
  25.         UserSimilarity userSimilarity = RecommendFactory.userSimilarity(RecommendFactory.SIMILARITY.EUCLIDEAN, dataModel);
  26.         UserNeighborhood userNeighborhood = RecommendFactory.userNeighborhood(RecommendFactory.NEIGHBORHOOD.NEAREST, userSimilarity, dataModel, NEIGHBORHOOD_NUM);
  27.         RecommenderBuilder recommenderBuilder = RecommendFactory.userRecommender(userSimilarity, userNeighborhood, true);
  28.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  29.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  30.         return recommenderBuilder;
  31.     }
  32.    
  33.     public static RecommenderBuilder userLoglikelihood(DataModel dataModel) throws TasteException, IOException {
  34.         System.out.println("userLoglikelihood");
  35.         UserSimilarity userSimilarity = RecommendFactory.userSimilarity(RecommendFactory.SIMILARITY.LOGLIKELIHOOD, dataModel);
  36.         UserNeighborhood userNeighborhood = RecommendFactory.userNeighborhood(RecommendFactory.NEIGHBORHOOD.NEAREST, userSimilarity, dataModel, NEIGHBORHOOD_NUM);
  37.         RecommenderBuilder recommenderBuilder = RecommendFactory.userRecommender(userSimilarity, userNeighborhood, true);
  38.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  39.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  40.         return recommenderBuilder;
  41.     }
  42.    
  43.     public static RecommenderBuilder userEuclideanNoPref(DataModel dataModel) throws TasteException, IOException {
  44.         System.out.println("userEuclideanNoPref");
  45.         UserSimilarity userSimilarity = RecommendFactory.userSimilarity(RecommendFactory.SIMILARITY.EUCLIDEAN, dataModel);
  46.         UserNeighborhood userNeighborhood = RecommendFactory.userNeighborhood(RecommendFactory.NEIGHBORHOOD.NEAREST, userSimilarity, dataModel, NEIGHBORHOOD_NUM);
  47.         RecommenderBuilder recommenderBuilder = RecommendFactory.userRecommender(userSimilarity, userNeighborhood, false);
  48.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  49.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  50.         return recommenderBuilder;
  51.     }
  52.     public static RecommenderBuilder itemEuclidean(DataModel dataModel) throws TasteException, IOException {
  53.         System.out.println("itemEuclidean");
  54.         ItemSimilarity itemSimilarity = RecommendFactory.itemSimilarity(RecommendFactory.SIMILARITY.EUCLIDEAN, dataModel);
  55.         RecommenderBuilder recommenderBuilder = RecommendFactory.itemRecommender(itemSimilarity, true);
  56.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  57.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  58.         return recommenderBuilder;
  59.     }
  60.     public static RecommenderBuilder itemLoglikelihood(DataModel dataModel) throws TasteException, IOException {
  61.         System.out.println("itemLoglikelihood");
  62.         ItemSimilarity itemSimilarity = RecommendFactory.itemSimilarity(RecommendFactory.SIMILARITY.LOGLIKELIHOOD, dataModel);
  63.         RecommenderBuilder recommenderBuilder = RecommendFactory.itemRecommender(itemSimilarity, true);
  64.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  65.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  66.         return recommenderBuilder;
  67.     }
  68.    
  69.     public static RecommenderBuilder itemEuclideanNoPref(DataModel dataModel) throws TasteException, IOException {
  70.         System.out.println("itemEuclideanNoPref");
  71.         ItemSimilarity itemSimilarity = RecommendFactory.itemSimilarity(RecommendFactory.SIMILARITY.EUCLIDEAN, dataModel);
  72.         RecommenderBuilder recommenderBuilder = RecommendFactory.itemRecommender(itemSimilarity, false);
  73.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  74.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  75.         return recommenderBuilder;
  76.     }
  77.     public static RecommenderBuilder slopeOne(DataModel dataModel) throws TasteException, IOException {
  78.         System.out.println("slopeOne");
  79.         RecommenderBuilder recommenderBuilder = RecommendFactory.slopeOneRecommender();
  80.         RecommendFactory.evaluate(RecommendFactory.EVALUATOR.AVERAGE_ABSOLUTE_DIFFERENCE, recommenderBuilder, null, dataModel, 0.7);
  81.         RecommendFactory.statsEvaluator(recommenderBuilder, null, dataModel, 2);
  82.         return recommenderBuilder;
  83.     }
  84. }
复制代码
控制台输出:
  1. userEuclidean
  2. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:0.33333325386047363
  3. Recommender IR Evaluator: [Precision:0.3010752688172043,Recall:0.08542713567839195]
  4. userLoglikelihood
  5. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:2.5245869159698486
  6. Recommender IR Evaluator: [Precision:0.11764705882352945,Recall:0.017587939698492466]
  7. userEuclideanNoPref
  8. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:4.288461538461536
  9. Recommender IR Evaluator: [Precision:0.09045226130653267,Recall:0.09296482412060306]
  10. itemEuclidean
  11. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:1.408880928305655
  12. Recommender IR Evaluator: [Precision:0.0,Recall:0.0]
  13. itemLoglikelihood
  14. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:2.448554412835434
  15. Recommender IR Evaluator: [Precision:0.0,Recall:0.0]
  16. itemEuclideanNoPref
  17. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:2.5665197873957957
  18. Recommender IR Evaluator: [Precision:0.6005025125628134,Recall:0.6055276381909548]
  19. slopeOne
  20. AVERAGE_ABSOLUTE_DIFFERENCE Evaluater Score:2.6893078179405814
  21. Recommender IR Evaluator: [Precision:0.0,Recall:0.0]
复制代码
可视化“评估推荐器”输出:
推荐的结果的平均距离
difference.png

推荐器的评分
evaluator.png
只有itemEuclideanNoPref算法评估的结果是非常好的,其他算法的结果都不太好。
2). BookResult.java, 对指定数量的结果人工比较

为得到差异化结果,我们分别取4个算法:userEuclidean,itemEuclidean,userEuclideanNoPref,itemEuclideanNoPref,对推荐结果人工比较。
源代码
  1. package org.conan.mymahout.recommendation.book;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import org.apache.mahout.cf.taste.common.TasteException;
  5. import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
  6. import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
  7. import org.apache.mahout.cf.taste.model.DataModel;
  8. import org.apache.mahout.cf.taste.recommender.RecommendedItem;
  9. public class BookResult {
  10.     final static int NEIGHBORHOOD_NUM = 2;
  11.     final static int RECOMMENDER_NUM = 3;
  12.     public static void main(String[] args) throws TasteException, IOException {
  13.         String file = "datafile/book/rating.csv";
  14.         DataModel dataModel = RecommendFactory.buildDataModel(file);
  15.         RecommenderBuilder rb1 = BookEvaluator.userEuclidean(dataModel);
  16.         RecommenderBuilder rb2 = BookEvaluator.itemEuclidean(dataModel);
  17.         RecommenderBuilder rb3 = BookEvaluator.userEuclideanNoPref(dataModel);
  18.         RecommenderBuilder rb4 = BookEvaluator.itemEuclideanNoPref(dataModel);
  19.         
  20.         LongPrimitiveIterator iter = dataModel.getUserIDs();
  21.         while (iter.hasNext()) {
  22.             long uid = iter.nextLong();
  23.             System.out.print("userEuclidean       =>");
  24.             result(uid, rb1, dataModel);
  25.             System.out.print("itemEuclidean       =>");
  26.             result(uid, rb2, dataModel);
  27.             System.out.print("userEuclideanNoPref =>");
  28.             result(uid, rb3, dataModel);
  29.             System.out.print("itemEuclideanNoPref =>");
  30.             result(uid, rb4, dataModel);
  31.         }
  32.     }
  33.     public static void result(long uid, RecommenderBuilder recommenderBuilder, DataModel dataModel) throws TasteException {
  34.         List list = recommenderBuilder.buildRecommender(dataModel).recommend(uid, RECOMMENDER_NUM);
  35.         RecommendFactory.showItems(uid, list, false);
  36.     }
  37. }
复制代码
控制台输出:只截取部分结果
  1. ...
  2. userEuclidean       =>uid:63,
  3. itemEuclidean       =>uid:63,(984,9.000000)(690,9.000000)(943,8.875000)
  4. userEuclideanNoPref =>uid:63,(4,1.000000)(723,1.000000)(300,1.000000)
  5. itemEuclideanNoPref =>uid:63,(867,3.791667)(947,3.083333)(28,2.750000)
  6. userEuclidean       =>uid:64,
  7. itemEuclidean       =>uid:64,(368,8.615385)(714,8.200000)(290,8.142858)
  8. userEuclideanNoPref =>uid:64,(860,1.000000)(490,1.000000)(64,1.000000)
  9. itemEuclideanNoPref =>uid:64,(409,3.950000)(715,3.830627)(901,3.444048)
  10. userEuclidean       =>uid:65,(939,7.000000)
  11. itemEuclidean       =>uid:65,(550,9.000000)(334,9.000000)(469,9.000000)
  12. userEuclideanNoPref =>uid:65,(939,2.000000)(185,1.000000)(736,1.000000)
  13. itemEuclideanNoPref =>uid:65,(666,4.166667)(96,3.093931)(345,2.958333)
  14. userEuclidean       =>uid:66,
  15. itemEuclidean       =>uid:66,(971,9.900000)(656,9.600000)(918,9.577709)
  16. userEuclideanNoPref =>uid:66,(6,1.000000)(492,1.000000)(676,1.000000)
  17. itemEuclideanNoPref =>uid:66,(185,3.650000)(533,3.617307)(172,3.500000)
  18. userEuclidean       =>uid:67,
  19. itemEuclidean       =>uid:67,(663,9.700000)(987,9.625000)(486,9.600000)
  20. userEuclideanNoPref =>uid:67,(732,1.000000)(828,1.000000)(113,1.000000)
  21. itemEuclideanNoPref =>uid:67,(724,3.000000)(279,2.950000)(890,2.750000)
  22. ...
复制代码
我们查看uid=65的用户推荐信息:
查看user.csv数据集
  1. > user[65,]
  2. userid gender age
  3. 65     65      M  14
复制代码
用户65,男性,14岁。
以itemEuclideanNoPref的算法的推荐结果,查看bookid=666的图书评分情况
  1. > rating[which(rating$bookid==666),]
  2. userid bookid pref
  3. 646      44    666   10
  4. 1327     89    666    7
  5. 2470    165    666    3
  6. 2697    179    666    7
复制代码
发现有4个用户对666的图书评分,查看这4个用户的属性数据
  1. > user[c(44,89,165,179),]
  2. userid gender age
  3. 44      44      F  76
  4. 89      89      M  40
  5. 165    165      F  59
  6. 179    179      F  68
复制代码
这4个用户,3女1男。

我们假设男性和男性有相同的图书兴趣,女性和女性有相同的图书偏好。因为用户65是男性,所以我们接下来排除女性的评分者,只保留男性评分者的评分记录。

3). BookFilterGenderResult.java,只保留男性用户的图书列表

源代码
  1. package org.conan.mymahout.recommendation.book;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import org.apache.mahout.cf.taste.common.TasteException;
  10. import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
  11. import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
  12. import org.apache.mahout.cf.taste.model.DataModel;
  13. import org.apache.mahout.cf.taste.recommender.IDRescorer;
  14. import org.apache.mahout.cf.taste.recommender.RecommendedItem;
  15. public class BookFilterGenderResult {
  16.     final static int NEIGHBORHOOD_NUM = 2;
  17.     final static int RECOMMENDER_NUM = 3;
  18.     public static void main(String[] args) throws TasteException, IOException {
  19.         String file = "datafile/book/rating.csv";
  20.         DataModel dataModel = RecommendFactory.buildDataModel(file);
  21.         RecommenderBuilder rb1 = BookEvaluator.userEuclidean(dataModel);
  22.         RecommenderBuilder rb2 = BookEvaluator.itemEuclidean(dataModel);
  23.         RecommenderBuilder rb3 = BookEvaluator.userEuclideanNoPref(dataModel);
  24.         RecommenderBuilder rb4 = BookEvaluator.itemEuclideanNoPref(dataModel);
  25.         
  26.         long uid = 65;
  27.         System.out.print("userEuclidean       =>");
  28.         filterGender(uid, rb1, dataModel);
  29.         System.out.print("itemEuclidean       =>");
  30.         filterGender(uid, rb2, dataModel);
  31.         System.out.print("userEuclideanNoPref =>");
  32.         filterGender(uid, rb3, dataModel);
  33.         System.out.print("itemEuclideanNoPref =>");
  34.         filterGender(uid, rb4, dataModel);
  35.     }
  36.     /**
  37.      * 对用户性别进行过滤
  38.      */
  39.     public static void filterGender(long uid, RecommenderBuilder recommenderBuilder, DataModel dataModel) throws TasteException, IOException {
  40.         Set userids = getMale("datafile/book/user.csv");
  41.         //计算男性用户打分过的图书
  42.         Set bookids = new HashSet();
  43.         for (long uids : userids) {
  44.             LongPrimitiveIterator iter = dataModel.getItemIDsFromUser(uids).iterator();
  45.             while (iter.hasNext()) {
  46.                 long bookid = iter.next();
  47.                 bookids.add(bookid);
  48.             }
  49.         }
  50.         IDRescorer rescorer = new FilterRescorer(bookids);
  51.         List list = recommenderBuilder.buildRecommender(dataModel).recommend(uid, RECOMMENDER_NUM, rescorer);
  52.         RecommendFactory.showItems(uid, list, false);
  53.     }
  54.     /**
  55.      * 获得男性用户ID
  56.      */
  57.     public static Set getMale(String file) throws IOException {
  58.         BufferedReader br = new BufferedReader(new FileReader(new File(file)));
  59.         Set userids = new HashSet();
  60.         String s = null;
  61.         while ((s = br.readLine()) != null) {
  62.             String[] cols = s.split(",");
  63.             if (cols[1].equals("M")) {// 判断男性用户
  64.                 userids.add(Long.parseLong(cols[0]));
  65.             }
  66.         }
  67.         br.close();
  68.         return userids;
  69.     }
  70. }
  71. /**
  72. * 对结果重计算
  73. */
  74. class FilterRescorer implements IDRescorer {
  75.     final private Set userids;
  76.     public FilterRescorer(Set userids) {
  77.         this.userids = userids;
  78.     }
  79.     @Override
  80.     public double rescore(long id, double originalScore) {
  81.         return isFiltered(id) ? Double.NaN : originalScore;
  82.     }
  83.     @Override
  84.     public boolean isFiltered(long id) {
  85.         return userids.contains(id);
  86.     }
  87. }
复制代码
控制台输出:
  1. userEuclidean       =>uid:65,
  2. itemEuclidean       =>uid:65,(784,8.090909)(276,8.000000)(476,7.666667)
  3. userEuclideanNoPref =>uid:65,
  4. itemEuclideanNoPref =>uid:65,(887,2.250000)(356,2.166667)(430,1.866667)
复制代码
我们发现,由于只保留男性的评分记录,数据量就变得比较少了,基于用户的协同过滤算法,已经没有输出的结果了。基于物品的协同过滤算法,结果集也有所变化。
对于itemEuclideanNoPref算法,输出排名第一条为ID为887的图书。
我再进一步向下追踪:查询哪些用户对图书887进行了打分。
  1. > rating[which(rating$bookid==887),]
  2. userid bookid pref
  3. 1280     85    887    2
  4. 1743    119    887    8
  5. 2757    184    887    4
  6. 2791    186    887    5
复制代码
有4个用户对图书887评分,再分别查看这个用户的属性
  1. > user[c(85,119,184,186),]
  2. userid gender age
  3. 85      85      F  31
  4. 119    119      F  49
  5. 184    184      M  27
  6. 186    186      M  35
复制代码
其中2男,2女。由于我们的算法,已经排除了女性的评分,我们可以推断图书887的推荐应该来自于2个男性的评分者的推荐。
分别计算用户65,与用户184和用户186的评分的图书交集。
  1. rat65<-rating[which(rating$userid==65),]
  2. rat184<-rating[which(rating$userid==184),]
  3. rat186<-rating[which(rating$userid==186),]
  4. > intersect(rat65$bookid ,rat184$bookid)
  5. integer(0)
  6. > intersect(rat65$bookid ,rat186$bookid)
  7. [1]  65 375
复制代码
最后发现,用户65与用户186都给图书65和图书375打过分。我们再打分出用户186的评分记录。
  1. > rat186
  2. userid bookid pref
  3. 2790    186     65    7
  4. 2791    186    887    5
  5. 2792    186    529    3
  6. 2793    186    375    6
  7. 2794    186    566    7
  8. 2795    186    169    4
  9. 2796    186    907    1
  10. 2797    186    821    2
  11. 2798    186    720    5
  12. 2799    186    642    5
  13. 2800    186    137    3
  14. 2801    186    744    1
  15. 2802    186    896    2
  16. 2803    186    156    6
  17. 2804    186    392    3
  18. 2805    186    386    3
  19. 2806    186    901    7
  20. 2807    186     69    6
  21. 2808    186    845    6
  22. 2809    186    998    3
复制代码
用户186,还给图书887打过分,所以对于给65用户推荐图书887,是合理的。




本帖被以下淘专辑推荐:

已有(4)人评论

跳转到指定楼层
anyhuayong 发表于 2014-11-6 08:31:32
不错的资源,学习了
回复

使用道具 举报

buildhappy 发表于 2014-11-6 08:35:01
xuexi               
回复

使用道具 举报

kanwei163 发表于 2014-11-6 10:14:24
不错,非常不错
回复

使用道具 举报

loreting 发表于 2014-11-12 11:17:04
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条