Item metadata is structured information describing what an item is: its category, attributes, tags, description, text, sound, or images. It belongs to an item in a catalog, rather than recording how users behaved or the context of an interaction. A movie’s genres and a product’s color are metadata; clicks, purchases, and location are different inputs to a recommender. section 1.3 section 1.3.1
A content-based recommender turns metadata into an item representation, compares representations, and retrieves similar items. For an item-to-item recommendation, the current item supplies the starting point. For a user-to-item recommendation, the system can find neighbors of items in a user’s recent history and merge the results. This can work without interaction data from other users, but it can only compare features that have been represented. section 1.3.1 section 1.6
Two practical representations are:
- Tag sets: assign each item categorical labels, then compare the overlap. Jaccard similarity divides the number of shared tags by the number of distinct tags across both items; an empty union receives a score of zero. section 1.3.1
- Text vectors: combine fields such as a cleaned movie title and genres, convert the text to TF-IDF vectors, then retrieve items using cosine similarity. TF-IDF gives less weight to words common across many items. section 2.3.1
Comparing tagged comics
Asterix and Lucky Luke share “humor,” producing a Jaccard score of 1/9. Asterix and The Smurfs share “humor” and “magic,” scoring 2/8. The second pair is more similar under these tags; that result reflects the chosen metadata, not every way the comics might be related. section 1.3.1
Inspect metadata before trusting the neighbors it produces. In a movie example, years in titles dominated frequent terms until they were removed; “(no genres listed)” exposed missing genre data. Sparse representations also have limited discrimination: movies represented chiefly by the same genre can receive identical similarity scores. After retrieval, catalog membership alone does not guarantee that an item is available to a particular user. section 2.3.1 section 1.3
Listing 2.17 Creating TF-IDF vectors
from sklearn.feature_extraction.text import TfidfVectorizer
movies["clean_title"] = (
movies["title"]
.str.replace(r"\s*\((?:19|20)\d{2}(?:-(?:19|20)\d{2})?\)\s*$", "", regex=True)
.str.strip()
) #A
movies['content'] = movies['clean_title'] + ' ' + movies['genres'].fillna('') #B
vectorizer = TfidfVectorizer(
max_features=500,
stop_words='english', #C
token_pattern=r'(?u)\b\w+\b'
)
content_vectors = vectorizer.fit_transform(movies['content']) #DThe listing shows the metadata-to-vector step: remove a year from each title, combine the cleaned title with genres, and fit a TF-IDF vectorizer. section 2.3.1
