Building a Predictive Model in Python
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.
- 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.
- 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.
- 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.
- 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?
- 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.
- 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.
- We collect data from multi-sources and gather it to analyze and create our role model.
- 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.
- It involves managing gathered data.
- Managing the data refers to checking whether the data is well organized or not.
- This step involves saving the finalized or organized data craving our machine by installing the same by using the prerequisite algorithm
- 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 %.
- 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.
index sl age job marital_status education default balance housing loan contact day month duration campaign pdays previous poutcome y 0 0 30 unemployed married primary no 1787 no no cellular 19 oct 79 1 A 0 unknown no 1 1 3 services married secondary no 4789 yes yes cellular 11 may 220 1 339 4 failure no 2 2 35 management single tertiary no 1350 yes no cellular 16 apr 185 1 330 1 failure no 3 3 30 management married tertiary no 1476 yes yes unknown 3 jun 199 4 4 0 unknown no 4 4 59 blue_collar married secondary no 0 yes no unknown 5 may 226 1 A 0 unknown no #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.heatmap(data.corr(), annot = True)
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
print(data.shape) (5, 18) data.education.unique() array(['primary', 'secondary', 'tertiary'], dtype=object) pd.crosstab(index = data["education"], columns = data["y"])
data.education.value_counts().plot(kind = "barh")
data_y = pd.DataFrame(data_new['y']) data_x = data_new.drop(['y'], axis = 1) print(data_y.columns) print(data_x.columns)
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.