使用maven搭建基于mahout的推荐算法

随笔 2016-04-26

  1.从官网下载Maven,解压到本地,并配置环境变量,过程略过(因为本文的重点不在这,相关介绍也有很多)。

  2.在eclipse中配置maven

  1.jpg

  3.新建一个Maven Project

  4.png

  4.修改项目的pom.xml,默认已经有junit依赖了,我们只需要添加:

     <dependency>
        <groupId>org.apache.mahout</groupId>
        <artifactId>mahout-core</artifactId>
        <version>0.9</version>
    </dependency>

   <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.12</version>
  </dependency>

保存后,选择Run As->Maven Clean,大约7s后,相关包下载完成。

  2.jpg

  5.新建一个UserCF类测试基于用户的协同过滤算法:

 public class UserCF {
    final static int NEIGHBORHOOD_NUM = 2;
    final static int RECOMMENDER_NUM = 3;

    public static void main(String[] args) throws IOException, TasteException {
        String path = UserCF.class.getClassLoader().getResource("").getPath();
        String file = "/data/item.csv";
        DataModel model = new FileDataModel(new File(path+file));
        UserSimilarity user = new EuclideanDistanceSimilarity(model);
        NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model);
        Recommender r = new GenericUserBasedRecommender(model, neighbor, user);
        LongPrimitiveIterator iter = model.getUserIDs();

        while (iter.hasNext()) {
            long uid = iter.nextLong();
            List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);
            System.out.printf("uid:%s", uid);
            for (RecommendedItem ritem : list) {
                System.out.printf("(%s,%f)", ritem.getItemID(), ritem.getValue());
            }
            System.out.println();
        }
    }
}

  数据为:item.csv

  6.运行输出为:

  3.jpg

  总结:maven在研究开源项目的时候的确很方便,同时要研究推荐算法,还必须对mahout源码进一步研究。


本文由 Tony 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

赏个馒头吧