DSBA
Project 6
- Machine Learning
Rajendra M Bhat
Problem 1:
You are hired by one of the leading news channel CNBE who wants to analyze 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.
Data Ingestion:
1. Read the dataset. Do the descriptive statistics and do null value condition check.
Write an inference on it.
On reading data set and ‘using info()’ command it is seen that Election Data set has
10 columns and 1525 rows
Column Unnamed:0 is a serial number and cannot used for analysis and Unnamed:0 is
dropped. Remaining data set will be 1525X9 columns. ‘vote’ and ‘gender’ are object
variables and other seven variables are numerical.
From null value condition check it observed that there are no ‘null’ values in the dataset.
There are 8 duplicate records are there in the data set as below.
These duplicate records may affect the prediction and hence duplicate records removed.
Remaining data set is 1517 rows and 9 columns.
Description of data set is as below.
Checked for ‘0’ values. Only ‘[Link]” has ‘0’ values which is valid values.
2. Perform Univariate and Bivariate Analysis. Do exploratory data analysis. Check for
Outliers.
Gender-wise distribution of ‘vote’
28.6 % of male have voted for ‘Conservative party’ and 71.4% of males have voted for
‘Labour party’ 31.8 % of female have voted for ‘Conservative party ’ and 68.2% of females
have voted for ‘Labour party’
Overall 30.3 % have voted for ‘Conservative party’ and 69.7% have voted for ‘Labour
party’.
Histograms of numerical variables is as below.
Bar plots of numerical variables is as below.
There are no outliers in the data set . Age has nearly normal distribution All other
variables have multimodel distrubtion
From the plot it can be seen that
1. Young people have voted for Labour party whereas elder people have voted for
conservative party.
2. People having higher Eurosceptic sentiment have voted for conservative party and
less Eurosceptic sentiment have voted for Labour party
Variables are not correlated and there is no multicollinearity exists in the dataset.
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).
Encoded data and splitted data into train and test
feature: vote
['Labour', 'Conservative']
Categories (2, object): ['Conservative', 'Labour']
[1 0]
'Conservative' is represented by 0 and 'Labour' is represented by 1
feature: gender
['female', 'male']
Categories (2, object): ['female', 'male']
[0 1]
'female' is represented by 0 and 'male' is represented by 1
Given data set is not scaled and some models like KNN need data scaling as these
are distance-based models. Min-max scaling ensures all features have the exact
same scale but does not handle outliers well. Since this data set does not have
outliers, scaling of data done using min-max scaling.
After deleting duplicate records, data set has 1517 rows X 9 columns. Data set has
been split into Train and Test data in ratio 70:30
X_train has 1061 rows X 8 columns, y_train has 1061 rows X1 column.
X_test has 456 rows X8 columns, y_test has 456 rows X 1 column.
Modelling:
1. Apply Logistic Regression and LDA (linear discriminant analysis).
Logistic Regression
Co-efficients of Logistic regression analysis are as below.
coef
Blair 2.260286
[Link] 1.341051
[Link] 0.555891
gender 0.244673
[Link] -1.062728
age -1.148900
Europe -1.994639
Hague -2.955287
Both train and test data have nearly same accuracy level of 0.83
Classification Report of the test data:
precision recall f1-score support
0.0 0.75 0.64 0.69 132
1.0 0.86 0.91 0.89 324
accuracy 0.83 456
macro avg 0.81 0.77 0.79 456
weighted avg 0.83 0.83 0.83 456
In 75% cases where model has predicted vote as Conservative party were correct and
in 86% cases where model has predicted vote as Labour party were correct.
LDA (linear discriminant analysis)
Co-efficients of LDA are as below.
coef
Blair 2.990521
[Link] 1.615459
[Link] 0.516754
gender 0.189480
[Link] -1.437890
age -1.726578
Europe -2.322135
Hague -3.681419
Both train and test data have nearly same accuracy level of 0.83
Classification Report of the test data:
precision recall f1-score support
0.0 0.74 0.66 0.70 132
1.0 0.87 0.90 0.89 324
accuracy 0.83 456
macro avg 0.80 0.78 0.79 456
weighted avg 0.83 0.83 0.83 456
In 74% cases where model has predicted vote as Conservative party were correct and
in 87% cases where model has predicted vote as Labour party were correct.
2. Apply KNN Model and Naïve Bayes Model. Interpret the results.
KNN model
Accuracy score for train data is 1.00 and test data is 0.813
In 68% cases where model has predicted vote as Conservative party were correct and
in 87% cases where model has predicted vote as Labour party were correct.
Naïve Bayes Model.
Accuracy for train data is 0.825 where as for test data it is 0.846
Classification Report Test data
precision recall f1-score support
1 0.89 0.90 0.89 324
0 0.74 0.72 0.73 132
accuracy 0.85 456
macro avg 0.81 0.81 0.81 456
weighted avg 0.85 0.85 0.85 456
In 89% cases where model has predicted vote as Conservative party were correct and
in 74% cases where model has predicted vote as Labour party were correct.
3. Model Tuning, Bagging (Random Forest should be applied for Bagging) and
Boosting.
Model turning: Grid search is used for arriving best parameters of Random
Forest classifier and best parameter are as below.
{'max_depth': 9,
'max_features': 3,
'min_samples_leaf': 30,
'min_samples_split': 100,
'n_estimators': 501}
Accuracy for train data is .8275 and for test data 0.8289
Bagging Classifier
Accuracy for Train data is 0.80 and for test data 0.82
Boosting Classifier
Accuracy for Train data is 0.83 and for test data 0.85
4. 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. (7 marks)
Model : Logistic Regression- Performance indicators
Performance Train data Test data
indicators
Accuracy 0.8312912346842601 0.8333333333333334
Confusion [[215 113] [[ 84 48]
Matrix [ 66 667]] [ 28 296]]
Classification
Report
ROC Curve
ROC_AUC RoC AUC Score: 0.888 Roc_Auc_Score 0.891
score
Linear Discriminant Analysis- Performance indicators
Train data Test data
Accuracy 0.8312912346842601 0.8333333333333334
Confusion [[222 106] [[ 87 45]
Matrix [ 73 660]] [ 31 293]]
Classification
Report
ROC Curve
ROC_AUC RoC AUC Score: 0.887 AUC for the Test Data: 0.891
score
KNN - Performance indicators
Train data Test data
Accuracy 1.0 0.8135964912280702
Confusion [[328 0] [[ 90 42]
Matrix [ 0 733]] [ 43 281]]
Classification
Report
ROC Curve
ROC_AUC 1.00 0.838
score
Naïve Bayes Model. Performance indicators
Train data Test data
Accuracy 0.825636192271442 0.8464912280701754
Confusion [[230 98] [[ 95 37]
Matrix [ 87 646]] [ 33 291]]
Classification
Report
ROC Curve
ROC_AUC 0.883 0.886
score
Random Forest Performance indicators
Train data Test data
Accuracy 0.827521206409048 0.8289473684210527
Confusion [[191 137] [[ 73 59]
Matrix [ 46 687]] [ 19 305]]
Classification
Report
ROC Curve
ROC_AUC 0.899 0.883
score
Bagging Classifier Performance indicators
Train data Test data
Accuracy 0.8096135721017907 0.8201754385964912
Confusion [[168 160] [[ 65 67]
Matrix [ 42 691]] [ 15 309]]
Classification
Report
ROC Curve
ROC_AUC 0.892 0.885
score
AdaBoost Classifier Performance indicators
Train data Test data
Accuracy 0.8341187558906692 0.8464912280701754
Confusion [[230 98] [[ 90 42]
Matrix [ 78 655]] [ 28 296]]
Classification
Report
ROC Curve
ROC_AUC 0.895 0.887
score
Among above models AdaBoost Classifier has shown best performance in test data and
therefore AdaBoost Classifier can be used as final model for prediction.
Inference:
1. Based on these predictions, what are the insights?
Objective is creating an exit poll to guide in predicting overall trend. From the analysis important
factors in deciding the vote are
❖ Assessment of Blair and Hague
❖ Level of Eurosceptic Sentiment
❖ Assessment of Economic Conditions -National
❖ Assessment of Economic Conditions - Household
❖ Level of Political Knowledge
❖ Age
Gender is not important criteria for deciding vote. Using these variables, it is possible to predict
vote/exit poll by about 84 % correctly using these variables.
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:
• Find the number of characters, words and sentences for the mentioned documents.
Number of characters, words and sentences are as under.
• Remove all the stopwords from all the three speeches
Number of stopwords counted for each file and number of words before removing
stopwords and after removing stopwords are as under.
• Which word occurs the most number of times in his inaugural address for each
president? Mention the top three words. (after removing the stopwords)
• Plot the word cloud of each of the speeches of the variable. (after removing the
stopwords)
• Word Cloud for 1941-Roosevelt (after cleaning)!!
Word Cloud for 1961-Kennedy (after cleaning)!
Word Cloud for 1973-Nixon (after cleaning)!!