推荐引擎之Mahout 基于用户协同过滤算法的使用

来源:互联网 发布:软件服务公司会计分录 编辑:程序博客网 时间:2024/06/05 11:21

本文目的: 介绍一种常见推荐算法(用户协同过滤)的使用。

应用场景: XXX项目运行一段时间后,系统中将会存在很多视频信息, 而通常 APP 给用户推送的消息(1-3条/每天),

那么这就需要我们根据用户的行为特征,进行更为有效的推送。 

工具介绍:mahout 协同过滤算法的使用, 下面将分别介绍 基于布尔型喜好值,数字喜好值, 

以及jdbc dada model.

测试代码:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 
 * 基于用户近邻协同过滤推荐算法,
 * 本文目的:针对xxx后续广告推荐算法,提供一些算法模型的参考
 
 * @版权所有:来谊金融 版权所有 (c) 2015
 * @author feihu.wang
 * @version Revision 1.0.0
 * @see:
 * @创建日期:2015年5月18日
 * @功能说明:
 *
 */
public class CfTest {
    public static void main(String[] args) {
 
        try {
            testBooleanPreference();
            System.out.println("-------------------");
            testMysqlDataModel();
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    /**
     
     * 基于带喜好值的协同过滤算法.
     
     * @throws Exception
     * @author feihu.wang
     * @since 2015年5月29日
     */
    public static void test() throws Exception {
 
        DataModel model = new FileDataModel(new File("E:\\projects\\code\\mahout_test\\pref.csv"));
 
        //用户相识度 :皮尔森相关性相视度
        //UserSimilarity sim = new PearsonCorrelationSimilarity(model);
 
        //用户相识度 :欧式距离
        UserSimilarity sim = new EuclideanDistanceSimilarity(model);
 
        // 最近邻算法
        UserNeighborhood nbh = new NearestNUserNeighborhood(2, sim, model);
 
        // 生成推荐引擎  : 基于用户的协同过滤算法, 
        //还有基于物品的过滤算法,mahout 下面已经有很多实现
        Recommender rec = new GenericUserBasedRecommender(model, nbh, sim);
 
        // 为用户ID(1)推荐物品(数量2个)  
        List<RecommendedItem> recItemList = rec.recommend(12);
 
        for (RecommendedItem item : recItemList) {
            System.out.println(item);
        }
    }
     
    /**
     
     * 基于布尔类型的喜好值.
     
     * @throws Exception
     * @author feihu.wang
     * @since 2015年5月29日
     */
    public static void testBooleanPreference() throws Exception {
        DataModel dataModel = new FileDataModel(new File("E:\\projects\\code\\mahout_test\\data_nopref.csv"));
 
        //用户相识度 :皮尔森相关性相视度
        //UserSimilarity sim = new PearsonCorrelationSimilarity(model);
 
        //用户相识度 :欧式距离
        UserSimilarity sim = new LogLikelihoodSimilarity(dataModel);
 
        // 最近邻算法
        UserNeighborhood nbh = new NearestNUserNeighborhood(2, sim, dataModel);
 
        // 生成推荐引擎  : 基于用户的协同过滤算法, 
        //还有基于物品的过滤算法,mahout 下面已经有很多实现
        //Recommender rec = new GenericUserBasedRecommender(model, nbh, sim);  
 
        Recommender rec = new GenericBooleanPrefUserBasedRecommender(dataModel, nbh, sim);
 
        // 为用户ID(1)推荐物品(数量2个)  
        List<RecommendedItem> recItemList = rec.recommend(13);
 
        for (RecommendedItem item : recItemList) {
            System.out.println(item);
        }
    }
 
    /**
     
     * 基于mysql 的data model.
     
     * @throws Exception
     * @author feihu.wang
     * @since 2015年5月29日
     */
    public static void testMysqlDataModel() throws Exception {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setServerName("localhost");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDatabaseName("mahout");
 
        DataModel model = new MySQLJDBCDataModel(dataSource, "prefs""USER_ID""ITEM_ID""SCORE""CREATETIME");
 
        UserSimilarity similarity = new LogLikelihoodSimilarity(model);
        UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);
 
        Recommender recommender = new GenericBooleanPrefUserBasedRecommender(model, neighborhood, similarity);
 
        List<RecommendedItem> recommendations = recommender.recommend(13);
        for (RecommendedItem recommendation : recommendations) {
            System.out.println(recommendation);
        }
    }
     
 
 
    public static FastByIDMap<FastIDSet> toDataMap(DataModel dataModel) throws TasteException {
        FastByIDMap<FastIDSet> data = new FastByIDMap<FastIDSet>(dataModel.getNumUsers());
        LongPrimitiveIterator it = dataModel.getUserIDs();
        while (it.hasNext()) {
            long userID = it.nextLong();
            data.put(userID, dataModel.getItemIDsFromUser(userID));
        }
        return data;
    }
 
 
}

测试数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1,101,5.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0

 

pom 依赖:

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-math</artifactId>
<version>0.9</version>
</dependency>
0 0