Mahout基于协同过滤(CF)的用户推荐

一、Mahout推荐算法简介

Mahout算法框架自带的推荐器有下面这些:

  • GenericUserBasedRecommender:基于用户的推荐器,用户数量少时速度快;
  • GenericItemBasedRecommender:基于商品推荐器,商品数量少时速度快,尤其当外部提供了商品相似度数据后效率更好;
  • SlopeOneRecommender:基于slope-one算法的推荐器,在线推荐或更新较快,需要事先大量预处理运算,物品数量少时较好;
  • SVDRecommender:奇异值分解,推荐效果较好,但之前需要大量预处理运算;
  • KnnRecommender:基于k近邻算法(KNN),适合于物品数量较小时;
  • TreeClusteringRecommender:基于聚类的推荐器,在线推荐较快,之前需要大量预处理运算,用户数量较少时效果好;
    Mahout最常用的三个推荐器是上述的前三个,本文的实例仅“基于用户的推荐器”做个实验,其实大体原理都差不多。
二、基于协同过滤(CF)模型的用户推荐

          Mahout里自带的基本CF模型原理如下:GenericUserBasedRecommender是基于用户(user-based)的简单推荐器实现类,推荐主要参照传入的DataModel和UserNeighborhood,总体是三个步骤:
          (1) 从UserNeighborhood获取当前用户Ui最相似的K个用户集合{U1, U2, …Uk};
          (2) 从这K个用户集合排除Ui的偏好商品,剩下的Item集合为{Item0, Item1, …Itemm};
          (3) 对Item集合里每个Itemj计算Ui可能偏好程度值pref(Ui, Itemj),并把Item按此数值从高到低排序,前N个item推荐给用户Ui。

三、源码
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
import java.util.List;
import org.apache.mahout.cf.taste.common.TasteException;
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.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import com.mysql.MysqlDataSource;
/**
* mahout基于协同过滤(CF)的推荐
*/
public class Mahout {
public static void main(String[] args) throws TasteException {
//(1)----连接数据库部分
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setUser("admin");
dataSource.setPassword("admin");
dataSource.setDatabaseName("mahoutDB");
//(2)----使用MySQLJDBCDataModel数据源读取MySQL里的数据
JDBCDataModel dataModel = new MySQLJDBCDataModel(dataSource, "table1", "userId", "itemId", "preference", "date");
//(3)----数据模型部分
//把MySQLJDBCDataModel对象赋值给DataModel
DataModel model = dataModel;
//用户相似度UserSimilarity:包含相似性度量和邻居参数
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
//相邻用户UserNeighborhood
UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);
//一旦确定相邻用户,一个普通的user-based推荐器被构建,构建一个GenericUserBasedRecommender推荐器需要数据源DataModel,用户相似性UserSimilarity,相邻用户相似度UserNeighborhood
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
//向用户1推荐2个商品
List<RecommandedItem> recommendations = recommender.recommend(1, 2);
for(RecommendedItem recommendation : recommendations){
//输出推荐结果
System.out.println(recommendation);
}
}
}