在创建graphlab推荐模型时,应该如何设置参数,以确保不会向用户再次推荐他们已经拥有的物品?这是可以通过设置某些参数直接实现的,还是需要从头开始编写推荐器?数据看起来像这样:
| user_id | item_id | othercolumns ||:-----------|------------:|:------------:|| 1 | 21 | This | | 2 | 22 | column || 1 | 23 | will || 3 | 24 | hold || 2 | 25 | other || 1 | 26 | values |
因为用户1已经拥有物品21、23和26,这些物品不应再被推荐给他。
回答:
这种行为是由recommender.recommend
方法的exclude_known
参数控制的(文档)。
exclude_known : bool, optional
默认情况下,所有在训练数据中或通过
new_observation_data
提供的新数据中之前见过的用户-物品交互,都将从推荐中排除。设置exclude_known = False
可以覆盖此行为。
示例
>>> import graphlab as gl>>> sf = gl.SFrame({'user_id':[1,2,1,3,2,1], 'item_id':[21,22,23,24,25,26]})>>> print sf+---------+---------+| item_id | user_id |+---------+---------+| 21 | 1 || 22 | 2 || 23 | 1 || 24 | 3 || 25 | 2 || 26 | 1 |+---------+---------+[6 rows x 2 columns]>>> rec_model = gl.recommender.create(sf)>>> # 我们推荐用户未拥有的物品>>> rec_wo_own_item = rec_model.recommend(sf['user_id'].unique())>>> rec_wo_own_item.sort('user_id').print_rows(100)+---------+---------+----------------+------+| user_id | item_id | score | rank |+---------+---------+----------------+------+| 1 | 22 | 0.0 | 1 || 1 | 24 | 0.0 | 2 || 1 | 25 | 0.0 | 3 || 2 | 21 | 0.0 | 1 || 2 | 23 | 0.0 | 2 || 2 | 24 | 0.0 | 3 || 2 | 26 | 0.0 | 4 || 3 | 21 | 0.333333333333 | 1 || 3 | 23 | 0.333333333333 | 2 || 3 | 26 | 0.333333333333 | 3 || 3 | 22 | 0.166666666667 | 4 || 3 | 25 | 0.166666666667 | 5 |+---------+---------+----------------+------+[12 rows x 4 columns]>>> # 我们推荐用户拥有的物品>>> rec_w_own_item = rec_model.recommend(sf['user_id'].unique(), exclude_known=False)>>> rec_w_own_item.sort('user_id').print_rows(100)+---------+---------+----------------+------+| user_id | item_id | score | rank |+---------+---------+----------------+------+| 1 | 21 | 0.666666666667 | 1 || 1 | 23 | 0.666666666667 | 2 || 1 | 26 | 0.666666666667 | 3 || 1 | 22 | 0.0 | 4 || 1 | 24 | 0.0 | 5 || 1 | 25 | 0.0 | 6 || 2 | 26 | 0.0 | 6 || 2 | 24 | 0.0 | 5 || 2 | 23 | 0.0 | 4 || 2 | 21 | 0.0 | 3 || 2 | 25 | 0.5 | 2 || 2 | 22 | 0.5 | 1 || 3 | 24 | 0.0 | 6 || 3 | 25 | 0.166666666667 | 5 || 3 | 22 | 0.166666666667 | 4 || 3 | 26 | 0.333333333333 | 3 || 3 | 23 | 0.333333333333 | 2 || 3 | 21 | 0.333333333333 | 1 |+---------+---------+----------------+------+[18 rows x 4 columns]>>> # 我们将推荐的用户未拥有的物品添加到原始的SFrame中>>> rec = rec_wo_own_item.groupby('user_id', {'reco':gl.aggregate.CONCAT('item_id')})>>> sf = sf.join(rec, 'user_id', 'left')>>> print sf+---------+---------+----------------------+| item_id | user_id | reco |+---------+---------+----------------------+| 21 | 1 | [24, 25, 22] || 22 | 2 | [24, 26, 23, 21] || 23 | 1 | [24, 25, 22] || 24 | 3 | [21, 23, 26, 25, 22] || 25 | 2 | [24, 26, 23, 21] || 26 | 1 | [24, 25, 22] |+---------+---------+----------------------+[6 rows x 3 columns]