码迷,mamicode.com
首页 > 其他好文 > 详细

【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则

时间:2014-06-09 19:10:29      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

通过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原来的评分
调用该方法的方法包含:
bubuko.com,布布扣
bubuko.com,布布扣

2.isFiltered
功能:定义过滤规则。推断指定id的item,依据新的规则,是否该排除在外,返回true就是该item应该排除在结果之外。
返回:true or false
输入參数:指定的id
调用该方法的方法包含:
bubuko.com,布布扣
bubuko.com,布布扣


不管是否须要依据特定规则过滤推荐结果,都必须先创建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扩展评分规则,布布扣,bubuko.com

【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/hrhguanli/p/3777296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!