Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Content-based Filtering

User Profiles and Feature Weighting

Combine several item fields with weights, decay old interactions in user profiles, and use genres, numeric features and one-hot encodings in a content-based recommender.

A first content-based model uses one text field and a plain average. Real catalogs have several kinds of features (text, genres, price, year, brand), and real users change over time. This lesson shows how to weight feature groups, mix text with categorical and numeric features, and build a profile that forgets old behavior gracefully.

Everything builds on the content-based recommender from the previous lesson.

Several fields, one vector

Different fields deserve different influence. A genre match is a strong, clean signal; a word in a long description is a weak one. Build one matrix per field, multiply each by a weight, and stack them side by side with scipy.sparse.hstack.

Since each block is L2-normalized, the weight directly controls how much of the final similarity comes from that field.

Python Weighted field blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With text only, similarity comes from sparse overlapping words; with genre only, Star Harbor (same genres) wins; the 1:2 mix balances both. Tune weights on a validation split, not by taste.

Categorical and numeric features

  • Categorical (brand, director, country): one-hot encode with OneHotEncoder or CountVectorizer(binary=True). Multi-valued fields such as genres become multi-hot.
  • Numeric (price, year, duration): scale to 0-1 or standardize, or bucket into ranges and one-hot them. A raw price in the thousands would swamp every other feature.
  • Missing values: use an explicit "unknown" category or the median, and a weight small enough not to dominate.
Python Mixing genres, price and year

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Time decay: users change

A profile averaged over three years of history reflects who the user was. To favor recent taste, weight each interaction by exp(-age / half_life) (or 0.5 ** (age / half_life)). A half-life of 30 days means an interaction 30 days old counts half as much as one from today.

Python Profile with time decay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no decay (huge half-life) the older space interests still matter. With a 30-day half-life the user's recent cooking taste wins.

Negative signals and the short/long split

  • Explicit dislikes (low ratings, "not interested") can subtract from the profile. Use a small negative weight.
  • Short-term and long-term: keep two profiles, one with a long half-life (stable taste) and one from the last session. Blend their scores, giving more weight to the session when it is rich.
  • Multiple interests: one average can sit between two unrelated tastes and match neither. A common fix is several profile vectors (clusters of liked items) and taking the best score per item.

Shaping a profile

Field weights Genre counts more than a word
Time decay Recent behavior counts more
Dislikes Push the profile away
Many tastes Several profile vectors

Tune every weight on validation data.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why scale numeric features such as price before combining them with text features?

    Without scaling, a column with values in the thousands overwhelms TF-IDF weights in the 0 to 1 range.
  2. 2. A 30-day half-life means an interaction from 60 days ago has what weight?

    Each 30 days halves the weight: 60 days is two halvings, 0.5 squared = 0.25.
  3. 3. How do you choose the weight of each feature block?

    Weights are hyperparameters; evaluate ranking quality on validation data to pick them.
  4. 4. Why might one averaged profile vector fail for a user with two unrelated interests?

    Mixing distant clusters yields a vector that is close to neither; several profile vectors avoid this.

Summary

  • Build one normalized block per feature group, weight it, and stack the blocks.
  • Encode categories as one-hot or multi-hot, and scale or bucket numbers.
  • Apply time decay to favor recent behavior; consider negative signals and several interest vectors.
  • Treat weights and half-life as hyperparameters to validate.

Next: Limits of Content-based Filtering