Building a Content-Based Video Recommender System

Recommender systems are foundational to modern media platforms, dictating how users discover relevant video content from massive catalogs.

By utilizing TF-IDF vectorization and cosine similarity in Python, you can build a lightweight content-based video recommendation engine that analyzes metadata, tags, and descriptions to suggest similar videos.

Core Architecture of Content-Based Filtering

Unlike collaborative filtering which relies on user interaction histories, content-based recommendation systems operate on item features:

  • Metadata Aggregation: Combines video titles, tags, categories, and description text into a unified feature string.
  • TF-IDF Vectorization: Transforms textual metadata into numerical frequency vectors, penalizing overly common words while boosting distinctive descriptive terms.
  • Cosine Similarity Matrix: Computes the angular similarity between video vectors in a high-dimensional space to quantify how closely related any two videos are.

Content-Based vs. Collaborative Filtering

Evaluation MetricContent-Based FilteringCollaborative Filtering
Cold Start ProblemHandles new videos instantly using metadata featuresFails to recommend new items until user interaction data accumulates
Data RequirementsRelies purely on descriptive video attributes and tagsRequires extensive historical user rating and watch logs
Best Suited ForNiche catalogs, cold-start items, and explicit attribute matchingLarge-scale user behavior tracking and personalized social feeds

Python Implementation with Scikit-Learn

Here is how to build a working video recommendation engine using Python, Pandas, and Scikit-Learn:

Python
Building a TF-IDF and cosine similarity video recommender in Python
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# 1. Sample video dataset metadata
data = {
    'video_id': [1, 2, 3],
    'title': ['Python Tutorial for Beginners', 'Advanced Python Concurrency', 'Building React Web Apps'],
    'tags': ['python programming tutorial beginner', 'python asyncio multithreading advanced', 'react javascript frontend web']
}
df = pd.DataFrame(data)

# 2. Initialize TF-IDF Vectorizer and fit on video tags
tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df['tags'])

# 3. Compute cosine similarity matrix between all videos
similarity_matrix = cosine_similarity(tfidf_matrix, tfidf_matrix)

# 4. Function to recommend top similar videos
def get_recommendations(video_title, df=df, similarity=similarity_matrix):
    idx = df[df['title'] == video_title].index[0]
    scores = list(enumerate(similarity[idx]))
    scores = sorted(scores, key=lambda x: x[1], reverse=True)
    # Get top similar videos excluding the video itself
    top_indices = [i[0] for i in scores[1:3]]
    return df['title'].iloc[top_indices].tolist()

# Test recommendations
print(get_recommendations('Python Tutorial for Beginners'))

Production Recommendation Best Practices

When scaling content-based recommender systems for production web applications, follow these guidelines:

  • Precompute Similarity Matrices: Calculate and cache cosine similarity matrices in Redis or PostgreSQL rather than computing them on the fly for every user request.
  • Incorporate Weighted Metadata: Give higher weight to specific attributes like categories and exact tags by concatenating metadata fields multiple times before vectorization.
  • Hybrid Filtering Integration: Combine content-based similarity scores with collaborative filtering user interaction signals to improve recommendation diversity.