本文目的: 介绍一种常见推荐算法(用户协同过滤)的使用。
应用场景: 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<RecommendedItem> 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<RecommendedItem> 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<RecommendedItem> recommendations = recommender.recommend(1, 3);
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,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 依赖:
<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>
转载于:https://blog.51cto.com/now51jq/1652239