Introduction
When it comes to data science or machine learning, the first thing that crosses our mind is prediction, recommendation systems, or stuff like that. Actually, recommendation systems are pretty common these days. If we talk about some of the most popular websites like Amazon, eBay, and let’s not forget about Facebook, you’ll see those recommendation systems in action. You would definitely have come across something with the tag ‘you might be interested in’, ‘you might know this person’ or ‘people also searched for’ kind of things. So I decided to take a look at how things work and here I am. We’ll talk about some basic and common types of recommendation systems and how they work and will develop them using Python. One thing to be noted; these systems do not match the quality, complexity or accuracy used by the tech companies but will just give you the idea and a starting point.
Environment Setup
Ipython notebook or now known as a Jupyter notebook is one of the most commonly used techs for scientific computation. The main reason for its usage is because it excels in literate programming. In other words, it has the ability to re-run the portion of a program instead of running the whole which is convenient when dealing with large datasets. The easiest way to get the Jupyter notebook app is installing a scientific Python distribution; the most common of which is Anaconda. You can download the Anaconda distribution from https://www.anaconda.com/download/ and simply install it using default settings for a single user.
Our environment is all set up now so let’s actually do something. Create a new folder naming Book Recommendation System (I named it this way because we are going to build a book recommendation system, but you can name it anything.) Now launch the Anaconda command prompt and start a new notebook by entering the following command.
- $ jupyter notebook
You should see the following screen.

What it did is create an empty notebook inside our mentioned folder and will also launch a web-based interactive environment for you to work in. You can easily rename the file in your browser. Now let’s talk about some commonly used recommendation systems and see things in action.
Note
This article assumes your very basic understanding of working with data science libraries of Python. Even if you're new to this go ahead as I tried breaking down things easy even for a newbie. Also the dataset I’m going to use for this article is rather a small dataset based on collected data from Amazon and Goodreads. You can download it and feel free to experiment. The code will also work fine with any other datasets.
Popularity based Recommender
This is the most basic recommendation system which offers a generalized recommendation to every user based on the popularity. But it does make sense even with all the simplicity. Let’s take the scenario of an ice cream parlor. Every other customer orders the chocolate flavor so indeed that is more popular among the customers and is a hit of that ice cream parlor. So if a new customer walks in and asks for the best, he would get a suggestion to try the chocolate flavor. The same is true about tourist attractions, hotel recommendations, movies, books, music, etc. whatever is more popular among the general public, is more likely to be recommended to the new customers.
As mentioned before, this type of recommender makes generalized recommendations, not personalized. It means that this system will not take into account the ‘personal’ preferences or choices, rather it would tell you that this particular thing is liked by most of the users.
Building one will clarify the idea behind it. Let’s get started.
- # In[1]:
- #importing libraries
- import pandas as pd
- import numpy as np
Pandas and Numpy are two powerful libraries provided by Python for scientific computation, data manipulation, and data analysis. Numpy; above all; provides high performance, multi-dimensional array along with the tools to manipulate it. Whereas Pandas is known for its data structures and operations for manipulating data. We will be using both of these libraries in this article.
- # In[2]:
- #reading the files
- data = pd.read_csv('listing.csv', encoding = 'latin-1')
- books = pd.read_csv('books.csv', encoding = 'latin-1')
- # In[3]:
- #use head() function to view first 5 rows for the object based on position. This is just to test if we have right data.
- data.head()

- # In[4]:
- books.head()
- # In[5]:
- # Getting recommendation based on No. Of ratings
- rating_count = pd.DataFrame(books, columns=['book_id','no_of_ratings'])
- # Sorting and dropping the duplicates
- rating_count.sort_values('no_of_ratings', ascending=False).drop_duplicates().head(10)

- # In[6]:
- # getting the detail of 5 most rated books
- most_rated_books = pd.DataFrame([4755, 2409, 2194, 4696, 1616], index=np.arange(5), columns=['book_id'])
- detail = pd.merge(most_rated_books, data, on='book_id')
- detail
You can also get only the highest-rated books as follows.
- # In[7]:
- # getting the most rated book
- most_rated_book = pd.DataFrame(books, columns=['book_id', 'user_id', 'avg_rating', 'no_of_ratings'])
- most_rated_book.max()

- # In[8]:
- #getting description for most rated book
- most_rated_book.describe()

You can also get the description of any column using the same function.
- # In[9]:
- # description for author
- data['author'].describe()

Correlation-Based Recommender
As this is an age of more ‘personalized’ stuff so, popularity based recommenders are not enough to satisfy the need. Thus, there exist Correlation Based Recommenders which would make the recommendations based on the similarity of items (review similarity we’re talking about). The basic idea behind it, being that if you like this item, you are probably going to like an item similar to it. Correlation-Based Recommenders are a simpler form of collaborative filtering based recommenders. They give you more flavor of being personalized as they would recommend the item that is most similar to the item selected before.
We are going to use Pearson’s correlation for our recommendation system. This recommendation system would use item-based similarity; correlate the items based on user ratings.
- # In[1]:
- # importing libraries
- import pandas as pd
- import numpy as np
- # In[2]:
- # reading files
- data = pd.read_csv('listing.csv', encoding = 'latin-1')
- books = pd.read_csv('books.csv', encoding = 'latin-1')
- # In[3]:
- # Checking the data using head function
- books.head()

- # In[4]:
- # calculating the mean
- rating = pd.DataFrame(books.groupby('book_id')['no_of_ratings'].mean())
- rating.head()

- # In[5]:
- # getting the description of rating
- rating.describe()

- # In[6]:
- # sorting based on no of ratings that each book got
- rating.sort_values('no_of_ratings', ascending=False).head()

- # In[7]:
- # Preparing data table for analysis
- ratings_pivot = pd.pivot_table(data=books, values='user_rating', index='user_id', columns='book_id')
- ratings_pivot.head()












Gaius CaesarPosted Sep 29, 2019, 2:21 PM
Hello, how can I implement content based recommendation using C#, Do you have any idea about implement it?Using movielens dataset.
Sahiner GulerPosted Aug 2, 2019, 3:59 PM
Hi, i have something to ask you. Can i ask you by email? Can you contact me please? My email is [email protected]. Thanks
Aaron EPosted Nov 6, 2018, 3:43 PM
Hello, I am a beginner in Python and I am trying to implement something similar to your code to my own dataset. I was wondering where and how you collected your dataset with all the user information.
Abhishek MishraPosted Sep 19, 2018, 8:50 AM
Good one. Well articulated