100% found this document useful (4 votes)
983 views

Machine Learning Project: Sneha Sharma PGPDSBA Mar'21 Group 2

The document outlines a machine learning project to analyze an election dataset and predict which party voters will vote for. It includes: 1) Exploratory data analysis including descriptive statistics, outlier detection, and univariate/bivariate analysis to understand the data. 2) Applying classification models like logistic regression, LDA, KNN, naive bayes and interpreting results. 3) Model tuning using grid search, and ensemble methods like bagging and boosting. Gradient boosting performed best with 89% accuracy on training data. 4) The best model can predict voter behavior with 84% precision and 78% recall, aiding an exit poll analysis for news channels.

Uploaded by

preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
983 views

Machine Learning Project: Sneha Sharma PGPDSBA Mar'21 Group 2

The document outlines a machine learning project to analyze an election dataset and predict which party voters will vote for. It includes: 1) Exploratory data analysis including descriptive statistics, outlier detection, and univariate/bivariate analysis to understand the data. 2) Applying classification models like logistic regression, LDA, KNN, naive bayes and interpreting results. 3) Model tuning using grid search, and ensemble methods like bagging and boosting. Gradient boosting performed best with 89% accuracy on training data. 4) The best model can predict voter behavior with 84% precision and 78% recall, aiding an exit poll analysis for news channels.

Uploaded by

preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

MACHINE

LEARNING
PROJECT

Sneha Sharma
PGPDSBA Mar’21
Group 2
List of Content or Index
1.1 Read the dataset. Do the descriptive statistics and do the null value condition check.
Write an inference on it
1.2 Perform Univariate and Bivariate Analysis. Do exploratory data analysis. Check for
Outliers
1.3 Encode the data (having string values) for Modelling. Is Scaling necessary here or
not? Data Split: Split the data into train and test (70:30)
1.4 Apply Logistic Regression and LDA (linear discriminant analysis)
1.5 Apply KNN Model and Naïve Bayes Model. Interpret the results
1.6 Model Tuning, Bagging (Random Forest should be applied for Bagging), and
Boosting
1.7 Performance Metrics: Check the performance of Predictions on Train and Test sets
using Accuracy, Confusion Matrix, Plot ROC curve and get ROC_AUC score for each
model. Final Model: Compare the models and write inference which model is
best/optimized
1.8 Based on these predictions, what are the insights?
2.1 Find the number of characters, words, and sentences for the mentioned document
2.2 Remove all the stop words from all three speeches
2.3 Which word occurs the most number of times in his inaugural address for each
president? Mention the top three words. (after removing the stop words)
2.4 Plot the word cloud of each of the speeches of the variable

List of Figures
Figure 1:dist plot & box plot for age variable
Figure 2: Frequency distribution of the categorical variables (vote ,
economic .condition. national, economic condition household , Blair , Hague ,
Europe , political knowledge , gender)
Figure 3 : count plots
Figure 4: Box plots for outlier check
Figure 4: Box plots for outlier check
Figure 5: Pair plot
Figure 6: Heat Map
Figure 7: AUC ROC curve for Linear Discriminate Analysis Train
Figure 8: AUC ROC curve for Linear Discriminate Analysis Test
Figure 9: AUC ROC curve for Logistic Regression Train
Figure 10 : AUC ROC curve for Logistic Regression Test
Problem 1:
You are hired by one of the leading news channels CNBE who wants to analyst recent
elections. This survey was conducted on 1525 voters with 9 variables. You have to build
a model, to predict which party a voter will vote for on the basis of the given information,
to create an exit poll that will help in predicting overall win and seats covered by a
particular party.

1.1) Read the dataset. Describe the data briefly. Interpret the inferences for each.
Initial steps like head() .info(), Data Types, etc . Null value check, Summary stats,
Skewness must be discussed.
Dataset head :

Shape of the dataset,

The Election dataset have 1525 rows and 10 columns.


Below are the variables and their data types,
All the variables except vote and gender are int64 datatypes.
But when looking at the values in the dataset for the other variables, they all look like
categorical columns except age.
Removing the unwanted variable “Unnamed : 0”, which is not giving a meaningful information.
And displaying the head of the Election dataset.

From the below snippet it is evident that the dataset does not have null values.

The dataset has few duplicates and removing them is the best choice as duplicates does not add
any value. Below snippet also shows the shape of dataset after removing the duplicates

Descriptive Statistics for the dataset


From the above snippet we can come to a conclusion that the dataset has only one integer
column which is ’age’
The mean and median for the only integer column ‘age’ is almost same indicating the column is
normally distributed.
‘vote’ have two unique values Labour and Conservative, which is also a dependent variable.
‘gender’ has two unique values male and female.
Rest all the columns has object variables with ‘Europe’ being highest having 11 unique values

1.2) Perform EDA (Check the null values, Data types, shape, Univariate, bivariate analysis).
Also check for outliers (4 pts). Interpret the inferences for each (3 pts) Distribution
plots(histogram) or similar plots for the continuous columns. Box plots, Correlation plots.
Appropriate plots for categorical variables. Inferences on each plot. Outliers proportion
should be discussed, and inferences from above used plots should be there. There is no
restriction on how the learner wishes to implement this but the code should be able to
represent the correct output and inferences should be logical and correct.

Univariate Analysis and Outlier Check


Figure 1:dist plot & box plot for age variable

‘age’ is the only integer variable and it is not having outliers. Also, the dist. plot shows that the
variable is normally distributed.

Figure 2: Frequency distribution of the categorical variables.


- votes are large in number for ‘Labour’.
‘female’ voters large in number than ‘male’

Bivariate Analysis
Figure 3 : count plots
Labour gets the highest voting from both female and male voters. Almost in all the categories
Labour is getting the maximum votes. Conservative gets a little bit high votes from Europe ‘11’.
Figure 6: Heat Map

1.3) Encode the data (having string values) for Modelling. Is Scaling necessary here or not?( 2
pts), Data Split: Split the data into train and test (70:30) (2 pts). The learner is expected
to check and comment about the difference in scale of different features on the bases of
appropriate measure for example std dev, variance, etc. Should justify whether there is a
necessity for scaling. Object data should be converted into categorical/numerical data to
fit in the models. (pd.categorical().codes(), pd.get_dummies(drop_first=True)) Data split,
ratio defined for the split, train-test split should be discussed.

Data Preparation:
1. Encode the data (having string values) for Modelling. Is Scaling necessary here or not? Data Split: Split the
data into train and test (70:30).
Encoding the dataset
The variables ‘vote’ and ‘gender’ have string values. Converting them into numeric values for modelling,

Scaling
We are not going to scale the data for Logistic regression, LDA and Naive Bayes models as it is not necessary.

But in case of KNN it is necessary to scale the data, as it a distance-based algorithm (typically based on
Euclidean distance). Scaling the data gives similar weightage to all the variables.

1.4) Apply Logistic Regression and LDA (Linear Discriminant Analysis) (2 pts). Interpret the
inferences of both models (2 pts). Successful implementation of each model. Logical
reason behind the selection of different values for the parameters involved in each
model. Calculate Train and Test Accuracies for each model. Comment on the valid ness of
models (over fitting or under fitting)

Modelling:
Predicting train and test,

## LDA (Linear Discriminant Analysis)

Applying LDA and fitting the training data

#Figure 7: AUC ROC curve for LDA Train

# Figure 8 : AUC ROC curve for LDA Test


Training and Testing results shows that the model is excellent with good precision and recall
values. The LDA model is better than Logistic regression with better Test accuracy and recall
values

# Logistic Regression

Training score
0.8406747891283973
Figure 9 : AUC training

Figure 10 : AUC ROC curve for Logistic Regression Test


The model is not overfitting or underfitting. Training and Testing results shows that the model is
excellent with good precision and recall values.

1.5) Apply KNN Model and Naïve Bayes Model (2pts). Interpret the inferences of each model
(2 pts). Successful implementation of each model. Logical reason behind the selection of
different values for the parameters involved in each model. Calculate Train and Test
Accuracies for each model. Comment on the valid ness of models (over fitting or under
fitting)

# Naive Bayes

Importing GaussianNB from sklearn and applying NB model

Applying KNN and fitting the training data

Confusion matrix and Classification report for Train,


Applying KNN and fitting the testing data
Confusion matrix and Classification report for Test,
Training and Testing results shows that the model neither overfitting nor underfitting.

The Naive Bayes model also performs well with better accuracy and recall values.

Even though NB and KNN have same Train and Test accuracy. Based on their recall value in test
dataset it is evident that KNN performs better than Naive Bayes.

# KNN Model
Scaling the dataset as it is required because KNN is a distance-based algorithm,

Applying KNN and fitting the training data


# plot misclassification error vs k

Training and Testing results shows that the model is excellent with good precision and recall
values. This KNN model have good accuracy and recall values.

1.6) Model Tuning (4 pts) , Bagging ( 1.5 pts) and Boosting (1.5 pts). Apply grid search on each
model (include all models) and make models on best_params. Define a logic behind
choosing particular values for different hyper-parameters for grid search. Compare and
comment on performances of all. Comment on feature importance if applicable.
Successful implementation of both algorithms along with inferences and comments on
the model performances.

# Bagging Train
Decision Tree Classifier

# AUC _ROC Curve Bagging Train

# Bagging Test
# Boosting Train
# Ada Boost
# Gradient Boosting

# AUC _ROC Curve Boosting Train


# ADA Boosting Test

# Gradient Boosting Test


# Gradient Boosting AUC_ROC Curve Test

Model Comparison and Best Model

Gradient Boosting model performs the best with 89% train accuracy. And also have 84%
precision and 78% recall which is better than any other models that we have performed in here
with the Election dataset.

Rest all the models are more or less have same accuracy of 84%.

1.7 Performance Metrics: Check the performance of Predictions on Train and Test sets using
Accuracy, Confusion Matrix, Plot ROC curve and get ROC_AUC score for each model,
classification report (4 pts) Final Model - Compare and comment on all models on the basis of
the performance metrics in a structured tabular manner. Describe on which model is
best/optimized, After comparison which model suits the best for the problem in hand on the
basis of different measures. Comment on the final model.(3 pts)
Decision Tree DT model is overfitted with 100% train accuracy and 80% test accuracy.
Statistical 10% + or – is acceptable, but here it is over 10%. Hence, OVERFITTING Confusion
Matrix and Classification report – Train

Decision Tree Pruned Pruning/tuning DT with gini index and max depth = 4, and the model performance is
better And not overfitting with 84% train accuracy and 81% test accuracy

Ensemble Technique - Bagging (DT used) DT is used as a base estimator for bagging. The model is
OVERFITTING.

Ada Boosting Applying Ada Boosting model and predicting the train and test. The train and test accuracy are
84% and 82% respecting. We have seen models that performs better than this.
Gradient Boosting Gradient Boosting model performs the best with 89% train accuracy and with 83% test
accuracy. The precision, recall and f1 score is also good.

Random Forest - Bagging RF model with bagging applied, performs similar to the normal RF as they are not
different. The model has good recall and precision

KNN
Model Comparison and Best Model

Gradient Boosting model performs the best with 89% train accuracy. And also have 84%
precision and 78% recall which is better than any other models that we have performed in here
with the Election dataset.

Rest all the models are more or less have same accuracy of 84%.

1.8) Based on your analysis and working on the business problem, detail out appropriate insights
and recommendations to help the management solve the business objective. There should be at
least 3-4 Recommendations and insights in total. Recommendations should be easily
understandable and business specific, students should not give any technical suggestions. Full
marks should only be allotted if the recommendations are correct and business specific.

Inference:
1. Based on these predictions, what are the insights?
The important variable in predicting the dependent variables are ‘Hague’ and ‘Blair’

These are the ratings that the people gave to the Leaders of the ‘Labour’ and ‘Conservative’
party.
As the frequency distribution suggests most of the people gave 4 stars to ‘Blair’ and there are
larger number of people gave 2 stars to ‘Hague’ which made an impact in the dependent
variable ‘vote

Model Comparison and Best Model

Gradient Boosting model performs the best with 89% train accuracy. And also have 84%
precision and 78% recall which is better than any other models that we have performed in here
with the Election dataset.

Rest all the models are more or less have same accuracy of 84%.
Problem 2 :
In this particular project, we are going to work on the inaugural corpora from the nltk in
Python. We will be looking at the following speeches of the Presidents of the United
States of America:
1. President Franklin D. Roosevelt in 1941
2. President John F. Kennedy in 1961
3. President Richard Nixon in 1973

2.1) Find the number of characters, words and sentences for the mentioned documents. (Hint:
use .words(), .raw(), .sent() for extracting counts)

President Roosevelt's 1941 speech has 6174 characters 1536 words


and 68 sentences
President Kennedy's 1961 speech has 6202 characters 1546 words
and 52 sentences
President Nixon's 1973 speech has 8122 characters 2028 words and
69 sentences

2.2) Remove all the stop words from the three speeches. Show the word count before and after
the removal of stop words. Show a sample sentence after the removal of stop words.

Converting all the character to lower case and removing all the punctuations

Counting the number of stop words and removing them

Word count after removing all the stop words.

All the stop words have been removed from all the three speeches.
2.3) Which word occurs the most number of times in his inaugural address for each president?
Mention the top three words. (after removing the stop words)

In the below snippets we could see the words that occurred most number of times in their
inaugural address.

President Roosevelt's 1941 speech

President Kennedy's 1961 speech

President Nixon's 1973 speech

Removing the additional stop words from the above snippets and when checking the frequency

Most frequently used words from President Franklin D. Roosevelt’s speech are
• Nation
• Democracy
• Spirit
Most frequently used words from President Richard Nixon’s Speech are
• Peace
• World
• New
• America

Most frequently used words from President John F. Kennedy’s Speech are
• World
• New
• Pledge
• Power

2.4) Plot the word cloud of each of the three speeches. (after removing the stop words)

Word Cloud for President Franklin D. Roosevelt’s speech (after cleaning)!!


Word Cloud for President John F. Kennedy’s Speech (after cleaning)!!
Word Cloud for President Richard Nixon’s Speech (after cleaning)!

You might also like