Recommendation Engine : Python
Now a days systems are guiding you through out your life with the help of recommendation engine. They recommend you the products based on your interests. For example, if you are searching for a good movie in website like IMDb or Netflix it provides you recommendations based on your interest.
It applies not only for movies but also in other services like online shopping, online courses, etc. And how it is possible ? Any guesses ?
Yes. It is possible with python on top of Machine Learning. And let’s move into the core part.
What is a recommendation system?
Technically speaking, a recommendation system is a subclass of information filtering system that seeks to predict the “rating” or “preference” a user would give to an item.
Practically speaking, this system provides you items or products based on your past purchases or interests.
Types of making recommendation systems?
- Content-based recommendation — based on product attributes
- Collaborative filtering — based on similar users
Content based recommendation:
System use the knowledge of each product to recommend a new product.
Example: If you are interested to watch a romantic movie, then the system would recommend you the list of movies in that genre.
It works well if you have descriptive data available for the product you have to recommend.
Collaborative filtering:
System don’t have any knowledge about the product but it recommend only based on the user’s rating in the past.
Example: Consider that you rated a list of movies. If you gave 4.5/5 for an horror movie and 1.5 for an adventure movie, then the system would recommend horror movies in the future.
Now let us move on to the steps for building a system.
We make use of huge data sets in this module.
List of data sets used: movies.csv , movie_ratings_data_set.csv
Representing product reviews in the form of matrix:
# Convert the running list of user ratings into a matrix using the 'pivot table' function
ratings_df = pd.pivot_table(df, index='user_id', columns='movie_id', aggfunc=np.max)
print(ratings_df)#aggfunc is used to handle duplicate data. If an user rated the same movie twice it would consider the maximum value for the duplicates.
Handling missing data in the matrix:
Every human is unique in their interests. Assume that the user ratings are the reflection of how much a movie appeals the user’s unique set of interests.
For example: If user #1 gives 5 rating to movie #1, then obviously he/she might give the same rating to other movie in that genre.
It is done with the help of matrix factorization and it will be discussed below.
User Ratings:
The steps involved in calculating user’s ratings are
- Model how the movie appeals the interests.
- Model the user’s interest.
- Measure how well the user’s interest matches the movie attributes.
- Modelling a movies appeal (matrix M):
- Action appeal rating
- Drama appeal rating
- Romance appeal rating
- Arthouse appeal rating
- Crowd-pleaser appeal rating
Now we have the attributes and we shall give ratings based on the listed attributes from -5 to +5.
Get Ganapathi Subramanian’s stories in your inbox
Join Medium for free to get updates from this writer.
2. Modelling user’s interest (matrix U):
- Action interest rating
- Drama interest rating
- Romance interest rating
- Arthouse interest rating
- Crowd-pleaser interest rating
Similarly, we shall give ratings to user based on the listed attributed from -5 to +5.
3. Calculating user’s interest:
Multiplying matrix M and matrix U gives us a score for each attribute. If we do this for each user, we could get the review matrix for all the movies.
Based on linear algebra, we represent matrices as,
We could use numpy library to perform this operation with just a single line of code.
Matrix Factorization:
Matrix Factorization is applied to find the latent(hidden) features :
U,M=matrix_factorization_utilities.low_rank_matrix_factorization(ratings_df.as_matrix(),num_features=15,regularization_amount=0.1)Multiplied to find the predicted ratings :
predicted_ratings = np.matmul(U, M)Now the predicted ratings are as follows.
For user #1 it could be seen that the maximum score is obtained for movie #34. So that is the first movie we should recommend him/her. While we recommend to the user, the user might give ratings to our prediction and we may add it to that matrix for future calculations.
“More the ratings we have, less number of holes in the array and the better optimization to come up with the accurate values for the matrices.”
Working of Matrix Factorization:
Matrix factorization is an idea where large matrix could be broken down into smaller matrices. It is used to fill up the holes in the ratings data set as the users might not give ratings to some movies. It is just a backward operation from a larger matrix to smaller matrices using an iterative algorithm. At first we guess, then check and after several iterations we get closer to the correct answer.
Step 1 :
Set the elements of U and M matrices with random numbers and if we multiply both of it then the result will be random.
Step 2 :
Check how different is the calculated matrices from the real matrix. Obtain a cost function. We call the difference as cost.
“The Cost is how wrong we are”
Step 3:
To use numerical optimization algorithm to search into the minimum cost. The goal is to get the cost function a little closer to zero. The numerical optimization algorithm will tweak the numbers little at a time at each iteration.
Scipy’s fmin_cg() function is used to find the minimum cost.
Step 4:
Step 3 is repeated until we get the minimum cost.
At some point of iteration, we will get the minimum cost and the values of U and M at time is the values which we use. If we again multiply U and M we will get a little difference from the original matrix but it is not a major flaw as we are closer to it!
Product Similarity using latent representations:
Our system couldn’t recommend any product until the user gives any ratings or reviews (i.e; first time users). For this kind of users we could show some similar products to one which they looking at. With the product attributes calculated using matrix factorization we could also calculate the product similarity.
After concurrent operations to find similarity we get the result as follows
Finalized Product:
Our product gives the recommendation based on the given user id. It trains the system based on the past reviews and gives us the result which will be very much closer to the set of interests.
Avoiding Overfitting:
Consider movies of two different genres like horror-comedy and serious horror, a bad recommendation system might take the attribute horror by discarding everything else. This could be avoided by regularization with which we could reduce the amount of weight on a single attribute.
“Higher the regularization amount, lesser the weight on a single attribute”
References:
- “Film Recommendation Systems using Matrix Factorization and Collaborative Filtering” DOI: 10.1109/ICITSI.2014.7048228
Thank you… Have a great day :)








