Building a Predictive Model in Python

Ask Python (1)

Today we are going to learn a fascinating topic which is How to create a predictive model in python. It is an essential concept in Machine Learning and Data Science. Before getting deep into it, We need to understand what is predictive analysis. Let us look at the table of contents.

What is Predictive Analysis?

Predictive analysis is a field of Data Science, which involves making predictions of future events. We can create predictions about new data for fire or in upcoming days and make the machine supportable for the same. We use various statistical techniques to analyze the present data or observations and predict for future.

Why should we use Predictive Analysis?

If done correctly, Predictive analysis can provide several benefits. Some key features that are highly responsible for choosing the predictive analysis are as follows.

  • Immediate Feedback System
    • It provides a better marketing strategy as well.
    • In the case of taking marketing services or any business, We can get an idea about how people are liking it, How much people are liking it, and above all what extra features they really want to be added.
    • It figures out the current trend.
  • Optimization
    • We can optimize our prediction as well as the upcoming strategy using predictive analysis.
    • It is similar to modification.
    • It involves a comparison between present, past and upcoming strategies.
  • Better strategy
    • We end up with a better strategy using this Immediate feedback system and optimization process.
    • It also provides multiple strategies as well.
    • It leads the better decisions.
  • Risk Reduction
    • When we do not know about optimization not aware of a feedback system, We just can do Rist reduction as well.
    • It is best suitable for newcomers.

Predictive Analysis use cases

  • Churn Prevention
    • It allows us to predict whether a person is going to be in our strategy or not. Whether he/she is satisfied or not.
    • we get analysis based pon customer uses. Using that we can prevail offers and we can get to know what they really want.
  • Quality Assurance
    • We can understand how customers feel by using our service by providing forms, interviews, etc.
    • What actually the people want and about different people and different thoughts.
    • What about the new features needed to be installed and about their circumstances?
  • Risk Modelling
    • It allows us to know about the extent of risks going to be involved. so that we can invest in it as well.
    • Analyzing current strategies and predicting future strategies.
  • Sales Forecasting
    • How it is going in the present strategies and what it s going to be in the upcoming days.
    • Analyzing the same and creating organized data.

Steps involved in Predictive Analysis

  • Problem Definition
    • It aims to determine what our problem is. We need to resolve the same. The main problem for which we need to predict.
    • Every field of predictive analysis needs to be based on This problem definition as well.
  • Data Gathering
    • We collect data from multi-sources and gather it to analyze and create our role model.
  • Data cleaning
    • We can take a look at the missing value and which are not important. They need to be removed.
    • We need to improve the quality of this model by optimizing it in this way.
    • We need to remove the values beyond the boundary level.
  • Data Analysis
    • It involves managing gathered data.
    • Managing the data refers to checking whether the data is well organized or not.
  • Modeling
    • This step involves saving the finalized or organized data craving our machine by installing the same by using the prerequisite algorithm
  • Model Testing
    • We need to test the machine whether is working up to mark or not.
    • We need to check or compare the output result/values with the predictive values.
    • Analyzing the compared data within a range that is o to 1 where 0 refers to 0% and 1 refers to 100 %.
  • Deployment
    • Once our model is created or it is performing well up or it’s getting the success accuracy score then we need to deploy it for market use.

Applications of Predictive Analysis

Predictive can build future projections that will help in many businesses as follows:

  • Pricing
  • Demand Planning
  • Campaign Management
  • Customer Acquisition
  • Budgeting and Forecasting
  • Fraud detection
  • Promotions

Creating our Predictive model (Example)

Let us try a demo of predictive analysis using google collab by taking a dataset collected from a banking campaign for a specific offer. Analyzing the data and getting to know whether they are going to avail of the offer or not by taking some sample interviews.

indexslagejobmarital_statuseducationdefaultbalancehousingloancontactdaymonthdurationcampaignpdayspreviouspoutcomey
0030unemployedmarriedprimaryno1787nonocellular19oct791A0unknownno
113servicesmarriedsecondaryno4789yesyescellular11may22013394failureno
2235managementsingletertiaryno1350yesnocellular16apr18513301failureno
3330managementmarriedtertiaryno1476yesyesunknown3jun199440unknownno
4459blue_collarmarriedsecondaryno0yesnounknown5may2261A0unknownno
#importing modules
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

data = pd.read_csv("/dataset.csv", delimiter = ",", header = "infer")
data.head()
sns.pairplot(data)
Capture 2
Capture 2
data.corr()
Capture 3
Capture 3
sns.heatmap(data.corr(), annot = True)
Capture 4
Capture 4
data.dtypes

sl                 int64
age                int64
job               object
marital_status    object
education         object
default           object
balance            int64
housing           object
loan              object
contact           object
day                int64
month             object
duration           int64
campaign           int64
pdays             object
previous           int64
poutcome          object
y                 object
dtype: object
data_new = pd.get_dummies(data, columns=['marital_status',	'education',	'default',	'housing',	'loan',	'contact',	'month', 'poutcome'	])
data_new.y.replace(('yes','no'), (1,0), inplace = True)
data_new.dtypes
Capture 5
Capture 5
print(data.shape)
  (5, 18)
data.education.unique()
  array(['primary', 'secondary', 'tertiary'], dtype=object)
pd.crosstab(index = data["education"], columns = data["y"])
Capture 6
Capture 6
data.education.value_counts().plot(kind = "barh")
Capture 7
Capture 7
data_y = pd.DataFrame(data_new['y'])
data_x = data_new.drop(['y'], axis = 1)
print(data_y.columns)
print(data_x.columns)
Capture 8
Capture 8
x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size = 0.3, random_state = 2, stratify = data_y)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
#OUTPUT FOR THE ABOVE CODE
(3, 27)
(2, 27)
(3, 1)
(2, 1)

Summary

Today we covered predictive analysis and tried a demo using a sample dataset. Hope you must have tried along with our code snippet. You can try taking more datasets as well. We must visit again with some more exciting topics.