0Pricing
Learn AI with Python · Lesson

Content-Based Filtering

TF-IDF item representations, cosine similarity, building a movie recommender from metadata.

Content-Based Filtering is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Content-Based Filtering?

Content-based filtering recommends items similar to ones a user already liked, based on the items own features (text, genre, tags). Unlike collaborative filtering, it needs no other users data, so it sidesteps the item cold-start problem.

Items as Feature Vectors

The key idea: turn each item into a numeric vector describing its content, then measure similarity between vectors. For text descriptions, TF-IDF is the classic way to build those vectors.

What Is TF-IDF?

TF-IDF (Term Frequency-Inverse Document Frequency) weights words by how often they appear in an item but down-weights words common across all items. Rare, distinctive words get high weights, capturing what makes an item unique.

TfidfVectorizer

scikit-learn turns a list of item descriptions into a TF-IDF matrix in two lines.

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer(stop_words="english")
tfidf_matrix = vectorizer.fit_transform(df["description"])

Understanding the Matrix Shape

The tfidf_matrix has shape (n_items, n_terms): one row per item, one column per unique word in the vocabulary. It is sparse since most items use only a fraction of all words.

print(tfidf_matrix.shape)  # (n_items, vocabulary_size)

Tuning the Vectorizer

Useful parameters: stop_words drops common words, max_features caps vocabulary size, and ngram_range=(1,2) includes two-word phrases for richer features.

vectorizer = TfidfVectorizer(
    stop_words="english",
    max_features=5000,
    ngram_range=(1, 2)
)

Cosine Similarity Between Items

Compute pairwise similarity across all items. The result is an n_items x n_items matrix where each cell is how similar two items descriptions are.

from sklearn.metrics.pairwise import cosine_similarity

cosine_sim = cosine_similarity(tfidf_matrix)
print(cosine_sim.shape)  # (n_items, n_items)

Linear Kernel Shortcut

Because TF-IDF vectors are L2-normalized by default, linear_kernel gives the same result as cosine similarity but faster, a common optimization for large catalogs.

from sklearn.metrics.pairwise import linear_kernel

cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)

Mapping Titles to Indices

To look up an item by name, build a reverse index from title to row position.

indices = pd.Series(df.index, index=df["title"]).drop_duplicates()

The get_recommendations Function

Given a title, fetch its similarity row, sort descending, skip the item itself, and return the top matches.

def get_recommendations(title, n=10):
    idx = indices[title]
    scores = list(enumerate(cosine_sim[idx]))
    scores = sorted(scores, key=lambda x: x[1], reverse=True)
    top = scores[1:n+1]          # skip itself at position 0
    item_idxs = [i for i, _ in top]
    return df["title"].iloc[item_idxs]

Strengths and Limits

Strengths: works for brand-new items, recommendations are explainable ("because it shares these words").

Limits: it stays inside a user known tastes (over-specialization) and cannot discover surprising cross-genre hits the way collaborative filtering can.

Quick Check

Test your content-based filtering knowledge.

Recap

You built content-based filtering: TfidfVectorizer turns descriptions into a (n_items, n_terms) matrix, cosine_similarity compares items, and get_recommendations returns the top similar items (skipping the item itself). It handles cold-start but risks over-specialization. Next: hybrid systems and evaluation metrics.

Frequently asked questions

Is the “Content-Based Filtering” lesson free?

Yes — the full text of “Content-Based Filtering” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Content-Based Filtering”?

TF-IDF item representations, cosine similarity, building a movie recommender from metadata. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Content-Based Filtering” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Collaborative Filtering: User-Based and Item-Based
  2. Matrix Factorization with SVD
  3. Content-Based Filtering
  4. Hybrid Systems and Evaluation Metrics
← Back to Learn AI with Python