运行太不容易了 所以要记下来以免后面忘记了
首先是数据
vim testdata.txt
第一列为UserID ,第二列为ItemID,第三列为Preference Value 即评分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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的一个需要自己下
1
2
3
4
5
6
7
|
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
|
然后就能够运行了
结果 应该是
1
2
3
4
5
6
7
8
9
10
|
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
]
|
总结:
主要是依赖问题解决用了比较长的时间
我用的ubuntu 下开发 所以在找包的时候 直接用 find | grep *** 在mahout 的安装目录下去寻找包
参考自:http://blog.csdn.net/aidayei/article/details/6626699
本文转自 拖鞋崽 51CTO博客,原文链接:http://blog.51cto.com/1992mrwang/1205282