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<RecommendedItem> 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 文件如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>zhzhl_zju</groupId>
- <artifactId>mahout</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>mahout</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.mahout</groupId>
- <artifactId>mahout-core</artifactId>
- <version>0.4</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- </dependencies>
- </project>
原文地址:http://blog.csdn.net/zhzhl202/article/details/6316570