上篇文章介绍了协同过滤的安装与配置,这篇找了几个协同过滤的简单例子,看一下
Mahout给我们提供的强大的协同过滤算法。需要新建一个基于Maven的工程,下面是
pom.xml需要导入的包。
4.0.0 mahouttest mahouttest 0.0.1-SNAPSHOT jar mahouttest http://maven.apache.org UTF-8 junit junit 4.8.1 test org.apache.mahout mahout-core 0.8-SNAPSHOT jar compile
这里我们导入的是最新的Mahout包,需要在本地的maven库中安装好。
首先我们需要准备好测试的数据,我们就用《Mahout in action》中的例子:
1,101,51,102,31,103,2.52,101,22,102,2.52,103,52,104,23,101,2.53,104,43,105,4.53,107,54,101,54,103,34,104,4.54,106,45,101,45,102,35,103,25,104,45,105,3.55,106,4
具体对应的关系图如下:
下面我们用Mahout中三种不同的推荐代码来执行以下刚才给出的数据,看看Mahout中的推荐接口是
如何使用的。
1. 基于用户的协同推荐的代码:
DataModel model =new FileDataModel(new File("data/intro.csv"));UserSimilarity similarity =new PearsonCorrelationSimilarity(model);UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model);Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity);Listrecommendations =recommender.recommend(1, 1);for(RecommendedItem recommendation :recommendations){ System.out.println(recommendation);}
执行后的结果是:RecommendedItem[item:104, value:4.257081]
2. 基于Item的协同过滤的代码:
DataModel model =new FileDataModel(new File("data/intro.csv"));ItemSimilarity similarity =new PearsonCorrelationSimilarity(model);Recommender recommender= new GenericItemBasedRecommender(model,similarity);Listrecommendations =recommender.recommend(1, 1);for(RecommendedItem recommendation :recommendations){ System.out.println(recommendation);}
执行后的结果是:RecommendedItem[item:104, value:5.0]
3. SlopeOne推荐算法
DataModel model =new FileDataModel(new File("data/intro.csv"));Recommender recommender= new SlopeOneRecommender(model);Listrecommendations =recommender.recommend(1, 1);for(RecommendedItem recommendation :recommendations){ System.out.println(recommendation);}
执行结果是:RecommendedItem[item:105, value:5.75]