1. Mahout推荐引擎概况
Mahout的推荐引擎,要从org.apache.mahout.cf.taste包说起。
packages的说明:
从上面的package情况,我可以粗略地看出推荐引擎分为5个主要部分组成:数据模型,相似度算法,近邻算法,推荐算法,算法评分器。
从数据处理能力上,算法可以分为:单机内存算法,基于hadoop的分步式算法。
下面我们将基于单机内存算法,研究Mahout的推荐引擎的结构。
以UserCF的推荐算法为例,官方建议我们的开发过程:
图片摘自Mahout in Action
从上图中我们可以看到,算法是被模块化的,通过1,2,3,4的过程进行方法调用。
程序代码:
public class UserCF {
final static int NEIGHBORHOOD_NUM = 2;
final static int RECOMMENDER_NUM = 3;
public static void main(String[] args) throws IOException, TasteException {
String file = "datafile/item.csv";
DataModel model = new FileDataModel(new File(file));
UserSimilarity user = new EuclideanDistanceSimilarity(model);
NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model);
Recommender r = new GenericUserBasedRecommender(model, neighbor, user);
LongPrimitiveIterator iter = model.getUserIDs();
while (iter.hasNext()) {
long uid = iter.nextLong();
List list = r.recommend(uid, RECOMMENDER_NUM);
System.out.printf("uid:%s", uid);
for (RecommendedItem ritem : list) {
System.out.printf("(%s,%f)", ritem.getItemID(), ritem.getValue());
}
System.out.println();
}
}
}
我们调用算法的程序,要用到4个对象:DataModel, UserSimilarity, NearestNUserNeighborhood, Recommender。
Mahout的推荐引擎的数据模型,以DataModel接口为父类。
通过“策略模式”匹配不同的数据源,支持File, JDBC(MySQL, PostgreSQL), NoSQL(Cassandra, HBase, MongoDB)。
注:NoSQL的实现在mahout-integration-0.8.jar中。
数据格式支持2种:
相似度算法分为2种
1). 基于用户(UserCF)的相似度算法
计算用户的相似矩阵,可以通过上图中几种算法。
2). 基于物品(ItemCF)的相似度算法
计算物品的相似矩阵,可以通过上图中几种算法。
相似度算法介绍,摘自:http://www.cnblogs.com/dlts26/archive/2012/06/20/2555772.html
近邻算法只对于UserCF适用,通过近邻算法给相似的用户进行排序,选出前N个最相似的,作为最终推荐的参考的用户。
近邻算法分为2种:
推荐算法是以Recommender作为基础的父类,关于推荐算法的详细介绍,请参考文章:Mahout推荐算法API详解
转载请注明出处:
http://blog.fens.me/mahout-recommend-engine/