Popularity and content baselines
Quick answer
A recommender ranks eligible items for a user or context. Popularity ranks by historical demand; content-based retrieval compares item attributes with the attributes of items previously selected by that user. Examples are static Python snippets tested locally on CPU; NumPy and scikit-learn are required for selected examples. ByteMind execution is unverified.
Popularity with exclusions
from collections import Countercounts=Counter(["a","b","a","c","b","a"])print(sorted((i for i in counts if i!="a"),key=lambda i:(-counts[i],i)))Expected output (locally tested): ['b', 'c']. The most frequent eligible item is b.
How it works
First define the prediction unit (user, recommendation time), catalog eligibility, interaction signal and horizon. A two-stage system retrieves candidates then ranks them; a reranker may enforce diversity or inventory constraints. A popularity baseline can count distinct users per item in a training window rather than raw clicks, which may be dominated by repeat activity. Freeze these counts at the evaluation cutoff. Filter unavailable and previously consumed items for a discovery task, not necessarily for repeat-purchase tasks. For content-based ranking, represent items with vectors of attributes and average the vectors of historical positive items. Cosine similarity is dot(a,b)/(norm(a)*norm(b)); zero norms require an explicit fallback. Content features must be available as of ranking time. Scores are not calibrated probabilities. Resolve score ties with a stable item ID and document the policy. Content methods can recommend new items with known attributes, but may overspecialize and cannot infer preferences absent from the features. Popularity is a valuable nonpersonalized control, not a proof of relevance. Compare methods on identical eligible catalogs, cutoffs, histories and K.
Content cosine ranking
from sklearn.metrics.pairwise import cosine_similarityprint(cosine_similarity([[1,0]],[[1,0],[0,1],[1,1]]).round(3).tolist())Expected output (locally tested): [[1.0, 0.0, 0.707]]. The first item shares the entire direction of the history vector.
Common mistakes
Do not count validation or test interactions in popularity, include consumed items in a novel-item ranking, or interpret high cosine similarity as purchase probability. Avoid hidden arbitrary tie ordering.
Practice and next steps
Build a popularity list and a content list for a synthetic catalog of six books. Remove consumed items and explain why the rankings differ.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Popularity and content baselines — knowledge check
Apply popularity and content baselines to small examples and debugging scenarios.
Which data may train a popularity baseline?
Which is content based?
What is a zero-norm profile?
Why use a stable tie rule?
What must competing rankers share?