BLOGLLM Logo
Go Pro
Guest
Not signed in
所有博客课程练习面试准备系列标签书签价格
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboard游戏
Articles
练习 / Recommender systems5 of 22 in topic

198. 使用时间衰减流行度对热门物品进行排序

EasyRecommender systems

重要性 购物、视频和新闻应用中的热门栏目通常使用这种时间衰减计数作为强大的基准方案。

全时段流行度会不断推荐去年的热门内容。热门列表会根据交互发生的近期程度为每次交互分配权重。

编写 decayed_popularity(events, now, half_life, k)。events 是一个包含 (item, timestamp) 的列表。忽略 timestamp > now 的事件(这些是时钟偏移或未来日期事件)。每个剩余事件为其对应 item 的得分增加 0.5 ** ((now - timestamp) / half_life)。返回得分最高的前 k 个 item id,若得分相同,则按较小的 item id 排序。

函数def decayed_popularity(events, now, half_life, k)

示例 1

输入: decayed_popularity([(1, 0), (1, 0), (2, 9), (3, 12)], 10, 10, 2)

输出: [1, 2]

示例 2

输入: events = [(7, 0)] * 5 + [(8, 95), (8, 99), (9, 100)], now = 100, half_life = 10, k = 3

输出: [8, 9, 7]

示例 3

输入: events = [(5, 50), (4, 50), (6, 40)], now = 50, half_life = 10, k = 2

输出: [4, 5]

约束条件

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
提示 1

一个正好处于一个半衰期前的事件,其权重计为现在发生的事件的一半。

提示 2

age = now - ts。

提示 3

使用 defaultdict(float)。

提示 4

使用 key=lambda i: (-score[i], i) 对键进行排序。

常见错误
  • 统计了未来的事件。
  • 将指数写成了 half_life / age。
  • 任意处理平局,这会导致列表内容闪烁。
理解思路

流行度是每个推荐系统必须超越的基准,但单纯的计数永远不会遗忘。去年卖得好的产品会永远占据位置。时间衰减解决了这个问题:随着时间推移,每次交互的权重会越来越小,就像逐渐消退的记忆一样。

Example with half-life 10 and now = 10: item 1 在 time 0 有两次旧观看,每次计为 0.5^(10/10) = 0.5,总计 1.0。item 2 在 time 9 有一次新鲜观看,计为 0.5^(1/10) = 0.93。item 1 仍然以微弱优势胜出,但如果 item 2 再多一次新鲜观看就会反超。time 12 的事件属于未来,会被忽略。

score(item) = sum over its events of 0.5^((now - t) / half_life)。

热门栏目、“你附近的流行”列表以及针对新用户的兜底方案通常都是这样构建的。

主题
Recommender systems
相似题目
  • 衡量推荐系统的 hit rate 和 recall at kEasy
  • 计算隐式反馈的 BPR ranking loss 并防止溢出Easy
  • 推荐最热门的未见过的物品Easy
  • 衡量推荐系统的目录覆盖率Easy
所有 Recommender systems 道题目
反馈
198 / 903
Python 3solution.pyPython 空闲
1
2
3
4
5
6
from collections import defaultdict
​
def decayed_popularity(events, now, half_life, k):
# your code here
pass
​

Run 会在浏览器中检查这些用例,无需登录。 Submit 会检查所有 4 个测试。

Ctrl/按 + 回车 运行
© BlogLLM, Bengaluru, India关于条款隐私退款DMCA联系我们RSS