数据准备
intro.csv
1,101,5.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0
测试代码
下面是基于用户维度的推荐
DataModel model = new FileDataModel(new File("/home/yunpeng/test2/intro.csv"));UserSimilarity similarity = new PearsonCorrelationSimilarity(model);UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);List<RecommendedItem> recommendations = recommender.recommend(1, 1);for (RecommendedItem recommendation : recommendations) {System.out.println(recommendation);}
下面是比较两份内容之间的关联
DataModel model = new FileDataModel(new File("/home/yunpeng/test2/intro.csv"));ItemSimilarity similarity = new PearsonCorrelationSimilarity(model);ItemBasedRecommender recommender = new GenericItemBasedRecommender(model, similarity);List<RecommendedItem> recommendations = recommender.mostSimilarItems(101, 3);for (RecommendedItem recommendation : recommendations) {System.out.println(recommendation);}