Building a Personal Recommendation System for Few Users with Dynamic Labels in Python
This article explains how to design and implement a lightweight Python recommendation system for personal use by replacing rating‑based similarity with label‑based scoring, handling sparse data, and dynamically updating user models with time‑limited label weights.
The author and roommates, facing a lack of interesting shows, decided to create a personal recommendation system to alleviate the problem, and they share the entire development process for readers who want to build a similar tool.
Traditional recommendation algorithms are introduced first: a user‑item rating matrix is used, where rows represent users and columns represent items; a rating of 0 indicates an unseen item, and similarity between items is calculated and weighted by the user's existing ratings to produce a final score for ranking.
Two common similarity measures are presented: Euclidean distance (e.g., similar = 1/sqrt((0-2)^2 + (5-5)^2 + (4-2)^2 …) ) and cosine similarity, which is normalized to the range 0‑1 using 0.5 + 0.5*cos . Both methods require sufficient rating data to be effective.
The article points out that traditional methods struggle with few‑user scenarios because they rely on large amounts of explicit rating data, which is unavailable in a personal or dorm‑room setting where users may only provide binary feedback or no feedback at all.
To address this, the author proposes two innovations: (1) replace user‑rating‑based similarity with item label ‑based similarity, leveraging attributes such as genre, director, or product category; (2) maintain a dynamic user model where each label has a weight and a creation timestamp, allowing labels to expire after a configurable period so the system reflects recent user interests.
The Python implementation uses a dictionary called record to store label information as {"labelName": (weight, time), "labelName2": (weight, time), …} . The code snippet is shown below:
<code>record = {
"labelName":(weight,time),
"labelName2":(weight,time)
……
}
# labelName is the tag name; the first element of the tuple is its weight (higher means the user likes it more).
# The second element is the creation time of the tag.
</code>When generating recommendations, the algorithm creates an empty dictionary res , iterates over a list testList , checks each item's labels against record , discards labels whose age exceeds the allowed limit, increments the item's score for valid labels, and finally sorts res by score to return the top five items.
With these steps, a lightweight recommendation system tailored for a small user base is completed and currently running in the author's dormitory, awaiting performance evaluation.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.