Create a Personalized Movie Recommendation Engine using Content-based Filtering in Python

Create a Personalized Movie Recommendation Engine using Content-based Filtering in Python

Content-based recommendation ranks items by similarity between their metadata and a user’s expressed interests. This updated tutorial combines movie titles, descriptions, and tags, converts the text to TF-IDF vectors, and uses cosine similarity.

Vectorize movie metadata

vectorizer = TfidfVectorizer(stop_words="english")
matrix = vectorizer.fit_transform(movies["text"])
query_vector = vectorizer.transform([query])
scores = cosine_similarity(query_vector, matrix).ravel()

Because the same fitted vectorizer transforms both catalog text and the query, the scores live in one feature space.

Content-based recommendations

The result is reproducible and easy to inspect, but metadata-only recommendations can become narrow. A production system should evaluate ranking relevance, diversity, freshness, missing metadata, cold starts, and the effect of combining content and collaborative signals.

Florian Follonier

Florian Follonier · Cloud Solution Architect at Microsoft

Florian Follonier (PhD) is a Cloud Solution Architect at Microsoft based in Zurich and the author of relataly.com, writing hands-on tutorials on machine learning, Python, RAG, and AI agents.