【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则
2014-05-09 09:53
429 查看
通过Mahout构建推荐系统时,如果我们需要加入某些过滤规则(比如:item的创建时间在一年以内),则需要用到IDRescorer接口,该接口源码如下:
package org.apache.mahout.cf.taste.recommender;
/**
* <p>
* A {@link Rescorer} which operates on {@code long} primitive IDs, rather than arbitrary {@link Object}s.
* This is provided since most uses of this interface in the framework take IDs (as {@code long}) as an
* argument, and so this can be used to avoid unnecessary boxing/unboxing.
* </p>
*/
public interface IDRescorer {
/**
* @param id
* ID of thing (user, item, etc.) to rescore
* @param originalScore
* original score
* @return modified score, or {@link Double#NaN} to indicate that this should be excluded entirely
*/
double rescore(long id, double originalScore);
/**
* Returns {@code true} to exclude the given thing.
*
* @param id
* ID of thing (user, item, etc.) to rescore
* @return {@code true} to exclude, {@code false} otherwise
*/
boolean isFiltered(long id);
}
该接口规定了两个必须实现的方法:
1.rescore方法
功能:定义重新评分的逻辑。根据新的规则,为指定id的item重新评分。
返回:重评后的分数
输入参数:item的id,该item原来的评分
调用该方法的方法包括:
2.isFiltered
功能:定义过滤规则。判断指定id的item,根据新的规则,是否该排除在外,返回true就是该item应该排除在结果之外。
返回:true or false
输入参数:指定的id
调用该方法的方法包括:
无论是否需要根据特定规则过滤推荐结果,都必须先创建org.apache.mahout.cf.taste.recommender.Recommender类的对象r,然后通过对象r来执行推荐方法获得针对特定id用户的推荐结果List。
当无需使用特定规则过滤推荐结果时,只需使用Recommender对象的如下方法获得推荐结果:
/**
* @param userID
* user for which recommendations are to be computed
* @param howMany
* desired number of recommendations
* @return {@link List} of recommended {@link RecommendedItem}s, ordered from most strongly recommend to
* least
* @throws TasteException
* if an error occurs while accessing the {@link DataModel}
*/
List<RecommendedItem> recommend(long userID, int howMany) throws TasteException;
当需要根据特定规则过滤推荐结果时,需使用Recommender对象的如下方法获得推荐结果:
/**
* @param userID
* user for which recommendations are to be computed
* @param howMany
* desired number of recommendations
* @param rescorer
* rescoring function to apply before final list of recommendations is determined
* @return {@link List} of recommended {@link RecommendedItem}s, ordered from most strongly recommend to
* least
* @throws TasteException
* if an error occurs while accessing the {@link DataModel}
*/
List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException;
其中,最后一个参数就是本文开始提到的IDRescorer。
所以,当需要通过特定规则过滤推荐结果时,需先实现IDRescorer接口,定义评分逻辑和排除规则。
package org.apache.mahout.cf.taste.recommender;
/**
* <p>
* A {@link Rescorer} which operates on {@code long} primitive IDs, rather than arbitrary {@link Object}s.
* This is provided since most uses of this interface in the framework take IDs (as {@code long}) as an
* argument, and so this can be used to avoid unnecessary boxing/unboxing.
* </p>
*/
public interface IDRescorer {
/**
* @param id
* ID of thing (user, item, etc.) to rescore
* @param originalScore
* original score
* @return modified score, or {@link Double#NaN} to indicate that this should be excluded entirely
*/
double rescore(long id, double originalScore);
/**
* Returns {@code true} to exclude the given thing.
*
* @param id
* ID of thing (user, item, etc.) to rescore
* @return {@code true} to exclude, {@code false} otherwise
*/
boolean isFiltered(long id);
}
该接口规定了两个必须实现的方法:
1.rescore方法
功能:定义重新评分的逻辑。根据新的规则,为指定id的item重新评分。
返回:重评后的分数
输入参数:item的id,该item原来的评分
调用该方法的方法包括:
2.isFiltered
功能:定义过滤规则。判断指定id的item,根据新的规则,是否该排除在外,返回true就是该item应该排除在结果之外。
返回:true or false
输入参数:指定的id
调用该方法的方法包括:
无论是否需要根据特定规则过滤推荐结果,都必须先创建org.apache.mahout.cf.taste.recommender.Recommender类的对象r,然后通过对象r来执行推荐方法获得针对特定id用户的推荐结果List。
当无需使用特定规则过滤推荐结果时,只需使用Recommender对象的如下方法获得推荐结果:
/**
* @param userID
* user for which recommendations are to be computed
* @param howMany
* desired number of recommendations
* @return {@link List} of recommended {@link RecommendedItem}s, ordered from most strongly recommend to
* least
* @throws TasteException
* if an error occurs while accessing the {@link DataModel}
*/
List<RecommendedItem> recommend(long userID, int howMany) throws TasteException;
当需要根据特定规则过滤推荐结果时,需使用Recommender对象的如下方法获得推荐结果:
/**
* @param userID
* user for which recommendations are to be computed
* @param howMany
* desired number of recommendations
* @param rescorer
* rescoring function to apply before final list of recommendations is determined
* @return {@link List} of recommended {@link RecommendedItem}s, ordered from most strongly recommend to
* least
* @throws TasteException
* if an error occurs while accessing the {@link DataModel}
*/
List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException;
其中,最后一个参数就是本文开始提到的IDRescorer。
所以,当需要通过特定规则过滤推荐结果时,需先实现IDRescorer接口,定义评分逻辑和排除规则。
相关文章推荐
- 【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则
- 【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则
- 通过python和websocket构建实时通信系统[扩展saltstack监控] 推荐
- Mahout构建图书推荐系统【一起学Mahout】
- Mahout推荐系统构建
- Mahout构建图书推荐系统
- 项目实战:Mahout构建图书推荐系统
- 基于mahout推荐系统构建 上
- mahout0.8 构建推荐图书系统(dataguru mahout 第二周作业)
- 转】Mahout构建图书推荐系统
- 【甘道夫】通过Mahout构建贝叶斯文本分类器案例详解
- 用Mahout构建图书推荐系统
- 【甘道夫】通过Mahout构建贝叶斯文本分类器案例具体解释
- 推荐系统学习之mahout 学习评分计算
- Mahout构建图书推荐系统
- Mahout构建图书推荐系统
- Mahout构建图书推荐系统【一起学Mahout】
- Mahout小结:关于评估推荐系统估计值与实际值的偏差出现异常:DataModel doesn't have preference values
- 基于 Apache Mahout 构建社会化推荐引擎
- 【甘道夫】使用HIVE SQL实现推荐系统数据补全