Content-Based Recommendation
Content-based recommendation ranks items by how their declared features align with a user profile. It can nominate a new item before that item has interactions, but only when its metadata is available and meaningful.
Prerequisites
Keep the objective, global cutoff, and eligible candidate set from diversity, coverage, novelty, and serendipity. You should also know vectors and cosine similarity.
Learning goals
- Represent items and a user profile in the same feature space.
- Distinguish fixed input features from state learned from training interactions.
- Score only eligible candidates available at inference time.
- Audit feature quality, overspecialization, and leakage.
Model contract
| Part | This lesson's contract |
|---|---|
| Objective | Retrieve lessons whose declared topics match a returning learner's training-period interests. |
| Input representation | Three item features in the fixed order python, statistics, machine-learning; a user vector is a weighted mean of positive training items. |
| Learned state | The user's feature weights and any vocabulary or scaling parameters fitted before the global cutoff. |
| Inference path | Build the current profile, cosine-score the eligible unseen candidates, then sort by score and item ID. |
| Evaluation boundary | Reuse the shared fixture, its 2026-10-01 cutoff, its test relevance, and the same candidate constraints as the baselines. |
| Principal failure modes | Missing or manipulated metadata, stale profiles, feature leakage, popularity hidden in features, and a narrow “more of the same” list. |
Worked example: hand-check cosine scores
The training profile below is (1.0, 0.5, 0.0). It can be interpreted as one full unit of Python interest plus half a unit of statistics interest. The five candidates are the same frozen toy candidates used by the earlier baseline; their vectors are declared data, not learned from the test rows.
import math
profile = (1.0, 0.5, 0.0)
features = {
"i3": (0.0, 1.0, 0.0),
"i4": (0.0, 0.0, 1.0),
"i5": (1.0, 0.0, 0.0),
"i6": (0.0, 0.0, 1.0),
"i7": (0.0, 0.0, 1.0),
}
def cosine(left, right):
numerator = sum(a * b for a, b in zip(left, right))
denominator = math.sqrt(sum(a * a for a in left)) * math.sqrt(sum(b * b for b in right))
return numerator / denominator if denominator else 0.0
ranking = sorted(
((item, cosine(profile, vector)) for item, vector in features.items()),
key=lambda pair: (-pair[1], pair[0]),
)
print(f"profile_shape={(len(profile),)} candidate_shape={(len(features), len(profile))}")
print("ranking=[" + ", ".join(f"('{item}', {score:.3f})" for item, score in ranking) + "]")
Output:
profile_shape=(3,) candidate_shape=(5, 3)
ranking=[('i5', 0.894), ('i3', 0.447), ('i4', 0.000), ('i6', 0.000), ('i7', 0.000)]
This verifies shapes and arithmetic, not model superiority. A different vocabulary, feature weighting, or similarity changes the ranking. Fit text vocabularies, inverse-document-frequency weights, encoders, and normalizers on training content only; test-period engagement must never become an item feature.
Limitations and common mistakes
- Content features can encode sensitive attributes, editorial mistakes, or future information.
- Cosine similarity ignores vector magnitude; dot product would produce a different ranking.
- A sparse profile cannot express dislikes merely because a feature is absent: missing events are not dislikes.
- Strong similarity can reduce discovery. Measure ranking quality and beyond-accuracy outcomes together.
- Offline metric is not product impact; historical relevance cannot prove incremental benefit.
Practice
Add a multi-label item (1, 1, 0), normalize it, and recompute the list. State whether the change reflects better evidence or merely a different representation.
Next steps
Content features do not use cross-user behavior. Next, user and item neighborhoods derive local collaborative evidence from the shared fixture.
Primary sources
Checked 2026-10-17: Google, content-based filtering and candidate-generation similarity measures.