Eclipse 下mahout的配置与使用

mahout 是一个开源的旨在为实际问题提供可伸缩性算法的软件。

官方主页:http://mahout.apache.org/

QuickStart:https://cwiki.apache.org/confluence/display/MAHOUT/Quickstart

 

目前的版本是0.4 ,此例展示了在eclipse下如何配置并应用mahout到你的程序中。

 

环境:eclipse +maven(m2eclipse)+ mahout 0.4 +jdk 1.6

 

配置:

 

Step 1: 

       在eclipse中新建一个maven程序 在”Select an Archetype“ 中选择”maven-Archetype-QuickStart“ 即可。

 

 

Step 2:

     打开pom.xml 加入一些必备的jar。 

点击 pom.xml 下的dependencies选项卡,在dependencies出点击add。 在弹出的对话框中输入”mahout“ 稍等片刻就会出来很多的jar包,选择合适的mahout包,一般来说,如果做的很简单的程序,选择mahout-core 就行,如果需要分布计算则需要加入 hadoop。

 

保存pom.xml 这是程序就会自动下载你所选择的jar包。

 

实例:

 

好了我们用一个例子来说明吧:

 

新建一个class 写入以下代码:

 

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("data/intro.csv")); UserSimilarity similarity =new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model); Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity); List recommendations =recommender.recommend(1, 2); for(RecommendedItem recommendation :recommendations){ System.out.println(recommendation); } } }  

 

运行结果: 

 

 

RecommendedItem[item:104, value:4.257081]

RecommendedItem[item:106, value:4.0]

 

 

其中intro.csv 的格式如下:

第一列为UserID ,第二列为ItemID,第三列为Preference Value 即评分 1,101,5 1,102,3 1,103,2.5 2,101,2 2,102,2.5 2,103,5 2,104,2 3,101,2.5 3,104,4 3,105,4.5 3,107,5 4,101,5 4,103,3 4,104,4.5 4,106,4 5,101,4 5,102,3 5,103,2 5,104,4 5,105,3.5 5,106,4 

 

 

Pom.xml 文件如下:

 

4.0.0 zhzhl_zju mahout 0.0.1-SNAPSHOT jar mahout http://maven.apache.org UTF-8 junit junit 3.8.1 test org.apache.mahout mahout-core 0.4 jar compile  

你可能感兴趣的:(ML,问题解决,学习笔记)