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

本文通过Java代码展示了如何使用Mahout库实现基于用户协同过滤的推荐算法,包括基于布尔喜好值、数字喜好值以及从MySQL数据模型的推荐。通过对测试数据的应用,解释了如何为用户生成个性化的推荐列表。
摘要由CSDN通过智能技术生成

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

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

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

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

以及jdbc dada model.

测试代码:/**

*

* 基于用户近邻协同过滤推荐算法,

* 本文目的:针对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 recItemList = rec.recommend(1, 2);

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 recItemList = rec.recommend(1, 3);

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 recommendations = recommender.recommend(1, 3);

for (RecommendedItem recommendation : recommendations) {

System.out.println(recommendation);

}

}

public static FastByIDMap toDataMap(DataModel dataModel) throws TasteException {

FastByIDMap data = new FastByIDMap(dataModel.getNumUsers());

LongPrimitiveIterator it = dataModel.getUserIDs();

while (it.hasNext()) {

long userID = it.nextLong();

data.put(userID, dataModel.getItemIDsFromUser(userID));

}

return data;

}

}

测试数据: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 依赖:

org.apache.mahout

mahout-core

0.9

org.apache.mahout

mahout-math

0.9

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值