Title: Fundamentals of Recommendation Systems - PyImageSearch

URL Source: https://pyimagesearch.com/2023/06/19/fundamentals-of-recommendation-systems/

Published Time: 2023-06-19T13:00:00+00:00

Markdown Content:
* * *

**Table of Contents**
---------------------

*   [Fundamentals of Recommendation Systems](#h2BPTitle)

*   [Recommendation Engines and Their Types](#h3Types)

*   [What Are Recommendation Engines?](#h4Engines)
*   [Content-Based Recommendations](#h4Content)
*   [Collaborative Recommendations](#h4Collaborative)
*   [Hybrid Recommendations](#h4Hybrid)

*   [Evaluating Recommendation Systems](#h3Evaluating)

*   [Root Mean Square Error](#h4Mean)
*   [Precision@K](#h4Precision)
*   [Recall@K](#h4Recall)
*   [Mean Reciprocal Rank](#h4Reciprocal)

*   [Recommendation Techniques](#h3Techniques)

*   [Text Mining](#h4Mining)
*   [K-Nearest Neighbor](#h4Nearest)
*   [Clustering](#h4Clustering)
*   [Matrix Factorization](#h4Factorization)
*   [Neural Networks](#h4Neural)

*   [Summary](#h2Summary)

*   [Citation Information](#h3Citation)

* * *

In this course, we will explore the fascinating world of recommendation systems. We will start by learning the basics of these systems and then delve into some of the most popular ones in detail.

[![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201024%20575'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/ddpre-101-1-featured.png)

With the rise of the internet and the increasing use of smart devices, it has become more important than ever to understand user preferences and provide personalized content and services. Web apps can keep users engaged by offering personalization, which is made possible by recommendation engines.

These engines utilize user data (e.g., followers, tweets, pictures, likes and dislikes, ratings, etc.) to deliver personalized recommendations. Understanding the user’s taste is crucial for a successful item recommendation, and various techniques (e.g., machine learning, statistics, probability, and algebra) are used to achieve this.

Recommendation systems have become integral to many services and web apps (**Figure 1**), including Netflix, Amazon, LinkedIn, and YouTube. Each service uses unique techniques and algorithms to analyze user data and provide recommendations that keep us returning for more.

[![Image 2: Figure 1: Distribution of applications of recommendation systems (source: Ko et al., 2022).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20528%20500'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/application.jpeg)

**Figure 1:** Distribution of applications of recommendation systems (source: [Ko et al., 2022](https://www.mdpi.com/2079-9292/11/1/141)).

This lesson is designed to give readers a comprehensive understanding of how various tools (e.g., machine learning, statistics, probability, and algebra) are employed to recommend our popular daily applications. By the end of the lesson, readers will have a solid grasp of the underlying principles that enable these applications to make suggestions based on data analysis.

In this tutorial, you will learn the **fundamentals of recommendation systems**.

This lesson is the 1st of a 3-part series on **Deep Dive into Popular Recommendation Engines 101**:

1.  [**_Fundamentals of Recommendation Systems_**](https://pyimg.co/9kf03) **(this tutorial)**
2.  _Netflix Movies and Series Recommendation Systems_
3.  _LinkedIn Jobs Recommendation Systems_

**To learn the fundamentals of recommendation systems, _just keep reading._**

* * *

[**Fundamentals of Recommendation Systems**](#TOC)
--------------------------------------------------

* * *

### [**Recommendation Engines and Their Types**](#TOC)

* * *

#### [**What Are Recommendation Engines?**](#TOC)

When recommending items to users, a recommendation engine follows a simple process. It first analyzes the usefulness of each item in a given set (e.g., movies, books, videos, or music) for any user. Once it has determined the utility of each item, it then recommends the item that maximizes the overall utility for a particular user. This is done by finding the item, denoted as ![Image 3: c^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2012'%3E%3C/svg%3E), with the highest utility score when measured against the user. In other words,

![Image 4: c^* \ = \ \text{argmax}_{c \in C} f(c, u),](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20171%2019'%3E%3C/svg%3E)

where ![Image 5: f(c,u)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%2018'%3E%3C/svg%3E) is the utility function that measures the usefulness of an item ![Image 6: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) for a user ![Image 7: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E).

Alternatively, a recommendation engine can also suggest a list of top ![Image 8: N](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) items from the set ![Image 9: C](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2011'%3E%3C/svg%3E) based on their utility for a particular user. Overall, a recommendation engine helps users find items that they will enjoy and find useful. **Figure 2** illustrates an overall view of a recommendation engine.

[![Image 10: Figure 2: Recommendation System (source: Accenture).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20481'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/recommendation-system.jpeg)

**Figure 2:** Recommendation System (source: [Accenture](https://www.slideserve.com/kimo/recommender-systems-and-product-semantics)).

Regarding recommendation systems, the space of possible items can be quite large, with hundreds of thousands or even millions of items in some cases. The utility function typically represents a rating that predicts how likely a user will enjoy a particular item. For example, if a user enjoys action thrillers, he would probably like a Tom Cruise movie if it is recommended.

Each user has a profile with various features (e.g., age, marital status, watching history, recent queries, ratings, etc.). Similarly, an item (e.g., a movie) can be represented by features like genre, year, director, cast, IMDB rating, and so on. The utility function measures the similarity between a user profile and an item feature.

The biggest challenge in creating a recommendation system is the utility function, which needs to be defined for all user-item combinations. Additionally, the user and item space is continually growing and changing. This is where machine learning, statistics, and algebra come into play. By analyzing how users have interacted with items in the past, we can use algorithms to approximate the utility function and make personalized recommendations that users will love.

* * *

#### [**Content-Based Recommendations**](#TOC)

Content-based recommendations (**Figure 3**) rely on estimating the utility function of an item for a specific user based on their preferences for similar items. This allows the system to suggest items that align with the user’s interests, making it easier to discover new content they’ll enjoy.

For example, if a user has a liking for horror movies (e.g., “The Conjuring” or “The Nun”), the content-based recommendation system will suggest other horror movies (e.g., “Annabelle”). Ultimately, the system aims to find items that match the user’s preferences and interests, providing them with personalized recommendations.

One of the best-known measures for calculating the utility of an item ![Image 11: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) for user ![Image 12: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) is cosine similarity. To understand this, assume that each item ![Image 13: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) can be represented as a feature vector,

![Image 14: c \ = \ (w_1, w_2,\dots, w_m),](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20159%2018'%3E%3C/svg%3E)

where ![Image 15: w_i](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2016%2010'%3E%3C/svg%3E) represents the weight of the ![Image 16: i^\text{th}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2018%2014'%3E%3C/svg%3E) feature (e.g., Whether the movie is a horror or not? Does the film have Tom Cruise as a leading actor?). Note that the weights ![Image 17: w_i](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2016%2010'%3E%3C/svg%3E) can be binary or continuous depending on the nature of the feature.

Given these item feature vectors, the user profile vector ![Image 18: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) can be computed as the average of feature vectors of items ![Image 19: c_i](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2011%2010'%3E%3C/svg%3E) (total ![Image 20: L](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2011%2011'%3E%3C/svg%3E) items) liked or rated by that user in the past. In other words,

![Image 21: u \ = \ \displaystyle\frac{c_1 + c_2 + \dots + c_L}{L}.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20172%2034'%3E%3C/svg%3E)

Once we have the user profile vector, we can use cosine similarity to compute the similarity between the user profile and a new item ![Image 22: c_j](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2012%2012'%3E%3C/svg%3E). This can be done using the following equation:

![Image 23: f(c_j, u) \ = \ \displaystyle\frac{\langle c_j, u \rangle}{\Vert c_j \Vert \cdot \Vert u \Vert},](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20160%2041'%3E%3C/svg%3E)

where ![Image 24: \langle .,. \rangle](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2027%2018'%3E%3C/svg%3E) denotes the dot product operation and ![Image 25: \Vert \cdot \Vert](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2024%2018'%3E%3C/svg%3E) denotes the norm of a vector.

It is important to consider certain limitations when utilizing content-based recommendations.

*   New users may find establishing a user profile vector difficult due to limited information about their interests. This may result in less meaningful recommendations.
*   Additionally, content-based recommendations may become too similar to items the user has already engaged with, leading to overspecialization and limited exploration of new interests.

* * *

#### [**Collaborative Recommendations**](#TOC)

As opposed to content-based recommendations, collaborative systems (**Figure 4**) are designed to predict the usefulness of an item for a user based on the ratings provided by other users. This means that the system can suggest items that are liked or rated highly by users with similar interests as the given user. For instance, if two users, Mike and John, both prefer horror movies, and Mike likes “The Conjuring.” Then, the collaborative system will also recommend “The Conjuring” to John.

[![Image 26: Figure 4: Collaborative recommendation system (source: KDnuggets).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20474%20335'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/collaborative.jpg)

**Figure 4:** Collaborative recommendation system (source: [KDnuggets](https://www.kdnuggets.com/2018/06/technical-content-personalization.html)).

To predict the utility ![Image 27: f(c,u)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%2018'%3E%3C/svg%3E) of an un-rated item ![Image 28: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) for a user ![Image 29: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E), we define a set ![Image 30: U = \{ u_1, u_2, \dots, u_K\}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20146%2017'%3E%3C/svg%3E) consisting of ![Image 31: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) users that are most similar to user ![Image 32: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) and have already rated the item ![Image 33: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E). We can find these ![Image 34: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest users by measuring the cosine similarity between them and the given user ![Image 35: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E).

The utility ![Image 36: f(c,u)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%2018'%3E%3C/svg%3E) can thus be estimated as the average of utilities ![Image 37: f(c, u_i)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2049%2018'%3E%3C/svg%3E) ![Image 38: (u_i \in U)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2057%2018'%3E%3C/svg%3E) as follows:

![Image 39: f(c, u) \ = \ \displaystyle\frac{\sum\limits_{i=1}^{K} f(c, u_i)}{K}.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20158%2058'%3E%3C/svg%3E)

Here, we have chosen a simple function-like average to aggregate the utilities ![Image 40: f(c, u_i)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2049%2018'%3E%3C/svg%3E). In general, the aggregator can be any function.

Like content-based recommendations, collaborative systems have their limitations:

*   Identifying the ![Image 41: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest users for new users is difficult because of the limited information about their interests. Hence, recommendations won’t be meaningful.
*   New items are regularly added to the recommendation systems. Collaborative systems depend on the rating given to an item by other users. Therefore, until a substantial number of users rate the new item, the recommender system cannot recommend it.
*   Collaborative systems depend on the availability of similar users who have rated the item we are interested in. But, usually, the number of ratings already obtained could be bigger than the predicted ratings. This makes the recommendations ineffective. This is commonly referred to as the sparsity problem.

* * *

#### [**Hybrid Recommendations**](#TOC)

Hybrid systems are becoming increasingly popular in recommendation engines as they offer the benefits of both content-based and collaborative methods while avoiding some of their limitations. There are different ways to combine both of these methods, which can be classified as follows:

*   Ensembling the predictions of both content-based and collaborative systems (e.g., a linear combination of ratings and a voting scheme)
*   Incorporating content-based characteristics into a collaborative approach and vice-versa (e.g., collaborative systems can also maintain content-based profiles for each user, which can be used to provide recommendations for uncommonly rated items)
*   A unified recommendation system that incorporates both content and collaborative aspects of the recommendation

* * *

### [**Evaluating Recommendation Systems**](#TOC)

Establishing the qualities that make a recommendation system great is crucial when assessing its effectiveness. In addition, various metrics can be employed to evaluate a recommendation system, and it’s essential to delve into these metrics and understand how they can be used for evaluation purposes.

* * *

#### [**Root Mean Square Error**](#TOC)

Root mean square error (RMSE) is one of the simplest ways to evaluate a recommendation engine. RMSE measures how good the predicted utility ![Image 42: f(c,u)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%2018'%3E%3C/svg%3E) of an item ![Image 43: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) for user ![Image 44: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) is compared to the user’s actual ratings. Mathematically,

![Image 45: \text{RMSE} \ = \ \sqrt{\displaystyle\frac{\sum_{(u,c) \in R} (f(c, u) - r_{cu})^2}{\vert R \vert}},](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20268%2051'%3E%3C/svg%3E)

where ![Image 46: r_{cu}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2019%2010'%3E%3C/svg%3E) is the rating given by user ![Image 47: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) to item ![Image 48: c](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%207'%3E%3C/svg%3E) and ![Image 49: R](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2011'%3E%3C/svg%3E) is the set of users and items for which we already have the ratings.

* * *

#### [**Precision@K**](#TOC)

Precision measures the efficiency of a machine learning algorithm. For a recommendation system, Precision@K measures the fraction of top-![Image 50: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) recommendations which are actually relevant to the user. Relevant recommendations are those the user will probably like the recommended item.

Suppose, the recommendation system recommends top-![Image 51: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) items ![Image 52: \hat{C} = \{c_1, c_2, \dots, c_K\}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20140%2021'%3E%3C/svg%3E) to a user ![Image 53: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) based on their utilities. And assume that the set ![Image 54: C^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2019%2012'%3E%3C/svg%3E) represents the top-![Image 55: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) items that are actually relevant for the user ![Image 56: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) (i.e., ground-truth recommendations). The Precision@K is then defined as

![Image 57: \text{Precision@K} \ = \ \displaystyle\frac{\vert \hat{C} \cap C^*\vert}{K}.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20190%2040'%3E%3C/svg%3E)

* * *

#### [**Recall@K**](#TOC)

Recall measures the effectiveness of a machine learning system. For a recommendation system, Recall@K measures the fraction of relevant items covered in the top-![Image 58: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) recommendations.

Suppose, the recommendation system recommends top-![Image 59: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) items ![Image 60: \hat{C} = \{c_1, c_2,\dots, c_K\}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20140%2021'%3E%3C/svg%3E) to a user ![Image 61: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) based on their utilities. And assume that the set ![Image 62: C^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2019%2012'%3E%3C/svg%3E) represents the top-![Image 63: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) items that are actually relevant for the user ![Image 64: u](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%209%207'%3E%3C/svg%3E) (i.e., ground-truth recommendations). Recall@K is then defined as

![Image 65: \text{Recall@K} \ = \ \displaystyle\frac{\vert \hat{C} \cap C^*\vert}{\vert C^* \vert}.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20169%2044'%3E%3C/svg%3E)

We can define several other metrics based on precision-recall (e.g., F-score, ROC (receiver operator characteristic) curve, AUC (area under the curve), etc.). This is described in **Table 1**.

[![Image 66: Table 1: Summary of various evaluation metrics for recommendation systems (source: Ko et al., 2022).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20254'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/metrics-scaled.jpeg)

**Table 1:** Summary of various evaluation metrics for recommendation systems (source: [Ko et al., 2022](https://www.mdpi.com/2079-9292/11/1/141)).

* * *

#### [**Mean Reciprocal Rank**](#TOC)

Suppose the item ![Image 67: c^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2012'%3E%3C/svg%3E) is the topmost relevant item in the ground-truth recommendations ![Image 68: C^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2019%2012'%3E%3C/svg%3E) and ranks ![Image 69: r](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%208%207'%3E%3C/svg%3E) in the top-![Image 70: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) recommendations ![Image 71: \hat{C} = \{c_1, c_2, \dots, c_K\}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20140%2021'%3E%3C/svg%3E) provided by the recommendation engine. Then, the mean reciprocal rank (MRR) is defined as the “multiplicative inverse” of the rank of the most relevant item ![Image 72: c^*](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2012'%3E%3C/svg%3E):

![Image 73: \text{MRR} \ = \ \displaystyle\frac{1}{Q} \displaystyle\sum_{i=1}^Q \displaystyle\frac{1}{r_i},](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20137%2050'%3E%3C/svg%3E)

where ![Image 74: Q](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2014'%3E%3C/svg%3E) is the number of users for which we have provided recommendations.

* * *

### [**Recommendation Techniques**](#TOC)

Data mining techniques are incredibly valuable for uncovering patterns and correlations within data. This information can then be used to make recommendations (e.g., suggesting similar items or grouping users with similar interests). **Figure 5** provides an overview of the various data mining techniques commonly used in recommendation engines today, and we’ll delve into each of these techniques in more detail.

[![Image 75: Figure 5: Overview of recommendation techniques (source: Ko et al. 2022).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20694%20350'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/techniques.jpeg)

**Figure 5:** Overview of recommendation techniques (source: [Ko et al. 2022](https://www.mdpi.com/2079-9292/11/1/141)).

* * *

#### [**Text Mining**](#TOC)

Text mining (**Figure 6**) is a powerful tool for extracting valuable information from textual data. This can be especially useful when recommending blogs, news articles, and other text-based content. For example, with a content-based recommendation system, text mining can help to identify and recommend similar items by analyzing the underlying semantic structure of each piece. Similarly, for collaborative recommendation models, text mining can help to evaluate the semantic knowledge of information data between users, enabling the system to make more accurate item recommendations based on similarity.

For example, term frequency–inverse document frequency (TF-IDF) (**Figure 7**) is a popular text-mining technique in content-based recommendations. The TF-IDF algorithm assigns weight to different keywords in a text according to the number of repetitions and the whole corpus. It comprises the following:

*   **Term frequency (TF)** assigns weight proportional to the times a keyword occurs in a text. In other words, more repetitions mean that the keyword is essential in the given text.
*   **Inverse document frequency (IDF)** assigns weight inversely proportional to the times the keyword occurs in the whole corpus. It is less important if a keyword is present in most of the text corpus.

This technique expresses a text item as a feature vector, which can be used to compute cosine similarity with other item feature vectors.

* * *

#### [**K-Nearest Neighbor**](#TOC)

K-nearest neighbor (KNN) (**Figure 8**) is an algorithm that can be used to find the ![Image 76: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) closest points for a data point based on a distance measure (e.g., Euclidean distance, cosine similarity, or Pearson correlation). In a collaborative recommendation model, the KNN algorithm can identify the ![Image 77: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest users to a given user. The item ratings of these ![Image 78: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest neighbors are then used to recommend items to the given user. For a content-based recommendation, the algorithm can find ![Image 79: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-similar items for a particular item previously rated by the user.

* * *

#### [**Clustering**](#TOC)

Clustering is a class of algorithms that segregates the data into a set of definite clusters such that similar points lie in the same cluster and dissimilar points lie in different clusters. Several clustering algorithms (e.g., ![Image 80: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-means and spectral clustering) can be used in recommendation engines.

For example, ![Image 81: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-means (**Figure 9**) works by first selecting ![Image 82: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) random points as cluster centers. Then, it assigns each data point in the data to the nearest cluster center. After that, it calculates the mean of all data points assigned to each cluster and moves the cluster center to that mean. This process is repeated until the cluster centers no longer move.

[![Image 83: Figure 9: K-means clustering algorithm (source: Javatpoint).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20561%20283'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/clustering.png)

**Figure 9:** _K_\-means clustering algorithm (source: [Javatpoint](https://www.javatpoint.com/k-means-clustering-algorithm-in-machine-learning)).

Clustering is mainly used in collaborative settings to segregate users into similar interest groups. This way, the recommendation problem boils down to finding relevant items for a cluster rather than for each user separately.

* * *

#### [**Matrix Factorization**](#TOC)

Matrix factorization is a technique used to find the transformation that expresses the user information and preference into the same latent space by decomposing the user’s evaluation data stored in a rating matrix. Singular value decomposition (SVD) is a popular matrix factorization method that transforms the user and selected items into a space of the same latent factor.

To understand this, assume a rating matrix ![Image 84: M](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2017%2011'%3E%3C/svg%3E) (of size ![Image 85: m \times n](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%208'%3E%3C/svg%3E)) whose ![Image 86: i-j^\text{th}](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2045%2017'%3E%3C/svg%3E) entry ![Image 87: M(i,j)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2049%2018'%3E%3C/svg%3E) is `1` if user ![Image 88: i](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%205%2012'%3E%3C/svg%3E) rated the item ![Image 89: j](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%208%2015'%3E%3C/svg%3E) and `0` if it is unrated. Now, given this user-item matrix ![Image 90: M](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2017%2011'%3E%3C/svg%3E), the SVD (**Figure 10**) decomposes that matrix so that you have a user matrix and an item matrix independently. In other words,

![Image 91: M \ = \ U \Sigma V^T,](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20101%2017'%3E%3C/svg%3E)

where ![Image 92: U](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2012%2011'%3E%3C/svg%3E) is a user matrix of size ![Image 93: m \times r](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2042%208'%3E%3C/svg%3E), ![Image 94: \Sigma](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2011%2011'%3E%3C/svg%3E) is a ![Image 95: r \times r](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2036%208'%3E%3C/svg%3E) diagonal matrix consisting of singular values along the principal diagonal, and ![Image 96: V](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2011'%3E%3C/svg%3E) is the item matrix of size ![Image 97: n \times r](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2038%208'%3E%3C/svg%3E). This way, the SVD decomposes the original matrix ![Image 98: M](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2017%2011'%3E%3C/svg%3E) into a user ![Image 99: U](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2012%2011'%3E%3C/svg%3E) and an item ![Image 100: V](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2013%2011'%3E%3C/svg%3E) matrix that transforms the user and items into the same latent factor of dimension ![Image 101: r](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%208%207'%3E%3C/svg%3E). Note that in **Figure 10**, ![Image 102: V^* = V^T](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2064%2014'%3E%3C/svg%3E).

[![Image 103: Figure 10: Singular value decomposition (SVD) (source: Wikipedia).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20531%20483'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2023/06/svd.png)

**Figure 10:** Singular value decomposition (SVD) (source: [Wikipedia](https://en.wikipedia.org/wiki/Singular_value_decomposition)).

SVD is popularly used in recommendation engines to find similar items and users via their respective user and item matrices.

* * *

#### [**Neural Networks**](#TOC)

Neural networks have become increasingly popular in recommendation engines due to their ability to capture complex patterns in user-item data. They can now process various data (e.g., speech, text, images, tables, and graphs), making them suitable for various recommendation settings.

Deep neural networks (**Figure 11**) are used to learn user and item embeddings, which are vectors representing entity features. These embeddings help ensure that similar entities (e.g., users or items) have similar distances in the vector space. Neural networks can also be trained on multiple objectives simultaneously, making them a highly effective data mining technique.

An active area of research in recommendation systems is a bandit-based algorithm, a form of reinforcement learning that tries to balance exploring and exploiting possibilities. These algorithms can consider multiple objectives and metrics related to user satisfaction. For example, an additional objective in a music recommender system can be to provide “fairness” for new and long-tailed artists.

Graph neural networks (GNNs) are another area of active research, as they can extract information from graphs that capture the interactions between customers and items. GNNs have an advantage over sequence-based neural nets as there is no necessarily fixed order of items a user might like.

* * *

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

* * *

[**Summary**](#TOC)
-------------------

In this lesson, we learned the fundamentals of recommendation systems. Recommendation systems deliver personalization by ranking users’ items (e.g., movies) based on their interests and preferences.

Given an item, a recommendation engine measures the usefulness or utility of that item for a user. The central challenge in the recommendation system is to develop the utility function, as it is not defined for all combinations of users and items.

Based on the nature of the utility function, recommendation systems can be categorized into three types:

*   Content-based
*   Collaborative
*   Hybrid

Content-based recommendation systems try to recommend items similar to those the user already liked or rated. On the other hand, collaborative systems suggest items rated or liked by other users with similar interests as the given user. However, both these settings suffer from drawbacks (e.g., sparsity, new user problems, new item problems, overspecialization, etc.). Hybrid systems combine content-based and collaborative methods to avoid these issues.

There are several measures to evaluate the goodness of a recommendation system.

Root mean square error (RMSE) measures how good the predicted utility of an item for a user is compared to actual ratings provided by the user. Precision@K measures the fraction of top-![Image 104: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) recommendations relevant to the user. Recall@K measures the fraction of relevant items which are covered in top-![Image 105: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E) recommendations. Finally, mean reciprocal rank (MRR) is defined as the average “multiplicative inverse” of the rank of the most relevant item.

Various data mining algorithms are used in recommendation systems to derive useful information (e.g., patterns and correlations in data). These techniques include: text mining, nearest neighbor, clustering, matrix factorization, and neural networks.

Text mining is useful in extracting useful information from textual data. It can recommend similar items by performing a semantic analysis of the item.

For example, the K-nearest neighbor algorithm can identify the ![Image 106: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest users to a given user. The item ratings of these ![Image 107: K](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2015%2011'%3E%3C/svg%3E)\-closest neighbors can then be used to recommend items to the given user.

Clustering is mainly used in collaborative settings to segregate users into similar interest groups.

Matrix factorization is a technique used to find the transformation that expresses the user information and preference into the same latent space by decomposing the user’s evaluation data stored in a rating matrix.

Lastly, neural networks can capture complex patterns in user-item data and process various data (e.g., speech, text, images, tables, graphs, etc.).

In subsequent lessons, we will dive into the details of some of the popular recommendation systems around us. So stay tuned for our upcoming tutorial on Netflix movies and series recommendation systems!

* * *

### [**Citation Information**](#TOC)

**Mangla, P.** “Fundamentals of Recommendation Systems,” _PyImageSearch_, P. Chugh, A. R. Gosthipaty, S. Huot, K. Kidriavsteva, and R. Raha, eds., 2023, [https://pyimg.co/9kf03](https://pyimg.co/9kf03)

@incollection{Mangla\_2023\_FRS,
  author = {Puneet Mangla},
  title = {Fundamentals of Recommendation Systems},
  booktitle = {PyImageSearch},
  editor = {Puneet Chugh and Aritra Roy Gosthipaty and Susan Huot and Kseniia Kidriavsteva and Ritwik Raha},
  year = {2023},
  url = {https://pyimg.co/9kf03},
}

* * *

![Image 108: Featured Image](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20276'%3E%3C/svg%3E)

### Unleash the potential of computer vision with Roboflow - Free!

*   Step into the realm of the future by [signing up or logging into your Roboflow account](https://roboflow.com/?ref=pyimagesearch). Unlock a wealth of innovative dataset libraries and revolutionize your computer vision operations.
*   Jumpstart your journey by choosing from our broad array of datasets, or benefit from PyimageSearch’s comprehensive library, crafted to cater to a wide range of requirements.
*   Transfer your data to Roboflow in any of the 40+ compatible formats. Leverage cutting-edge model architectures for training, and deploy seamlessly across diverse platforms, including API, NVIDIA, browser, iOS, and beyond. Integrate our platform effortlessly with your applications or your favorite third-party tools.
*   Equip yourself with the ability to train a potent computer vision model in a mere afternoon. With a few images, you can import data from any source via API, annotate images using our superior cloud-hosted tool, kickstart model training with a single click, and deploy the model via a hosted API endpoint. Tailor your process by opting for a code-centric approach, leveraging our intuitive, cloud-based UI, or combining both to fit your unique needs.
*   Embark on your journey today with absolutely no credit card required. Step into the future with Roboflow.

[Join Roboflow Now](https://roboflow.com/?ref=pyimagesearch)

* * *

![Image 109](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.