eclipse 单机版 mahout collaborative demo

来源:互联网 发布:淘宝接单app软件 编辑:程序博客网 时间:2024/05/16 08:55

之前看了mahout的一些书籍,讲的是在linux系统下部署基于hadoop的分布式mahout实现,对于大规模数据来说分布式是一个不错的选择,但是对于想熟悉mahout,用它来做个demo试试的人来说就显得比较麻烦。


有一天看到网上有人将一些jar包导入,成功运行了mahout collaborative,就自己试了一下,果然是可以的。

下面讲步骤:

1.准备工作:

运行环境:jdk1.7

IDE: eclipse

官网下载:mahout-distribution-0.9.zip

链接:http://archive.apache.org/dist/mahout/0.9/

2.新建java工程

3.工程下新建lib文件夹

4.复制jar到lib下

commons-logging-1.1.3.jar

commons-math-2.1.jar
commons-math3-3.2.jar
guava-16.0.jar
jcl-over-slf4j-1.7.5.jar
log4j-1.2.17.jar
mahout-core-0.9-job.jar
mahout-core-0.9.jar
mahout-examples-0.9-job.jar
mahout-examples-0.9.jar
mahout-integration-0.9.jar
mahout-math-0.9.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar

5.全选jar,右键build path->add

6.新建类

public class FileDataModelDemo {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File  modelFile = new File("E:\\DataSet\\intro.csv");


DataModel model = new FileDataModel(modelFile);
// Build the same recommender for testing that we did last time:
RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
@Override
public Recommender buildRecommender(DataModel model) throws TasteException {
ItemSimilarity similarity = new PearsonCorrelationSimilarity(model);
return new GenericItemBasedRecommender(model, similarity);
}
};
//获取推荐结果
List<RecommendedItem> recommendations = recommenderBuilder.buildRecommender(model).recommend(3, 2);
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
//计算评分
RecommenderEvaluator evaluator =
new AverageAbsoluteDifferenceRecommenderEvaluator();
// Use 70% of the data to train; test using the other 30%.
double score = evaluator.evaluate(recommenderBuilder, null, model, 0.7, 1.0);
System.out.println(score);
//计算查全率和查准率
RecommenderIRStatsEvaluator statsEvaluator = new GenericRecommenderIRStatsEvaluator();
// Evaluate precision and recall "at 2":
IRStatistics stats = statsEvaluator.evaluate(recommenderBuilder,
null, model, null, 3,
GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD,
1.0);
System.out.println("stats.getPrecision"+stats.getPrecision());
System.out.println("stats.getRecall"+stats.getRecall());


}
}

7.样例输入数据

1,10,1.0


1,11,2.0


1,12,5.0


1,13,5.0


1,14,5.0


1,15,4.0


1,16,5.0


1,17,1.0


1,18,5.0


2,10,1.0


2,11,2.0


2,15,5.0


2,16,4.5


2,17,1.0


2,18,5.0


3,11,2.5


3,12,4.5


3,13,4.0


3,14,3.0


3,15,3.5


3,16,4.5


3,17,4.0


3,18,5.0


4,10,5.0


4,11,5.0


4,12,5.0


4,13,0.0


4,14,2.0


4,15,3.0


4,16,1.0


4,17,4.0


4,18,1.0


8.控制台输出

RecommendedItem[item:10, value:4.5468535]
1.65
stats.getPrecision0.75
stats.getRecall0.8333333333333333

1 0
原创粉丝点击