Mahout入门-1

Mahout是什么?

Mahout是用来进行机器学习和推荐算法的一个Java框架。

一个简单的基于用户的协同过滤的示例

读取用户对电影评分的数据集,为ID为2的用户推荐3部电影。

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
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.mahout;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Main {

public static void main(String[] args) {
try {
DataModel model = new FileDataModel(new File("resources/dataset.csv"));

UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.10, similarity, model);

UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);

List<RecommendedItem> recommendations = recommender.recommend(2, 3);
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
} catch (IOException e) {
e.printStackTrace();
} catch (TasteException e) {
e.printStackTrace();
}
}
}

实践后感

好久没有碰Java系列,今天又是看了Mahout的示例,看起来很简单,结果自己试了下,发现要调整IDE,配置Maven,包括数据集文件路径的设置都要注意。

引用

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×