
A machine learning-based technique that provides optimal suggestions based on user preferences, behavior, and past interaction data
Recommendations generated based on user interactions
Definition : Based on the assumption that 'users with similar preferences are likely to like similar items,' the algorithm recommends new items by utilizing the item preferences of similar users
How it works
User-Item Interaction Matrix generation : Create a User-Item Interaction Matrix based on the ratings or interaction data that each user gives to each item
Calculate Similarity : Calculate the similarity between users, usaually using Cosine Similarity or Pearson Correlation
Selecting the most similar users : Based on the calculated similarity values, the most similar users are selected using the K-NN method
Prediction and recommendation generation : Based on the ratings from the similar users, the predicted rating for items that the current user hasn't rated yet is calculated
Recommendation generation : Recommend the items with the highest predicted ratings
Code Example
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# User-Item Rating Matrix
user_item_matrix = np.array([[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4]])
# Calculate similarity between users (Cosine Similarity)
user_similarity = cosine_similarity(user_item_matrix)
# User-based Collaborative Filtering function
def recommend(user_id, user_similarity, user_item_matrix, k=2):
# Select users with high similarity
similar_users = np.argsort(-user_similarity[user_id])[:k]
# Ratings of similar users
similar_users_ratings = user_item_matrix[similar_users]
# Predict ratings by averaging the ratings of similar users
predicted_ratings = similar_users_ratings.mean(axis=0)
# Recommend items that the user has not rated yet, with the highest predicted ratings
unrated_items = np.where(user_item_matrix[user_id] == 0)[0]
recommendations = np.argsort(-predicted_ratings[unrated_items])
return unrated_items[recommendations]
# Calculate recommended items for user 0
recommendations = recommend(user_id=0, user_similarity=user_similarity, user_item_matrix=user_item_matrix)
print(f"User 0's recommended items: {recommendations}")
Finding items with similar features by analyzing the user's past behavior and preference information
How it works
Feature extraction of items: Extract the attributes of an item and represent them as a 'Feature Vector'
ex) For a movie, attributes like genre, director, and cast can be converted into a vector
Creating a user profile: Generate a user profile vector that reflects the user's preferences by combining the Feature Vectors of items they have rated
Calculating similarity between items and users: Calculate the similarity between the generated user profile vector and the feature vector of the item to recommend - usually using cosine similarity
Generating recommendations: Based on the calculated similarity, recommend the most similar items that the user has not rated yet
Code Example
Recommend similar movies based on the user's movie preferences using the Content-based Filtering method
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# The feature vector of a movie (e.g., [genre, director, cast])
movie_features = np.array([[1, 1, 0], # Movie 1
[0, 1, 1], # Movie 2
[1, 0, 1], # Movie 3
[0, 0, 1]]) # Movie 4
# The indices of movies rated by the user (e.g., the user likes Movie 1 and Movie 3)
user_ratings = np.array([5, 0, 4, 0]) # Ratings for movies rated by the user
# User profile vector calculation (weighted sum of the feature vectors of rated movies)
user_profile = user_ratings @ movie_features
# Calculation of similarity between the user profile and the feature vectors of each movie
similarities = cosine_similarity([user_profile], movie_features)[0]
# Recommend movies with high similarity from those the user has not rated yet
unrated_movies = np.where(user_ratings == 0)[0]
recommended_movies = np.argsort(-similarities[unrated_movies])
print(f"Recommended movies: {unrated_movies[recommended_movies]}")
Process of generating the user profile vector
Prepare the Feature Vector of items: A vector that quantifies the attributes of items
ex) Assume each movie has the following attributes
| Item | Genre | Director | Cast |
|---|---|---|---|
| Movie A | 1 | 1 | 0 |
| Movie B | 0 | 1 | 1 |
| Movie C | 1 | 0 | 1 |
Prepare user rating information: Information representing how much a user prefers each item
ex) Assume the user rated the following three movies as follows:
| Item | Rating |
|---|---|
| Movie A | 5 |
| Movie B | 0 |
| Movie C | 4 |
Calculate the user profile vector: Multiply the feature vector of each item by the user’s rating, sum them up, and normalize
The user profile vector is the weighted average of the feature vectors of the items rated by the user
Concrete example
Prepare the feature vectors of items: The feature vectors for movies A, B, and C are as follows:
Prepare user rating information: The ratings the user gave to movies A, B, and C are as follows:
Calculate the user profile vector
Calculate the user profile vector:
Here, (the number of items rated by the user), and the feature vector of each item is multiplied by the user's rating:
After calculating:
Therefore, this user’s profile vector is
Code Example
import numpy as np
# Item feature vectors (e.g., [Genre, Director, Cast])
item_features = np.array([[1, 1, 0], # Movie A
[0, 1, 1], # Movie B
[1, 0, 1]]) # Movie C
# Ratings given by the user
user_ratings = np.array([5, 0, 4]) # Ratings for Movie A, Movie B, Movie C
# Calculate user profile vector (weighted sum of the feature vectors of rated items)
user_profile = np.dot(user_ratings, item_features) / np.count_nonzero(user_ratings)
print(f"User profile vector: {user_profile}")
A method that combines multiple recommendation algorithms to complement the weaknesses of individual algorithms and improve the performance and diversity of the recommendation system
A method that generates the final recommendation by assigning weights to the predicted scores of each algorithm
How it works: Calculate the result of each recommendation algorithm, and apply weights to those results to compute the final rating
Example formula (Combining Collaborative Filtering and Content-based Filtering)
Application example
A method that runs multiple recommendation algorithms in parallel and combines their results to generate the final recommendation
How it works
Example Formula
Application example
A method that performs the first filtering with one algorithm and then applies another algorithm to the filtered results to generate the final recommendation
How it works: Perform the first filtering, and then use another algorithm to generate the final recommendation list from the filtered items
Example Formula
Application example
A method where the results or features from one algorithm are used as inputs to another algorithm
How it works: Use the feature information calculated by one algorithm as input to another algorithm to generate recommendations
Example Formula
Application example