mahout0.7 示例运行纪实

运行太不容易了 所以要记下来以免后面忘记了

首先是数据

vim testdata.txt

第一列为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


新建立一个工程

导入 mahout包 mahout-core-0.7-job.jar

这样进行开发的时候就不会出现各种红叉叉  从编程心情上会更好一点

新建一个类 RecommenderIntro

代码如下

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 {                  
        DataModel model = new FileDataModel(new File(
                "intro.csv")); // step:1 构建模型
        UserSimilarity similarity = new PearsonCorrelationSimilarity(model); // 2 计算相似度
        UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,
                similarity, model); // 3 查找邻近
        Recommender recommender = new GenericUserBasedRecommender(model,
                neighborhood, similarity); // 4 构造推荐引擎
        List<RecommendedItem> recommendations = recommender.recommend(1, 2);// 为用户1推荐两个ItemID
        for (RecommendedItem recommendation : recommendations) {
            System.out.println(recommendation);
        }
    }
}

这样就能够运行了

但是会报错 原因在于没有导入它所需要的所有包

在mahout当中 只要你正确安装了都会有他所需要的包

除了google的一个需要自己下

mahout-core-0.7-job.jar
slf4j-api-1.6.1.jar
slf4j-jcl-1.6.1.jar
google-collection-1.0-rc2.jar //这个找不到 然后需要自己下载一个 guava-r09.zip 用当中的 guava-r09.jar来解决这个google依赖问题
commons-logging-1.1.1.jar
mahout-math-0.7.jar
uncommons-maths-1.2.2.jar

然后就能够运行了

结果 应该是

2013-5-21 2:45:56 org.slf4j.impl.JCLLoggerAdapter info
信息: Creating FileDataModel for file /root/input/testMahout.txt
2013-5-21 2:45:56 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-5-21 2:45:56 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-5-21 2:45:56 org.slf4j.impl.JCLLoggerAdapter info
信息: Processed 5 users
RecommendedItem[item:104, value:4.257081]
RecommendedItem[item:106, value:4.0]


031454774.png


总结:

主要是依赖问题解决用了比较长的时间

我用的ubuntu 下开发 所以在找包的时候 直接用 find | grep *** 在mahout 的安装目录下去寻找包

参考自:http://blog.csdn.net/aidayei/article/details/6626699




本文出自 “某人说我技术宅” 博客,请务必保留此出处http://1992mrwang.blog.51cto.com/3265935/1205282

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值