• Abhishek Singh

Building Recommender system using python

In this tutorial, we are going to make a location-based recommendation system using python and machine learning.

Some other blog post that you may want to read is

1- Introduction

With the increasing volume of online information, the recommender system is the best line of defense for consumer choice. Due to the massive information that is available on the web, due to this reason customer often get confused that which product they have to buy because in web there is a countless number of the product that a seller can offer. That problem is known as the paradox of choice.


Challenge Accepted

To solve this problem the recommendation system was built. Its main aim is to provide a better experience to the customer by giving them their favorite products.

The recommendation system does not provide benefits to the consumer only, it also provides benefits to the online store like Amazon, Flipkart, Alibaba and many more.

Let us take an example, suppose that you are going to buy an Apple laptop from Amazon. Then the amazon recommender system also gives you options to buy hard-disk, laptop cover, mouse and many more products also. So this way the recommender system works.

Some years back Amazon reported that by using a recommender system there is a hike in their sales more than 30%. Same as for Netflix also.

So the question arises is that;-

How does the recommender system work

Basically, the idea is very simple, recommendation lists are generated based on the user preferences, item features and the consumer interaction with the particular product. There are some other additional information also like temporal and spatial data.

Temporal data means the data that is related to the past time. For example- user transaction history shows how frequently does he buy the product from us.

And the second term is the spatial data which means information about a physical object. For example- for which particular product that the user most frequently buys.

Types of recommender system

Recommendation models are mainly categorized into three categories that are

1- Collaborative filtering recommender system

Collaborative system predicts what you like based on the other similar user liked in the past.

collaborative filtering

For example- Jack likes to buy a computer because one of his friends also buys a computer the previous month.

That's how the collaborative system works.

2- Content-based recommender system

content- based recommender system

Content-based systems predict what you like based on what you like in the past. For example- Shane buys an earphone last week then the content-based system also recommends him to buy a headset or an AirPods.

One major issue with content-based systems is it is only limited to the products from similar categories that the user bought in the past. Maybe the user may have different interests. To solve this issue the hybrid recommender system comes into play.

3- Hybrid recommender system

hybrid recommender system using python

It is a combination of collaborative based filtering and content-based filtering. For example- Netflix uses a hybrid recommender system to recommend the movies to the user.

Top 6 Applications of recommender system

1- Products recommendation system that is used by Amazon to recommend the products.

2- Movie recommendation system that is used by Netflix and it is reported that more than 80% of movies that are watched on Netflix came from recommendations.

3-Video recommendation system that is used by YouTube to recommend home-page videos.

4- App recommender system that is used by Google Play and the App Store to recommend similar apps to the user.

5- Location-based recommendation system that is used by an application or service to recommend like best hotels in the XYZ area or the best restaurant in Las Vegas to customers.

6- Facebook recommender system that is used to recommend peoples that you might know.

How to make location-based recommendation system using python

In this example, we are going to make a location-based recommendation system with python and machine learning.

You can download the dataset directly from the Kaggle. Here is the link.

The dataset consists of business information across 11 metropolitan areas in four countries. Some of the features are opening hours, latitude, longitude, reviews, stars, categories and the name of the business along with the id.

Well, keep in mind that the location-based recommendation system post relied on four important external libraries: 1- Plotly- It is used to plotting and interactive charts or graphs. 2- Geopandas- It is used when we are working with geospatial data. 3- Folium- It is used to visualize data on an interactive leaflet map. 4- GDAL- It is used for reading and writing geospatial data.

!pip install git+git://github.com/geopandas/geopandas.git
!pip install folium 
!pip install plotly_express
!apt install gdal-bin python-gdal python3-gdal 

Now let us start coding

Step 1- To load all the required libraries

Step 2- Load the dataset, display the features along with shape.

Step 3- Now its time to get some insights from the data

Now we making a new column called Restaurants which contains all the Restaurants details and then we assign it to a new variable named df_restaurants. After that, we check the shape of the new variable.

Now we plot the ratings of the restaurant in range 1 to 5.

ratings of restaurants

Now we arrange the restaurants in the descending order and then plot the top 20 restaurants with their respective ratings and save the figure in png format.

Top 20 restaurants

Step 4- Now set the map box access token.

For getting the access token you have to create an account in Mapbox and from there you will get the access token.

After that plot the scatter plot of restaurant location i.e latitude and longitude by using our beautiful plotly library.

Now let's plot the restaurants named NV that is in Las Vegas.

Step 5- Now let us use the famous clustering algorithm that is K-Means clustering to get our expected output which is the recommendation of restaurants in a particular place.

To apply K-Means clustering the first step is going to determine the optimal no of the cluster.

There are two methods to find the optimal no clusters are:-

1- Elbow method- According to Wikipedia, the elbow model is defined as the mean squared distance between each instance and its closest centroid. There is a package in machine learning which is sklearn that is used to find optimal no of the cluster by running the K-means algorithm for n number of times and choose the one with having the lowest inertia.

2- Silhouette Score- We use the silhouette coefficient to find the optimal number of clusters. The formula to calculate the silhouette coefficient is

Silhouette Coefficient = (x-y)/ max(x,y)

where y is the mean intracluster distance: mean distance to the other instances in the same cluster. x means nearest cluster distance that is the mean distance to the instances of the next closest cluster.

Elbow method

In the given diagram we can see that by using the elbow method the optimal no of the cluster is 5.

Now let us see the optimal no of the cluster using the silhouette method

Now after that, we apply the k-means algorithm. And predict the location.

And then plot the scatter map box for the prediction.

Wrap up the Session

In this tutorial, we have learned about how to make a recommendation system using python. Now your task is to take this code as a reference and make a Facebook recommendation system using python to recommend friends.

If you like this tutorial please like it and subscribe to our newsletter or you may follow our Facebook page to get a notification when I release any other blog post.