Instagram Recommendation System with Machine Learning

Instagram is one of the popular social media applications today. People use Instagram to share photos and videos. One of the reasons behind the popularity of Instagram is its recommendation system. It helps in grabbing the attention of the user for a longer time. So, if you want to learn how to use machine learning to recommend Instagram posts, this article is for you. In this article, I will take you through how to create an Instagram Recommendation System with Machine Learning using Python.

How does Instagram Recommendation System Works?

The post you see as a suggested post on Instagram when you scroll through your feed is where Instagram uses a recommendation system to recommend posts that may interest you.

Instagram also use a recommender system to suggest more accounts to follow, but here I will take you through how the recommender system of Instagram recommends more posts.

The suggested posts you see on Instagram are recommended based on your activities on Instagram, such as:

  1. What kind of accounts do you follow, and what kind of posts do you engage with
  2. The caption of the posts that you engage with also plays a role in suggesting more similar posts
  3. How do other users with similar interests as yours engage with the posts

So these were the most common factors for suggesting more posts on Instagram. In the section below, I will take you through how to create an Instagram Recommendation system by using the captions of the Instagram posts.

Instagram Recommendation System using Python

The dataset I am using for creating an Instagram recommendation system is collected from my Instagram account. You can download the dataset from here. Now let’s import the necessary Python libraries and the dataset to get started with this task:

import pandas as pd
import numpy as np
from sklearn.feature_extraction import text
from sklearn.metrics.pairwise import cosine_similarity

data = pd.read_csv("Instagram data.csv")
print(data.head())
         Date  Impressions  From Home  From Hashtags  From Explore  \
0  2021-12-10         3920       2586           1028           619   
1  2021-12-11         5394       2727           1838          1174   
2  2021-12-12         4021       2085           1188             0   
3  2021-12-13         4528       2700            621           932   
4  2021-12-14         2518       1704            255           279   

   From Other  Saves  Comments  Shares  Likes  Profile Visits  Follows  \
0          56     98         9       5    162              35        2   
1          78    194         7      14    224              48       10   
2         533     41        11       1    131              62       12   
3          73    172        10       7    213              23        8   
4          37     96         5       4    123               8        0   

   Conversion Rate                                            Caption  \
0         5.714286  Here are some of the most important data visua...   
1        20.833333  Here are some of the best data science project...   
2        19.354839  Learn how to train a machine learning model an...   
3        34.782609  Here’s how you can write a Python program to d...   
4         0.000000  Plotting annotations while visualizing your da...   

                                            Hashtags  
0  #finance #money #business #investing #investme...  
1  #healthcare #health #covid #data #datascience ...  
2  #data #datascience #dataanalysis #dataanalytic...  
3  #python #pythonprogramming #pythonprojects #py...  
4  #datavisualization #datascience #data #dataana...  

I will only choose the caption and the hashtags column for the rest of the task:

data = data[["Caption", "Hashtags"]]
print(data.head())
                                             Caption  \
0  Here are some of the most important data visua...   
1  Here are some of the best data science project...   
2  Learn how to train a machine learning model an...   
3  Here’s how you can write a Python program to d...   
4  Plotting annotations while visualizing your da...   

                                            Hashtags  
0  #finance #money #business #investing #investme...  
1  #healthcare #health #covid #data #datascience ...  
2  #data #datascience #dataanalysis #dataanalytic...  
3  #python #pythonprogramming #pythonprojects #py...  
4  #datavisualization #datascience #data #dataana...  

Here I will use cosine similarity in machine learning to find similarities between the captions. After finding similarities between the posts, we can recommend them to the user based on the similarities to the post the user just interacted with. Below is how we can recommend Instagram posts:

captions = data["Caption"].tolist()
uni_tfidf = text.TfidfVectorizer(input=captions, stop_words="english")
uni_matrix = uni_tfidf.fit_transform(captions)
uni_sim = cosine_similarity(uni_matrix)

def recommend_post(x):
  return ", ".join(data["Caption"].loc[x.argsort()[-5:-1]])

data["Recommended Post"] = [recommend_post(x) for x in uni_sim]
print(data.head())
                                             Caption  \
0  Here are some of the most important data visua...   
1  Here are some of the best data science project...   
2  Learn how to train a machine learning model an...   
3  Here’s how you can write a Python program to d...   
4  Plotting annotations while visualizing your da...   

                                            Hashtags  \
0  #finance #money #business #investing #investme...   
1  #healthcare #health #covid #data #datascience ...   
2  #data #datascience #dataanalysis #dataanalytic...   
3  #python #pythonprogramming #pythonprojects #py...   
4  #datavisualization #datascience #data #dataana...   

                                    Recommended Post  
0  Here are some of the most important tools that...  
1  Here are some of the best data science project...  
2  Data Science Use Cases: Here’s how Zomato is u...  
3  Here’s how to write a Python function to rever...  
4  Practice these 90+ Data Science Projects For B...  

Now let’s have a look at the recommendations of an Instagram post from the dataset:

print(data["Recommended Post"][3])
Here’s how to write a Python function to reverse a string., 
To calculate the execution time of the program, we need to calculate the time taken by the program from its initiation to the final result. Here’s how to calculate the execution time of a Python program., 
Here’s how to calculate execution time of a Python program., 
Grouping anagrams is one of the popular questions in coding interviews. Here you will be given a list of words, and you have to write an algorithm to group all the words which are anagrams of each other. Here's how to group anagrams using Python.
[ ]

So this is how you can recommend Instagram posts based on the captions of the posts.

Summary

The post you see as a suggested post on Instagram when you scroll through your feed is where Instagram uses a recommender system to recommend posts that may interest you. It helps in grabbing the attention of the user for a longer time. I hope you liked this article on creating an Instagram Recommendation System with Machine Learning using Python. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1237

Leave a Reply