Instagram Reach Analysis

Objective: -

Instagram is one of the most popular social media applications today. People using Instagram professionally are using it for promoting their business, building a portfolio, blogging, and creating various kinds of content. As Instagram is a popular application used by millions of people with different niches, Instagram keeps changing to make itself better for the content creators and the users. But as this keeps changing, it affects the reach of our posts that affects us in the long run. So if a content creator wants to do well on Instagram in the long run, they have to look at the data of their Instagram reach.

The goal of this challenge is to build a machine learning model that predicts the number of impressions on a particular account.

Dataset: -

The dataset used in this model is publicly available on Statso.io

Thirteen three real-valued features:

  1. Total Impressions

  2. Impressions From Home

  3. Impressions From Hashtag

  4. Impressions From Explore

  5. Impressions From Others

  6. Total Profile Saves

  7. Comments

  8. Shares

  9. Likes

  10. Profile Visits

  11. Follows

  12. Captions

  13. Hashtags

Step 1: Import all the required libraries

  • Pandas : In computer programming, pandas is a software library written for the Python programming language for data manipulation and analysis and storing in a proper way. In particular, it offers data structures and operations for manipulating numerical tables and time series

  • Sklearn : Scikit-learn (formerly scikits.learn) is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerical and scientific libraries NumPy and SciPy. The library is built upon the SciPy (Scientific Python) that must be installed before you can use scikit-learn.

  • Pickle : Python pickle module is used for serializing and de-serializing a Python object structure. Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.

  • Seaborn : Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

  • Matplotlib : Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.

#Loading libraries   
import pandas as pd  
import seaborn as sns  
import numpy as np  
import matplotlib.pyplot as plt  
from sklearn.linear_model import LinearRegression, Ridge, RidgeCV, Lasso, LassoCV  
from sklearn.model_selection import KFold, cross_val_score, train_test_split  
import warnings  

warnings.filterwarnings('ignore')

Step 2 : Read dataset and basic details of dataset

Goal:- In this step we are going to read the dataset, view the dataset and analysis the basic details like total number of rows and columns, what are the column data types and see to need to create new column or not.

In this stage we are going to read our problem dataset and have a look on it.

#loading the dataset  
try:  
    df = pd.read_csv('C:/Users/YAJENDRA/Documents/final notebooks/Instagram Reach and Analysis Prediction/data/Instagram.csv',encoding='latin1') #Path for the file  
    print('Data read done successfully...')  
except (FileNotFoundError, IOError):  
    print("Wrong file or file path")
Data read done successfully...
# To view the content inside the dataset we can use the head() method that returns a specified number of rows, string from the top.   
# The head() method returns the first 5 rows if a number is not specified.  
df.head()

png

Step3: Data Preprocessing

Why need of Data Preprocessing?

Preprocessing data is an important step for data analysis. The following are some benefits of preprocessing data:

  • It improves accuracy and reliability. Preprocessing data removes missing or inconsistent data values resulting from human or computer error, which can improve the accuracy and quality of a dataset, making it more reliable.

  • It makes data consistent. When collecting data, it’s possible to have data duplicates, and discarding them during preprocessing can ensure the data values for analysis are consistent, which helps produce accurate results.

  • It increases the data’s algorithm readability. Preprocessing enhances the data’s quality and makes it easier for machine learning algorithms to read, use, and interpret it.

Why we drop column?

By analysing the first five rows we found that there is a column named car_ID,it only shows the serial number so it is not helpful for us so we will drop it.

Axis are defined for arrays with more than one dimension. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0) and the second running horizontally across columns (axis 1).

After we read the data, we can look at the data using:

# count the total number of rows and columns.  
print ('The train data has {0} rows and {1} columns'.format(df.shape[0],df.shape[1]))
The train data has 119 rows and 13 columns

The df.value_counts() method counts the number of types of values a particular column contains.

df.shape
(119, 13)

The df.shape method shows the shape of the dataset.

df.info()
<class 'pandas.core.frame.DataFrame'>  
RangeIndex: 119 entries, 0 to 118  
Data columns (total 13 columns):  
 #   Column          Non-Null Count  Dtype   
---  ------          --------------  -----   
 0   Impressions     119 non-null    int64   
 1   From Home       119 non-null    int64   
 2   From Hashtags   119 non-null    int64   
 3   From Explore    119 non-null    int64   
 4   From Other      119 non-null    int64   
 5   Saves           119 non-null    int64   
 6   Comments        119 non-null    int64   
 7   Shares          119 non-null    int64   
 8   Likes           119 non-null    int64   
 9   Profile Visits  119 non-null    int64   
 10  Follows         119 non-null    int64   
 11  Caption         119 non-null    object  
 12  Hashtags        119 non-null    object  
dtypes: int64(11), object(2)  
memory usage: 12.2+ KB

The df.info() method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage.

df.iloc[1]
Impressions                                                    5394  
From Home                                                      2727  
From Hashtags                                                  1838  
From Explore                                                   1174  
From Other                                                       78  
Saves                                                           194  
Comments                                                          7  
Shares                                                           14  
Likes                                                           224  
Profile Visits                                                   48  
Follows                                                          10  
Caption           Here are some of the best data science project...  
Hashtags          #healthcare #health #covid #data #datascience ...  
Name: 1, dtype: object

df.iloc[ ] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array. The iloc property gets, or sets, the value(s) of the specified indexes.

Data Type Check for every column

Why data type check is required?

Data type check helps us with understanding what type of variables our dataset contains. It helps us with identifying whether to keep that variable or not. If the dataset contains contiguous data, then only float and integer type variables will be beneficial and if we have to classify any value then categorical variables will be beneficial.

objects_cols = ['object']  
objects_lst = list(df.select_dtypes(include=objects_cols).columns)
print("Total number of categorical columns are ", len(objects_lst))  
print("There names are as follows: ", objects_lst)
Total number of categorical columns are  2  
There names are as follows:  ['Caption', 'Hashtags']
int64_cols = ['int64']  
int64_lst = list(df.select_dtypes(include=int64_cols).columns)
print("Total number of numerical columns are ", len(int64_lst))  
print("There names are as follows: ", int64_lst)
Total number of numerical columns are  11  
There names are as follows:  ['Impressions', 'From Home', 'From Hashtags', 'From Explore', 'From Other', 'Saves', 'Comments', 'Shares', 'Likes', 'Profile Visits', 'Follows']
float64_cols = ['float64']  
float64_lst = list(df.select_dtypes(include=float64_cols).columns)
print("Total number of float64 columns are ", len(float64_lst))  
print("There name are as follow: ", float64_lst)
Total number of float64 columns are  0  
There name are as follow:  []

Step 2 Insights: -

  1. We have total 13 features where 11 of them are integer type and 2 are object type.

After this step we have to calculate various evaluation parameters which will help us in cleaning and analysing the data more accurately.

Step 3: Descriptive Analysis

Goal/Purpose: Finding the data distribution of the features. Visualization helps to understand data and also to explain the data to another person.

Things we are going to do in this step:

  1. Mean

  2. Median

  3. Mode

  4. Standard Deviation

  5. Variance

  6. Null Values

  7. NaN Values

  8. Min value

  9. Max value

  10. Count Value

  11. Quatilers

  12. Correlation

  13. Skewness

df.describe()

png

The df.describe() method returns description of the data in the DataFrame. If the DataFrame contains numerical data, the description contains these information for each column: count — The number of not-empty values. mean — The average (mean) value.

Measure the variability of data of the dataset

Variability describes how far apart data points lie from each other and from the center of a distribution.

1. Standard Deviation

The standard deviation is the average amount of variability in your dataset.

It tells you, on average, how far each data point lies from the mean. The larger the standard deviation, the more variable the data set is and if zero variance then there is no variability in the dataset that means there no use of that dataset.

So, it helps in understanding the measurements when the data is distributed. The more the data is distributed, the greater will be the standard deviation of that data.Here, you as an individual can determine which company is beneficial in long term. But, if you didn’t know the SD you would have choosen a wrong compnay for you.

df.std()
Impressions       4843.780105  
From Home         1489.386348  
From Hashtags     1884.361443  
From Explore      2613.026132  
From Other         289.431031  
Saves              156.317731  
Comments             3.544576  
Shares              10.089205  
Likes               82.378947  
Profile Visits      87.088402  
Follows             40.921580  
dtype: float64

We can also understand the standard deviation using the below function.

def std_cal(df,float64_lst):  

    cols = ['normal_value', 'zero_value']  
    zero_value = 0  
    normal_value = 0  

    for value in float64_lst:  

        rs = round(df[value].std(),6)  

        if rs > 0:  
            normal_value = normal_value + 1  

        elif rs == 0:  
            zero_value = zero_value + 1  

    std_total_df =  pd.DataFrame([[normal_value, zero_value]], columns=cols)   

    return std_total_df

2. Variance

The variance is the average of squared deviations from the mean. A deviation from the mean is how far a score lies from the mean.

Variance is the square of the standard deviation. This means that the units of variance are much larger than those of a typical value of a data set.

Why do we used Variance ?

By Squairng the number we get non-negative computation i.e. Disperson cannot be negative. The presence of variance is very important in your dataset because this will allow the model to learn about the different patterns hidden in the data

df.var()
Impressions       2.346221e+07  
From Home         2.218272e+06  
From Hashtags     3.550818e+06  
From Explore      6.827906e+06  
From Other        8.377032e+04  
Saves             2.443523e+04  
Comments          1.256402e+01  
Shares            1.017921e+02  
Likes             6.786291e+03  
Profile Visits    7.584390e+03  
Follows           1.674576e+03  
dtype: float64

We can also understand the Variance using the below function.

zero_cols = []  

def var_cal(df,float64_lst):  

    cols = ['normal_value', 'zero_value']  
    zero_value = 0  
    normal_value = 0  

    for value in float64_lst:  

        rs = round(df[value].var(),6)  

        if rs > 0:  
            normal_value = normal_value + 1  

        elif rs == 0:  
            zero_value = zero_value + 1  
            zero_cols.append(value)  

    var_total_df =  pd.DataFrame([[normal_value, zero_value]], columns=cols)   

    return var_total_df

Measure central tendency

A measure of central tendency is a single value that attempts to describe a set of data by identifying the central position within that set of data. As such, measures of central tendency are sometimes called measures of central location. They are also classed as summary statistics.

Mean — The average value. Median — The mid point value. Mode — The most common value.

1. Mean

1_tjAEMZx_0uIYGUhxEnPXPw.png

The mean is the arithmetic average, and it is probably the measure of central tendency that you are most familiar.

Why do we calculate mean?

The mean is used to summarize a data set. It is a measure of the center of a data set.

df.mean()
Impressions       5703.991597  
From Home         2475.789916  
From Hashtags     1887.512605  
From Explore      1078.100840  
From Other         171.092437  
Saves              153.310924  
Comments             6.663866  
Shares               9.361345  
Likes              173.781513  
Profile Visits      50.621849  
Follows             20.756303  
dtype: float64

We can also understand the mean using the below function.

def mean_cal(df,int64_lst):  

    cols = ['normal_value', 'zero_value']  
    zero_value = 0  
    normal_value = 0  

    for value in int64_lst:  

        rs = round(df[value].mean(),6)  

        if rs > 0:  
            normal_value = normal_value + 1  

        elif rs == 0:  
            zero_value = zero_value + 1  

    mean_total_df =  pd.DataFrame([[normal_value, zero_value]], columns=cols)   

    return mean_total_df

2.Median

Alg1_14_02_0011-diagram_thumb-lg.png

The median is the middle value. It is the value that splits the dataset in half.The median of a dataset is the value that, assuming the dataset is ordered from smallest to largest, falls in the middle. If there are an even number of values in a dataset, the middle two values are the median.

Why do we calculate median ?

By comparing the median to the mean, you can get an idea of the distribution of a dataset. When the mean and the median are the same, the dataset is more or less evenly distributed from the lowest to highest values.The median will depict that the patient below median is Malignent and above that are Benign.

df.median()
Impressions       4289.0  
From Home         2207.0  
From Hashtags     1278.0  
From Explore       326.0  
From Other          74.0  
Saves              109.0  
Comments             6.0  
Shares               6.0  
Likes              151.0  
Profile Visits      23.0  
Follows              8.0  
dtype: float64

We can also understand the median using the below function.

def median_cal(df,int64_lst):  

    cols = ['normal_value', 'zero_value']  
    zero_value = 0  
    normal_value = 0  

    for value in float64_lst:  

        rs = round(df[value].mean(),6)  

        if rs > 0:  
            normal_value = normal_value + 1  

        elif rs == 0:  
            zero_value = zero_value + 1  

    median_total_df =  pd.DataFrame([[normal_value, zero_value]], columns=cols)   

    return median_total_df

Null and Nan values

  1. Null Values

missing-values.png

A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).

df.isnull().sum()
Impressions       0  
From Home         0  
From Hashtags     0  
From Explore      0  
From Other        0  
Saves             0  
Comments          0  
Shares            0  
Likes             0  
Profile Visits    0  
Follows           0  
Caption           0  
Hashtags          0  
dtype: int64

As we notice that there are no null values in our dataset.

  1. Nan Values

images.png

NaN, standing for Not a Number, is a member of a numeric data type that can be interpreted as a value that is undefined or unrepresentable, especially in floating-point arithmetic.

df.isna().sum()
Impressions       0  
From Home         0  
From Hashtags     0  
From Explore      0  
From Other        0  
Saves             0  
Comments          0  
Shares            0  
Likes             0  
Profile Visits    0  
Follows           0  
Caption           0  
Hashtags          0  
dtype: int64

As we notice that there are no nan values in our dataset.

Another way to remove null and nan values is to use the method “df.dropna(inplace=True)”.

Count of unique occurences of every value in all categorical value

for value in objects_lst:  

    print(f"{value:{10}} {df[value].value_counts()}")
Caption    Here are some of the best websites that you can follow to learn everything in data science.                                                                                                                                                                                                          3  
Here are some of the best data science project ideas on healthcare. If you want to become a data science professional in the healthcare domain then you must try to work on these projects.                                                                                                          3  
Here are all the programming languages that Facebook uses in the Front-end and the back-end of Facebook.                                                                                                                                                                                             2  
Each company has its ways of creating an OTP for verification, but most of the companies have their systems programmed to generate a 6-digit random number. So here you will learn the complete process of OTP verification using Python.                                                            2  
Deep Learning is a subset of machine learning. If you want to become a machine learning engineer, you should know everything about deep learning. If you are looking for some of the best free resources to learn deep learning then here are the two best free resources to learn deep learning.    2  
                                                                                                                                                                                                                                                                                                    ..  
Here is how you can prepare yourself for your very first data science interview.                                                                                                                                                                                                                     1  
Here is the difference between the process of Deep Learning and Machine Learning.                                                                                                                                                                                                                    1  
Here’s how you can create a QR code by using the Python programming language.                                                                                                                                                                                                                        1  
Here are some of the best data sources that you can use to collect data for your data science projects.                                                                                                                                                                                              1  
175 Python Projects with Source Code solved and explained for free: Link in Bio                                                                                                                                                                                                                      1  
Name: Caption, Length: 90, dtype: int64  
Hashtags   #data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer                                                                                                                                                          19  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #pythonprojects                                                                                                                                                                                                                                                  8  
#timeseries #time #statistics #datascience #bigdata #machinelearning #python #ai #timeseriesanalysis #datavisualization #dataanalytics #data #iot #analysis #timeseriesmalaysia #artificialintelligence #analytics #amankharwal #thecleverprogrammer                                                                                                                                                                       6  
#programming #coding #programmer #python #developer #javascript #technology #code #coder #java #html #computerscience #tech #css #webdeveloper #software #webdevelopment #codinglife #softwaredeveloper #linux #programmingmemes #webdesign #programmers #php #programminglife #machinelearning #hacking #pythonprogramming #thecleverprogrammer #amankharwal                                                              5  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #sentimentanalysis #sentiment #nlp #naturallanguageprocessing #amankharwal #thecleverprogrammer                                                                                                                                           5  
#deeplearning #machinelearning #artificialintelligence #datascience #ai #python #coding #technology #programming #bigdata #dataanalytics #datascientist #data #computerscience #tech #neuralnetwork #amankharwal #thecleverprogrammer                                                                                                                                                                                      4  
#datascience #datasciencejobs #datasciencetraining #datascienceeducation #datasciencecourse #data #dataanalysis #dataanalytics #datascientist #machinelearning #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer                                                                                                                                   4  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #bitcoin #cryptocurrency                                                                                                                                  3  
#neuralnetwork #machinelearning #artificialintelligence #deeplearning #python #ai #datascience #neuralnetworks #programming #tensorflow #datascientist #pythonprogramming #tech #technology #artificialintelligenceai #machinelearningalgorithms #computerscience #coding #ml #data #amankharwal #thecleverprogrammer                                                                                                      3  
#dataanalytics #datascience #data #machinelearning #datavisualization #bigdata #artificialintelligence #datascientist #python #analytics #ai #dataanalysis #deeplearning #technology #programming #coding #dataanalyst #business #pythonprogramming #datamining #tech #businessintelligence #database #computerscience #statistics #powerbi #dataanalysisprojects #businessanalytics #thecleverprogrammer #amankharwal     3  
#healthcare #health #covid #data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #dataanalyst #amankharwal #thecleverprogrammer                                                                                                                                                                               3  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #roadmap                                                                                                                                                  2  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #pythonprojects #otp #otpverification                                                                                                                                                                                                                            2  
#recommended #recommendations #recommendationsystem #recommendation #data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #amankharwal #thecleverprogrammer                                                                                                                                                   2  
#datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #artificialintelligence #python #datascientist #bigdata #deeplearning #dataviz #ai #analytics #technology #dataanalyst #programming #pythonprogramming #statistics #amankharwal #thecleverprogrammer                                                                                                                                   2  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #machinelearningmodels #zomato #business #casestudy #businessmodel #amazonfinds                                                                           2  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #machinelearningalgorithms #ml #amankharwal #thecleverprogrammer #projects #casestudies                                                                                                                            2  
#coding #programming #programmer #python #developer #javascript #code #coder #technology #html #computerscience #codinglife #java #webdeveloper #tech #css #webdevelopment #software #softwaredeveloper #interview #job #codinginterview #amankharwal #thecleverprogrammer                                                                                                                                                 2  
#machinelearning #machinelearningalgorithms #datascience #dataanalysis #dataanalytics #datascientist #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #amankharwal #thecleverprogrammer #clustering                                                                                                                                                 2  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer                                                                                                                                                                                                                                                                  2  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #machinelearningmodels #stress #stressdetection                                                                                                           2  
#career #job #jobs #jobsearch #education #business #success #careergoals #motivation #work #careerdevelopment #careers #goals #resume #students #careeradvice #datascience #marketing #digitalmarketing #media #socialmedia #IT #webdevelopment #amankharwal #thecleverprogrammer                                                                                                                                          2  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #machinelearningalgorithms #amankharwal #thecleverprogrammer                                                                                                                                                       2  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #machinelearningmodels #zomato #business #casestudy #businessmodel                                                                                        2  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #coding #programming #computerscience #datascience #webdevelopment #machinelearning                                                                                                                                                                              1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #instagram #instagramreach                                                                                                                                1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #whatsapp                                                                                                                                                 1  
#machinelearning #machinelearningalgorithms #datascience #dataanalysis #dataanalytics #datascientist #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #amankharwal #thecleverprogrammer                                                                                                                                                             1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #sentimentanalysis #sentiment #nlp #naturallanguageprocessing #amankharwal #thecleverprogrammer #ukraine                                                                                                                                  1  
#sql #mysql #datascience #datasciencejobs #datasciencetraining #datascienceeducation #datasciencecourse #data #dataanalysis #dataanalytics #datascientist #machinelearning #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer                                                                                                                       1  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #naturallanguageprocessing #nlp                                                                                                                                                                                                                                  1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #news                                                                                                                                                     1  
#datavisualization #datascience #datasciencejobs #datasciencetraining #datascienceeducation #datasciencecourse #data #dataanalysis #dataanalytics #datascientist #machinelearning #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer                                                                                                                1  
#python #pythonprogramming #pythoncode #pythonlearning #pythondeveloper #pythonprogrammer #pythonprojects #python3 #pythoncoding #pythonprogramminglanguage #amankharwal #thecleverprogrammer #nlp #naturallanguageprocessing                                                                                                                                                                                              1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #sentimentanalysis #sentiment #nlp #naturallanguageprocessing #amankharwal #thecleverprogrammer #flipkart                                                                                                                                 1  
#python #pythonprogramming #pythoncode #pythonlearning #pythondeveloper #pythonprogrammer #pythonprojects #python3 #pythoncoding #pythonprogramminglanguage #amankharwal #thecleverprogrammer                                                                                                                                                                                                                              1  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #pythonprogram @codergallery                                                                                                                                                                                                                                     1  
#finance #money #business #investing #investment #trading #stockmarket #data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #dataanalyst #amankharwal #thecleverprogrammer                                                                                                                                   1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #finance #business #money #investing                                                                                                                      1  
#datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #artificialintelligence #python #datascientist #bigdata #deeplearning #dataviz #ai #analytics #technology #dataanalyst #programming #pythonprogramming #statistics #coding #businessintelligence #datamining #tech #business #computerscience #tableau #database #bigdataanalytics #powerbi                                            1  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #pythonprojects #qrcodes                                                                                                                                                                                                                                         1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #interview #neuralnetworks                                                                                                                                1  
#data #technology #datascience #business #tech #bigdata #dataanalytics #machinelearning #ai #analytics #datavisualization #security #artificialintelligence #cybersecurity #programming #python #software #coding #network #internet #cloud #dataanalysis #innovation #datascientist #database #amankharwal #thecleverprogrammer #facebook                                                                                 1  
#data #technology #datascience #business #tech #bigdata #dataanalytics #machinelearning #ai #analytics #datavisualization #security #artificialintelligence #cybersecurity #programming #python #software #coding #network #internet #cloud #dataanalysis #innovation #datascientist #database #amankharwal #thecleverprogrammer #google                                                                                   1  
#datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #artificialintelligence #python #datascientist #bigdata #deeplearning #dataviz #ai #analytics #technology #dataanalyst #programming #pythonprogramming #statistics #coding #businessintelligence #datamining #tech #business #boxplots #thecleverprogrammer #amankharwal                                                               1  
#stockmarket #investing #stocks #trading #money #investment #finance #forex #datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #ai #candlestick #candlestickcharts #amankharwal #thecleverprogrammer                                                                                                                                                                                      1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #machinelearningalgorithms #ml #amankharwal #thecleverprogrammer                                                                                                                                                   1  
#neuralnetwork #machinelearning #artificialintelligence #deeplearning #python #ai #datascience #neuralnetworks #programming #tensorflow #datascientist #pythonprogramming #tech #technology #artificialintelligenceai #machinelearningalgorithms #computerscience #coding #ml #data #amankharwal #thecleverprogrammer #alexnet                                                                                             1  
#python #pythonprogramming #pythonprojects #pythoncode #pythonlearning #pythondeveloper #pythoncoding #pythonprogrammer #amankharwal #thecleverprogrammer #pythonprojects #pythonbooks #bookstagram                                                                                                                                                                                                                        1  
#stockmarket #investing #stocks #trading #money #investment #finance #forex #datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #ai #candlestick #candlestickcharts                                                                                                                                                                                                                        1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #algorithm #algorithms #machinelearningalgorithms #ml #amankharwal #thecleverprogrammer #softskills                                                                                                                                       1  
#datavisualization #datascience #data #dataanalytics #machinelearning #dataanalysis #artificialintelligence #python #datascientist #bigdata #deeplearning #dataviz #ai #analytics #technology #dataanalyst #programming #pythonprogramming #statistics #coding #businessintelligence #datamining #tech #business #computerscience #tableau #database #thecleverprogrammer #amankharwal                                     1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #machinelearningmodels                                                                                                                                    1  
#data #datascience #dataanalysis #dataanalytics #datascientist #machinelearning #python #pythonprogramming #pythonprojects #pythoncode #artificialintelligence #ai #deeplearning #machinelearningprojects #datascienceprojects #amankharwal #thecleverprogrammer #interview #datascienceinterview                                                                                                                          1  
Name: Hashtags, dtype: int64
  • Categorical data are variables that contain label values rather than numeric values.The number of possible values is often limited to a fixed set.

  • Use Label Encoder to label the categorical data. Label Encoder is the part of SciKit Learn library in Python and used to convert categorical data, or text data, into numbers, which our predictive models can better understand.

Skewness

Skewness is a measure of the asymmetry of a distribution. A distribution is asymmetrical when its left and right side are not mirror images. A distribution can have right (or positive), left (or negative), or zero skewness

Why do we calculate Skewness ?

Skewness gives the direction of the outliers if it is right-skewed, most of the outliers are present on the right side of the distribution while if it is left-skewed, most of the outliers will present on the left side of the distribution

Below is the function to calculate skewness.

def right_nor_left(df, int64_lst):  

    temp_skewness = ['column', 'skewness_value', 'skewness (+ve or -ve)']  
    temp_skewness_values  = []  

    temp_total = ["positive (+ve) skewed", "normal distrbution" , "negative (-ve) skewed"]  
    positive = 0  
    negative = 0  
    normal = 0  

    for value in float64_lst:  

        rs = round(df[value].skew(),4)  

        if rs > 0:  
            temp_skewness_values.append([value,rs , "positive (+ve) skewed"])     
            positive = positive + 1  

        elif rs == 0:  
            temp_skewness_values.append([value,rs,"normal distrbution"])  
            normal = normal + 1  

        elif rs < 0:  
            temp_skewness_values.append([value,rs, "negative (-ve) skewed"])  
            negative = negative + 1  

    skewness_df =  pd.DataFrame(temp_skewness_values, columns=temp_skewness)   
    skewness_total_df =  pd.DataFrame([[positive, normal, negative]], columns=temp_total)   

    return skewness_df, skewness_total_df
float64_cols = ['float64']  
float64_lst_col = list(df.select_dtypes(include=float64_cols).columns)  

skew_df,skew_total_df = right_nor_left(df, float64_lst_col)
skew_df

png

skew_total_df

png

We notice with the above results that we have following details:

  1. All columns are positively skewed.

Step 3 Insights: -

With the statistical analysis we have found that the data have a lot of skewness in them all the columns are positively skewed with mostly zero variance.

Statistical analysis is little difficult to understand at one glance so to make it more understandable we will perform visulatization on the data which will help us to understand the process easily.

Why we are calculating all these metrics?

Mean / Median /Mode/ Variance /Standard Deviation are all very basic but very important concept of statistics used in data science. Almost all the machine learning algorithm uses these concepts in data preprocessing steps. These concepts are part of descriptive statistics where we basically used to describe and understand the data for features in Machine learning

Step 4: Data Exploration

Goal/Purpose:

Graphs we are going to develop in this step

  1. Histogram of all columns to check the distrubution of the columns

  2. Distplot or distribution plot of all columns to check the variation in the data distribution

  3. Heatmap to calculate correlation within feature variables

  4. Boxplot to find out outlier in the feature columns

1. Histogram

A histogram is a bar graph-like representation of data that buckets a range of classes into columns along the horizontal x-axis.The vertical y-axis represents the number count or percentage of occurrences in the data for each column

# Distribution in attributes  
%matplotlib inline  
import matplotlib.pyplot as plt  
df.hist(bins=50, figsize=(30,30))  
plt.show()

png

Histogram Insight: -

Histogram helps in identifying the following:

  • View the shape of your data set’s distribution to look for outliers or other significant data points.

  • Determine whether something significant has boccurred from one time period to another.

Why Histogram?

It is used to illustrate the major features of the distribution of the data in a convenient form. It is also useful when dealing with large data sets (greater than 100 observations). It can help detect any unusual observations (outliers) or any gaps in the data.

From the above graphical representation we can identify that the highest bar represents the outliers which is above the maximum range.

We can also identify that the values are moving on the right side, which determines positive and the centered values determines normal skewness.

2. Distplot

A Distplot or distribution plot, depicts the variation in the data distribution. Seaborn Distplot represents the overall distribution of continuous data variables. The Seaborn module along with the Matplotlib module is used to depict the distplot with different variations in it

num = [f for f in df.columns if df.dtypes[f] != 'object']  
nd = pd.melt(df, value_vars = num)  
n1 = sns.FacetGrid (nd, col='variable', col_wrap=4, sharex=False, sharey = False)  
n1 = n1.map(sns.distplot, 'value')  
n1
<seaborn.axisgrid.FacetGrid at 0x26b14117dd0>

png

Distplot Insights: -

Above is the distrution bar graphs to confirm about the statistics of the data about the skewness, the above results are:

  1. All columns are positive skewed

  2. 1 column is added here i.e Impressions which is our target variable ~ which is also +ve skewed. In that case we’ll need to log transform this variable so that it becomes normally distributed. A normally distributed (or close to normal) target variable helps in better modeling the relationship between target and independent variables All the negative skewed columns are those which are encoded so we will not transform them as it will not affect the accuracy.

Why Distplot?

Skewness is demonstrated on a bell curve when data points are not distributed symmetrically to the left and right sides of the median on a bell curve. If the bell curve is shifted to the left or the right, it is said to be skewed.

We can observe that the bell curve is shifted to left we indicates positive skewness.As all the column are positively skewed we don’t need to do scaling.

Let’s proceed and check the distribution of the target variable.

#+ve skewed   
df['Impressions'].skew()
4.181964841810171

The target variable is positively skewed.A normally distributed (or close to normal) target variable helps in better modeling the relationship between target and independent variables.

3. Heatmap

A heatmap (or heat map) is a graphical representation of data where values are depicted by color.Heatmaps make it easy to visualize complex data and understand it at a glance

Correlation — A positive correlation is a relationship between two variables in which both variables move in the same direction. Therefore, when one variable increases as the other variable increases, or one variable decreases while the other decreases.

Correlation can have a value:

  • 1 is a perfect positive correlation

  • 0 is no correlation (the values don’t seem linked at all)

  • -1 is a perfect negative correlation

#correlation plot  
sns.set(rc = {'figure.figsize':(25,20)})  
corr = df.corr().abs()  
sns.heatmap(corr,annot=True)   
plt.show()

png

corr

png

Heatmap insights: -

As we know, it is recommended to avoid correlated features in your dataset. Indeed, a group of highly correlated features will not bring additional information (or just very few), but will increase the complexity of the algorithm, hence increasing the risk of errors.

Why Heatmap?

Heatmaps are used to show relationships between two variables, one plotted on each axis. By observing how cell colors change across each axis, you can observe if there are any patterns in value for one or both variables.

4. Boxplot

211626365402575-b88c4d0fdacd5abb4c3dc2de3bc004bb.png

A boxplot is a standardized way of displaying the distribution of data based on a five number summary (“minimum”, first quartile [Q1], median, third quartile [Q3] and “maximum”).

Basically, to find the outlier in a dataset/column.

features = df.columns.tolist()  
features.remove('Caption')  
features.remove('Hashtags')
sns.boxplot(data=df)

png

png

png

png

png

png

png

png

png

png

png

The dark points are known as Outliers. Outliers are those data points that are significantly different from the rest of the dataset. They are often abnormal observations that skew the data distribution, and arise due to inconsistent data entry, or erroneous observations.

Boxplot Insights: -

  • Sometimes outliers may be an error in the data and should be removed. In this case these points are correct readings yet they are different from the other points that they appear to be incorrect.

  • The best way to decide wether to remove them or not is to train models with and without these data points and compare their validation accuracy.

  • So we will keep it unchanged as it won’t affect our model.

Here, we can see that most of the variables possess outlier values. It would take us days if we start treating these outlier values one by one. Hence, for now we’ll leave them as is and let our algorithm deal with them. As we know, tree-based algorithms are usually robust to outliers.

Why Boxplot?

Box plots are used to show distributions of numeric data values, especially when you want to compare them between multiple groups. They are built to provide high-level information at a glance, offering general information about a group of data’s symmetry, skew, variance, and outliers.

In the next step we will divide our cleaned data into training data and testing data.

Analyzing Instagram Reach

plt.figure(figsize=(10, 8))  
plt.style.use('fivethirtyeight')  
plt.title("Distribution of Impressions From Home")  
sns.distplot(df['From Home'])  
plt.show()

png

Insight:-

The impressions we get from the home section on Instagram shows how much our posts reach our followers. Looking at the impressions from home, We can say it’s hard to reach all our followers daily. Now let’s have a look at the distribution of the impressions we received from hashtags:

plt.figure(figsize=(10, 8))  
plt.title("Distribution of Impressions From Hashtags")  
sns.distplot(df['From Hashtags'])  
plt.show()

png

Insight:-

Hashtags are tools we use to categorize our posts on Instagram so that we can reach more people based on the kind of content we are creating. Looking at hashtag impressions shows that not all posts can be reached using hashtags, but many new users can be reached from hashtags. Now let’s have a look at the distribution of impressions I have received from the explore section of Instagram:

plt.figure(figsize=(10, 8))  
plt.title("Distribution of Impressions From Explore")  
sns.distplot(df['From Explore'])  
plt.show()

png

The explore section of Instagram is the recommendation system of Instagram. It recommends posts to the users based on their preferences and interests. By looking at the impressions I have received from the explore section, I can say that Instagram does not recommend our posts much to the users. Some posts have received a good reach from the explore section, but it’s still very low compared to the reach I receive from hashtags.

Step 2: Data Preparation

Goal:-

Tasks we are going to in this step:

  1. Now we will spearate the target variable and feature columns in two different dataframe and will check the shape of the dataset for validation purpose.

  2. Split dataset into train and test dataset.

  3. Scaling on train dataset.

1. Now we spearate the target variable and feature columns in two different dataframe and will check the shape of the dataset for validation purpose.

X = df[['Likes', 'Saves', 'Comments', 'Shares',   
                   'Profile Visits', 'Follows']]  
y = df["Impressions"]
X.info()
<class 'pandas.core.frame.DataFrame'>  
RangeIndex: 119 entries, 0 to 118  
Data columns (total 6 columns):  
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   Likes           119 non-null    int64  
 1   Saves           119 non-null    int64  
 2   Comments        119 non-null    int64  
 3   Shares          119 non-null    int64  
 4   Profile Visits  119 non-null    int64  
 5   Follows         119 non-null    int64  
dtypes: int64(6)  
memory usage: 5.7 KB
y
0       3920  
1       5394  
2       4021  
3       4528  
4       2518  
       ...    
114    13700  
115     5731  
116     4139  
117    32695  
118    36919  
Name: Impressions, Length: 119, dtype: int64
# Check the shape of X and y variable  
X.shape, y.shape
((119, 6), (119,))
# Reshape the y variable   
y = y.values.reshape(-1,1)
# Again check the shape of X and y variable  
X.shape, y.shape
((119, 6), (119, 1))

2. Spliting the dataset in training and testing data.

Here we are spliting our dataset into 80/20 percentage where 80% dataset goes into the training part and 20% goes into testing part.

# Split the X and y into X_train, X_test, y_train, y_test variables with 80-20% split.  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Check shape of the splitted variables  
X_train.shape, X_test.shape, y_train.shape, y_test.shape
((95, 6), (24, 6), (95, 1), (24, 1))

Insights: -

Train test split technique is used to estimate the performance of machine learning algorithms which are used to make predictions on data not used to train the model.It is a fast and easy procedure to perform, the results of which allow you to compare the performance of machine learning algorithms for your predictive modeling problem. Although simple to use and interpret, there are times when the procedure should not be used, such as when you have a small dataset and situations where additional configuration is required, such as when it is used for classification and the dataset is not balanced.

In the next step we will train our model on the basis of our training and testing data.

Step 3: Model Training

Goal:

In this step we are going to train our dataset on different regression algorithms. As we know that our target variable is in continuous format so we have to apply regression algorithm. Target variable is a continous value.In our dataset we have the outcome variable or Dependent variable i.e Price. So we will use Regression algorithm**

Algorithms we are going to use in this step

  1. Linear Regression

  2. Lasso Regression

  3. Random Forest Regressor

K-fold cross validation is a procedure used to estimate the skill of the model on new data. There are common tactics that you can use to select the value of k for your dataset. There are commonly used variations on cross-validation, such as stratified and repeated, that are available in scikit-learn

# Define kfold with 10 split  
cv = KFold(n_splits=10, shuffle=True, random_state=42)

The goal of cross-validation is to test the model’s ability to predict new data that was not used in estimating it, in order to flag problems like overfitting or selection bias and to give an insight on how the model will generalize to an independent dataset (i.e., an unknown dataset, for instance from a real problem).

1. Linear Regression

Linear regression analysis is used to predict the value of a variable based on the value of another variable. The variable you want to predict is called the dependent variable. The variable you are using to predict the other variable’s value is called the independent variable.

This form of analysis estimates the coefficients of the linear equation, involving one or more independent variables that best predict the value of the dependent variable. Linear regression fits a straight line or surface that minimizes the discrepancies between predicted and actual output values. There are simple linear regression calculators that use a “least squares” method to discover the best-fit line for a set of paired data. You then estimate the value of X (dependent variable) from Y (independent variable).

Train set cross-validation

#Using linear regression on our training data   
from sklearn.linear_model import LinearRegression
reg = LinearRegression()  
reg.fit(X_train,y_train)

LinearRegression()

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

LinearRegression

LinearRegression()

#Accuracy check of trainig data  

#Get R2 score  
reg.score(X_train, y_train)
0.8794668724408663
#Accuracy of test data  
reg.score(X_test, y_test)
0.8777977785012776
# Getting kfold values  
lg_scores = -1 * cross_val_score(reg,   
                                 X_train,   
                                 y_train,   
                                 cv=cv,   
                                 scoring='neg_root_mean_squared_error')  
lg_scores
array([1730.12815324, 1171.81370916, 1078.29142847, 1063.05383355,  
       2160.48842498,  959.76443058,  596.25936396, 1184.02847618,  
       2088.43027444, 5253.04422525])
# Mean of the train kfold scores  
lg_score_train = np.mean(lg_scores)  
lg_score_train
1728.5302319810362

Prediction

Now we will perform prediction on the dataset using Logistic Regression.

# Predict the values on X_test_scaled dataset   
y_predicted = reg.predict(X_test)

Various parameters are calculated for analysing the predictions.

  1. Confusion Matrix 2)Classification Report 3)Accuracy Score 4)Precision Score 5)Recall Score 6)F1 Score

Confusion Matrix

A confusion matrix presents a table layout of the different outcomes of the prediction and results of a classification problem and helps visualize its outcomes. It plots a table of all the predicted and actual values of a classifier.

confusion-matrix.jpeg

This diagram helps in understanding the concept of confusion matrix.

Evaluating all kinds of evaluating parameters.

Classification Report :

A classification report is a performance evaluation metric in machine learning. It is used to show the precision, recall, F1 Score, and support of your trained classification model.

F1_score :

The F1 score is a machine learning metric that can be used in classification models.

Precision_score :

The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative. The best value is 1 and the worst value is 0.

Recall_score :

Recall score is used to measure the model performance in terms of measuring the count of true positives in a correct manner out of all the actual positive values. Precision-Recall score is a useful measure of success of prediction when the classes are very imbalanced.

#Measuring Accuracy  
from sklearn.metrics import classification_report   
from sklearn.metrics import f1_score, accuracy_score, precision_score,recall_score,r2_score  
from sklearn.metrics import confusion_matrix  
from sklearn.metrics import mean_squared_error    

print("The model used is Linear Regression")  

l_acc = r2_score(y_test, y_predicted)  
print("\nThe accuracy is: {}".format(l_acc))
The model used is Linear Regression  

The accuracy is: 0.8777977785012776

As the data is continuous we cannot create confusion matrix and other evaluating parameters.

2. Lasso Regression

Lasso regression is a regularization technique. It is used over regression methods for a more accurate prediction. This model uses shrinkage. Shrinkage is where data values are shrunk towards a central point as the mean. The lasso procedure encourages simple, sparse models (i.e. models with fewer parameters). This particular type of regression is well-suited for models showing high levels of multicollinearity or when you want to automate certain parts of model selection, like variable selection/parameter elimination.

#Using Lasso Regression  
from sklearn import linear_model  
lreg = linear_model.Lasso(alpha=0.1)  
lreg.fit(X_train, y_train)

Lasso(alpha=0.1)

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Lasso

Lasso(alpha=0.1)

#Accuracy check of trainig data  
#Get R2 score  
lreg.score(X_train, y_train)
0.8794668723660407
#Accuracy of test data  
lreg.score(X_test, y_test)
0.877800440900774
#Get kfold values  
Nn_scores = -1 * cross_val_score(lreg,   
                                    X_train,   
                                    y_train,   
                                    cv=cv,   
                                    scoring='neg_root_mean_squared_error')  
Nn_scores
array([1730.11450564, 1171.79912123, 1078.29620089, 1063.04653654,  
       2160.46480348,  959.74178451,  596.24691991, 1184.03580545,  
       2088.44941172, 5253.03776997])
# Mean of the train kfold scores  
Nn_score_train = np.mean(Nn_scores)  
Nn_score_train
1728.5232859351806

Prediction

# Predict the values on X_test_scaled dataset   
y_predicted = lreg.predict(X_test)

Evaluating all kinds of evaluating parameters.

#Measuring Accuracy  
from sklearn.metrics import classification_report   
from sklearn.metrics import f1_score, accuracy_score, precision_score,recall_score  
from sklearn.metrics import confusion_matrix  


print("The model used is Lasso Regression")  

k_acc = r2_score(y_test, y_predicted)  
print("\nThe accuracy is: {}".format(k_acc))
The model used is Lasso Regression  

The accuracy is: 0.877800440900774

3. Random Forest Regressor

Random Forest is an ensemble technique capable of performing both regression and classification tasks with the use of multiple decision trees and a technique called Bootstrap and Aggregation, commonly known as bagging. The basic idea behind this is to combine multiple decision trees in determining the final output rather than relying on individual decision trees. Random Forest has multiple decision trees as base learning models

#Using Random Forest Regressor  
from sklearn.ensemble import RandomForestRegressor  
rig = RandomForestRegressor(max_depth=2, random_state=0)  
rig.fit(X_train,y_train)

RandomForestRegressor(max_depth=2, random_state=0)

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

RandomForestRegressor

RandomForestRegressor(max_depth=2, random_state=0)

#Accuracy check of trainig data  
#Get R2 score  
rig.score(X_train, y_train)
0.886741168055421
#Accuracy of test data  
rig.score(X_test, y_test)
0.8576507021983018
# Get kfold values  
Dta_scores = -1 * cross_val_score(rig,   
                                    X_train,   
                                    y_train,   
                                    cv=cv,   
                                    scoring='neg_root_mean_squared_error')  
Dta_scores
array([1340.12058533,  866.77243219, 1230.81334736, 1934.96155301,  
       2716.47086053, 1406.39272283,  807.41378493, 1800.8922652 ,  
        831.75933748, 8445.09267765])
# Mean of the train kfold scores  
Dta_score_train = np.mean(Dta_scores)  
Dta_score_train
2138.0689566500305

Prediction

# predict the values on X_test_scaled dataset   
y_predicted = rig.predict(X_test)

Evaluating all kinds of evaluating parameters.

#Measuring Accuracy  
from sklearn.metrics import classification_report   
from sklearn.metrics import f1_score, accuracy_score, precision_score,recall_score  
from sklearn.metrics import confusion_matrix  


print("The model used is Random Forest Regressor")  

r_acc = r2_score(y_test, y_predicted)  
print("\nThe accuracy is {}".format(r_acc))
The model used is Random Forest Regressor  

The accuracy is 0.8576507021983018

Insight: -

cal_metric=pd.DataFrame([l_acc,k_acc,r_acc],columns=["Score in percentage"])  
cal_metric.index=['Linear Regression',  
                  'Lasso Regression',  
                  'Random Forest Regressor']  
cal_metric

png

  • As you can see that Random Forest Regressor is good at handling outliers so we will save our model with that.

Step 4: Save Model

Goal:- In this step we are going to save our model in pickel format file.

import pickle  
pickle.dump(reg , open('Instagrm_Reach_LinearRegression.pkl', 'wb'))  
pickle.dump(lreg , open('Instagrm_Reach_LassoRegression.pkl', 'wb'))  
pickle.dump(rig , open('Instagrm_Reach_RandomRegressor.pkl', 'wb'))
import pickle  

def model_prediction(features):  

    pickled_model = pickle.load(open('Instagrm_Reach_RandomRegressor.pkl', 'rb'))  
    Impression = str(list(pickled_model.predict(features)))  

    return str(f'The Impressions on your account are:- {Impression}')

We can test our model by giving our own parameters or features to predict.

Likes = 162  
Saves = 98  
Comments = 9  
Shares = 5  
Profile_Visits = 35  
Follows = 2
print(model_prediction([[Likes,Saves, Comments, Shares,Profile_Visits,Follows]]))
The Impressions on your account are:- [4387.478621221635]

Conclusion

After observing the problem statement we have build an efficient model to overcome it. The above model helps in predicting the number of Impressions on our account. It helps the customer to analyze the Impressions on their account.

Checkout whole project codehere(github repo).

🚀 Unlock Your Dream Job with HiDevs Community!

🔍 Seeking the perfect job?HiDevs Community is your gateway to career success in the tech industry. Explore free expert courses, job-seeking support, and career transformation tips.

💼 We offer an upskill program in Gen AI, Data Science, Machine Learning, and assist startups in adopting Gen AI at minimal development costs.

🆓 Best of all, everything we offer is completely free! We are dedicated to helping society.

Book free of cost 1:1 mentorship on any topic of your choice —topmate

✨ We dedicate over 30 minutes to each applicant’s resume, LinkedIn profile, mock interview, and upskill program. If you’d like our guidance, check out our services here

💡 Join us now, and turbocharge your career!

Deepak Chawla LinkedIn
Vijendra Singh LinkedIn
Yajendra Prajapati LinkedIn
YouTube Channel
Instagram Page
HiDevs LinkedIn
Project Youtube Link