Eclipse下mahout实现推荐的简单实例

来源:互联网 发布:牛股王软件下载ios 编辑:程序博客网 时间:2024/06/04 19:17

数据准备:test.txt

第一列为UserID ,第二列为ItemID,第三列为Preference Value 即评分

[plain] view plaincopyprint?
  1. 1,101,5  
  2. 1,102,3  
  3. 1,103,2.5  
  4. 2,101,2  
  5. 2,102,2.5  
  6. 2,103,5  
  7. 2,104,2  
  8. 3,101,2.5  
  9. 3,104,4  
  10. 3,105,4.5  
  11. 3,107,5  
  12. 4,101,5  
  13. 4,103,3  
  14. 4,104,4.5  
  15. 4,106,4  
  16. 5,101,4  
  17. 5,102,3  
  18. 5,103,2  
  19. 5,104,4  
  20. 5,105,3.5  
  21. 5,106,4  
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

eclipse中新建一个java工程,然后在src目录下新建一个类RecommenderIntro (这里只是做测试,就放在默认包下,不新建包了),代码如下:

[java] view plaincopyprint?
  1. import org.apache.mahout.cf.taste.impl.model.file.*;  
  2. import org.apache.mahout.cf.taste.impl.neighborhood.*;  
  3. import org.apache.mahout.cf.taste.impl.recommender.*;  
  4. import org.apache.mahout.cf.taste.impl.similarity.*;  
  5. import org.apache.mahout.cf.taste.model.*;  
  6. import org.apache.mahout.cf.taste.neighborhood.*;  
  7. import org.apache.mahout.cf.taste.recommender.*;  
  8. import org.apache.mahout.cf.taste.similarity.*;  
  9. import java.io.*;  
  10. import java.util.*;  
  11. public class RecommenderIntro {  
  12.     private RecommenderIntro(){};  
  13.       
  14.     public static void main (String args[])throws Exception{  
  15.                 // step:1 构建模型 2 计算相似度 3 查找k紧邻 4 构造推荐引擎  
  16.         DataModel  model =new FileDataModel(new File("/home/test/test-in/test.txt"));//文件名一定要是绝对路径  
  17.         UserSimilarity similarity =new PearsonCorrelationSimilarity(model);  
  18.         UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model);  
  19.         Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity);  
  20.         List<RecommendedItem> recommendations =recommender.recommend(12);//为用户1推荐两个ItemID  
  21.         for(RecommendedItem recommendation :recommendations){  
  22.             System.out.println(recommendation);  
  23.         }  
  24.           
  25.     }  
  26. }  
import org.apache.mahout.cf.taste.impl.model.file.*;import org.apache.mahout.cf.taste.impl.neighborhood.*;import org.apache.mahout.cf.taste.impl.recommender.*;import org.apache.mahout.cf.taste.impl.similarity.*;import org.apache.mahout.cf.taste.model.*;import org.apache.mahout.cf.taste.neighborhood.*;import org.apache.mahout.cf.taste.recommender.*;import org.apache.mahout.cf.taste.similarity.*;import java.io.*;import java.util.*;public class RecommenderIntro {private RecommenderIntro(){};public static void main (String args[])throws Exception{                // step:1 构建模型 2 计算相似度 3 查找k紧邻 4 构造推荐引擎DataModel  model =new FileDataModel(new File("/home/test/test-in/test.txt"));//文件名一定要是绝对路径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, 2);//为用户1推荐两个ItemIDfor(RecommendedItem recommendation :recommendations){System.out.println(recommendation);}}}

此时肯定会报错,因为还没有导入任何相关Jar包,将mahout-core-0.4.jar导到工程中,此时代码不会显示有错了

然后Run As 选择Java Application,单机运行(当然集群如果成功启动的话,也可以在集群上运行的)。此时在运行后,会相继报一些包不存在,根据提示,依次将包导到工程中。我在测试的过程中,先后导入了slf4j-api-1.6.0.jar,slf4j-jcl-1.6.0.jar,google-collection-1.0-rc2.jar,commons-logging-1.1.1.jar,mahout-math-0.4.jar,uncommons-maths-1.2.jar,这些包都可以在mahout安装目录下的lib目录下找到。经过一步步差包导包(控制台报什么包不存在,我就找到相应包导入)调试后,最后运行成功,结果如下:

RecommendedItem[item:104, value:4.257081]

RecommendedItem[item:106, value:4.0]

结果分析:找到用户1的相似用户2和用户5,用户2和用户5共同有104,所以推荐分数高,但我不知道这个分数是怎么算出来的?用户2中没有其它可以推荐,然后用户5中还有105和106可以推荐,而106的评分比105高,所以第二个结果是106,不知道理解的对不对?只是自己猜测的,有知道其中内幕的和知道推荐分数怎么算的,请留言分享一下,开源是促进相互学习的好方法,呵呵

然后看了一下mahout的taste工作原理,我将mahout安装目录下的mahout-taste-webapp-0.4.war放在tomcat安装目录下的webapps中,然后启动tomcat,然后在浏览器中输入:http://localhost:8080/mahout-taste-webapp-0.4/RecommenderService.jws ,可以看到Taste 提供的Web 服务访问接口(即WebServices服务),但却不能根据用户ID查询,估计应该还要下载相应的数据集和jar包,详见 基于 Apache Mahout 构建社会化推荐引擎

参考资料:

1.http://www.ibm.com/developerworks/cn/java/j-mahout/

2.http://blog.csdn.net/zhzhl202/article/details/6316570

3.http://www.ibm.com/developerworks/cn/java/j-lo-mahout/

 

原创粉丝点击