Sub topic 2
Predicting likes, comments, shares, and hides
Before starting, this chapter assumes you can read short Python, that you understand probability as a number from 0 to 1, and that you know what a feed is. Ranking is the step that turns a pile of candidate posts into an ordered list.
Predicting reactions
A ranking model is a function (a formula that maps inputs to an output) that estimates how likely you are to react to a post. Each estimate is a probability between 0 and 1. The model learns these probabilities from past behavior, using signals such as the viewer's history, the author's past engagement, the post's format, and the time of day.
Platforms usually predict several actions separately:
p_like: the chance you like the postp_comment: the chance you commentp_share: the chance you share itp_hide: the chance you hide it or mark it as unwanted
Hides matter as much as likes. A hide is a strong negative signal, so the model treats it as a cost.
Turning predictions into one score
A feed needs one number per post to sort by. The platform combines the predicted probabilities with weights, then scales the result by freshness. The weights below are illustrative, not any real platform's values.
from dataclasses import dataclass
@dataclass
class Post:
id: str
author: str
age_hours: float
p_like: float
p_comment: float
p_hide: float
WEIGHTS = {"like": 1.0, "comment": 4.0, "hide": -10.0}
def score(post: Post) -> float:
value = (WEIGHTS["like"] * post.p_like
+ WEIGHTS["comment"] * post.p_comment
+ WEIGHTS["hide"] * post.p_hide)
freshness = 0.5 ** (post.age_hours / 6) # halves every 6 hours
return value * freshness
posts = [
Post("a", "ana", 1, 0.30, 0.02, 0.01),
Post("b", "ben", 12, 0.60, 0.10, 0.01),
Post("c", "cy", 2, 0.10, 0.01, 0.05),
]
for p in sorted(posts, key=score, reverse=True):
print(p.id, round(score(p), 3))Post b has the highest raw engagement, but its age of 12 hours cuts its score to a quarter, so post a ranks first.
Filtering for integrity and quality
Before sorting, the system removes posts that break platform rules. Typical filters check for spam, blocked authors, and content flagged by reviewers. A filter is a yes or no test. A post that fails the test never reaches the scoring step.
Re-ranking for diversity and variety
Sorting by score alone can show five posts from one author in a row. A re-ranking pass keeps the score order but limits repeats. The function below allows at most two consecutive posts from the same author.
def rerank(posts, score, max_run=2):
remaining = sorted(posts, key=score, reverse=True)
result = []
while remaining:
for i, p in enumerate(remaining):
recent = [q.author for q in result[-max_run:]]
blocked = len(recent) == max_run and all(a == p.author for a in recent)
if not blocked:
result.append(remaining.pop(i))
break
else:
result.append(remaining.pop(0))
return resultExercise
In the rerank function, what happens when every remaining post would break the author rule?
Show answer
The else branch runs, and the function takes the highest-scored remaining post (remaining.pop(0)) so the loop always finishes.