-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecommendUtils.java
More file actions
62 lines (52 loc) · 2.65 KB
/
RecommendUtils.java
File metadata and controls
62 lines (52 loc) · 2.65 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package utils;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.IRStatistics;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.model.jdbc.ConnectionPoolDataSource;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
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.model.JDBCDataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class RecommendUtils {
public static void evaluate() throws Exception{
ConnectionPoolDataSource connectionPool = new ConnectionPoolDataSource(getRecommendDateSource());
JDBCDataModel model = new MySQLJDBCDataModel(connectionPool, "userperference", "student_Id", "competition_Id", "perferences", "time");
//创建评估器
RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
//构建推荐引擎
RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
public Recommender buildRecommender(DataModel dataModel)
throws TasteException {
// TODO Auto-generated method stub
UserSimilarity similarity = new PearsonCorrelationSimilarity(
dataModel);
UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,
similarity, dataModel);
return new GenericUserBasedRecommender(dataModel, neighborhood,
similarity);
}
};
int at = 1;// 评估推荐两个ITEM时的查准率和查全率
//开始评估查准率和查全率
IRStatistics statistics = evaluator.evaluate(recommenderBuilder, null,model, null, at,GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD,
1.0);
System.out.println("precision at " + at + " 查准率:"+ statistics.getPrecision());
System.out.println("recall at " + at + " 查全率:" + statistics.getRecall());
}
public static MysqlDataSource getRecommendDateSource(){
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setUser("root");
dataSource.setPassword("mysql");
dataSource.setDatabaseName("dcms");
return dataSource;
}
}