Originally published by Susan Li at https://towardsdatascience.com
Logistic Regression is a Machine Learning classification algorithm that is used to predict the probability of a categorical dependent variable. In logistic regression, the dependent variable is a binary variable that contains data coded as 1 (yes, success, etc.) or 0 (no, failure, etc.). In other words, the logistic regression model predicts P(Y=1) as a function of X.
Keeping the above assumptions in mind, let’s look at our dataset.
The dataset comes from the UCI Machine Learning repository, and it is related to direct marketing campaigns (phone calls) of a Portuguese banking institution. The classification goal is to predict whether the client will subscribe (1/0) to a term deposit (variable y). The dataset can be downloaded from here.
import pandas as pdimport numpy as np
from sklearn import preprocessing
import matplotlib.pyplot as plt
plt.rc(“font”, size=14)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import seaborn as sns
sns.set(style=“white”)
sns.set(style=“whitegrid”, color_codes=True)
The dataset provides the bank customers’ information. It includes 41,188 records and 21 fields.
Figure 1
Input variables
Predict variable (desired target):
y — has the client subscribed a term deposit? (binary: “1”, means “Yes”, “0” means “No”)
The education column of the dataset has many categories and we need to reduce the categories for a better modelling. The education column has the following categories:
Figure 2
Let us group “basic.4y”, “basic.9y” and “basic.6y” together and call them “basic”.
data[‘education’]=np.where(data[‘education’] ==‘basic.9y’, ‘Basic’, data[‘education’])
data[‘education’]=np.where(data[‘education’] ==‘basic.6y’, ‘Basic’, data[‘education’])
data[‘education’]=np.where(data[‘education’] ==‘basic.4y’, ‘Basic’, data[‘education’])
After grouping, this is the columns:
Figure 3
Figure 4
count_no_sub = len(data[data[‘y’]==0])
count_sub = len(data[data[‘y’]==1])
pct_of_no_sub = count_no_sub/(count_no_sub+count_sub)
print(“percentage of no subscription is”, pct_of_no_sub100)
pct_of_sub = count_sub/(count_no_sub+count_sub)
print(“percentage of subscription”, pct_of_sub100)
percentage of no subscription is 88.73458288821988
percentage of subscription 11.265417111780131
Our classes are imbalanced, and the ratio of no-subscription to subscription instances is 89:11. Before we go ahead to balance the classes, let’s do some more exploration.
Figure 5
Observations:
We can calculate categorical means for other categorical variables such as education and marital status to get a more detailed sense of our data.
Figure 6
Figure 7
%matplotlib inlinepd.crosstab(data.job,data.y).plot(kind=‘bar’)
plt.title(‘Purchase Frequency for Job Title’)
plt.xlabel(‘Job’)
plt.ylabel(‘Frequency of Purchase’)
plt.savefig(‘purchase_fre_job’)
Figure 8
The frequency of purchase of the deposit depends a great deal on the job title. Thus, the job title can be a good predictor of the outcome variable.
table=pd.crosstab(data.marital,data.y)
table.div(table.sum(1).astype(float), axis=0).plot(kind=‘bar’, stacked=True)
plt.title(‘Stacked Bar Chart of Marital Status vs Purchase’)
plt.xlabel(‘Marital Status’)
plt.ylabel(‘Proportion of Customers’)
plt.savefig(‘mariral_vs_pur_stack’)
Figure 9
The marital status does not seem a strong predictor for the outcome variable.
table=pd.crosstab(data.education,data.y)
table.div(table.sum(1).astype(float), axis=0).plot(kind=‘bar’, stacked=True)
plt.title(‘Stacked Bar Chart of Education vs Purchase’)
plt.xlabel(‘Education’)
plt.ylabel(‘Proportion of Customers’)
plt.savefig(‘edu_vs_pur_stack’)
Figure 10
Education seems a good predictor of the outcome variable.
pd.crosstab(data.day_of_week,data.y).plot(kind=‘bar’)
plt.title(‘Purchase Frequency for Day of Week’)
plt.xlabel(‘Day of Week’)
plt.ylabel(‘Frequency of Purchase’)
plt.savefig(‘pur_dayofweek_bar’)
Figure 11
Day of week may not be a good predictor of the outcome.
pd.crosstab(data.month,data.y).plot(kind=‘bar’)
plt.title(‘Purchase Frequency for Month’)
plt.xlabel(‘Month’)
plt.ylabel(‘Frequency of Purchase’)
plt.savefig(‘pur_fre_month_bar’)
Figure 12
Month might be a good predictor of the outcome variable.
data.age.hist()
plt.title(‘Histogram of Age’)
plt.xlabel(‘Age’)
plt.ylabel(‘Frequency’)
plt.savefig(‘hist_age’)
Figure 13
Most of the customers of the bank in this dataset are in the age range of 30–40.
pd.crosstab(data.poutcome,data.y).plot(kind=‘bar’)
plt.title(‘Purchase Frequency for Poutcome’)
plt.xlabel(‘Poutcome’)
plt.ylabel(‘Frequency of Purchase’)
plt.savefig(‘pur_fre_pout_bar’)
Figure 14
Poutcome seems to be a good predictor of the outcome variable.
That is variables with only two values, zero and one.
cat_vars=[‘job’,‘marital’,‘education’,‘default’,‘housing’,‘loan’,‘contact’,‘month’,‘day_of_week’,‘poutcome’]
for var in cat_vars:
cat_list=‘var’+‘_’+var
cat_list = pd.get_dummies(data[var], prefix=var)
data1=data.join(cat_list)
data=data1cat_vars=[‘job’,‘marital’,‘education’,‘default’,‘housing’,‘loan’,‘contact’,‘month’,‘day_of_week’,‘poutcome’]
data_vars=data.columns.values.tolist()
to_keep=[i for i in data_vars if i not in cat_vars]
Our final data columns will be:
data_final=data[to_keep]
data_final.columns.values
Figure 15
With our training data created, I’ll up-sample the no-subscription using the SMOTE algorithm(Synthetic Minority Oversampling Technique). At a high level, SMOTE:
We are going to implement SMOTE in Python.
X = data_final.loc[:, data_final.columns != ‘y’]
y = data_final.loc[:, data_final.columns == ‘y’]from imblearn.over_sampling import SMOTEos = SMOTE(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
columns = X_train.columnsos_data_X,os_data_y=os.fit_sample(X_train, y_train)
os_data_X = pd.DataFrame(data=os_data_X,columns=columns )
os_data_y= pd.DataFrame(data=os_data_y,columns=[‘y’])we can Check the numbers of our data
print("length of oversampled data is ",len(os_data_X))
print(“Number of no subscription in oversampled data”,len(os_data_y[os_data_y[‘y’]==0]))
print(“Number of subscription”,len(os_data_y[os_data_y[‘y’]==1]))
print("Proportion of no subscription data in oversampled data is ",len(os_data_y[os_data_y[‘y’]==0])/len(os_data_X))
print("Proportion of subscription data in oversampled data is ",len(os_data_y[os_data_y[‘y’]==1])/len(os_data_X))
Figure 16
Now we have a perfect balanced data! You may have noticed that I over-sampled only on the training data, because by oversampling only on the training data, none of the information in the test data is being used to create synthetic observations, therefore, no information will bleed from test data into the model training.
Recursive Feature Elimination (RFE) is based on the idea to repeatedly construct a model and choose either the best or worst performing feature, setting the feature aside and then repeating the process with the rest of the features. This process is applied until all features in the dataset are exhausted. The goal of RFE is to select features by recursively considering smaller and smaller sets of features.
data_final_vars=data_final.columns.values.tolist()y=[‘y’]
X=[i for i in data_final_vars if i not in y]from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegressionlogreg = LogisticRegression()rfe = RFE(logreg, 20)
rfe = rfe.fit(os_data_X, os_data_y.values.ravel())
print(rfe.support_)
print(rfe.ranking_)
Figure 16
The RFE has helped us select the following features: “euribor3m”, “job_blue-collar”, “job_housemaid”, “marital_unknown”, “education_illiterate”, “default_no”, “default_unknown”, “contact_cellular”, “contact_telephone”, “month_apr”, “month_aug”, “month_dec”, “month_jul”, “month_jun”, “month_mar”, “month_may”, “month_nov”, “month_oct”, “poutcome_failure”, “poutcome_success”.
cols=[‘euribor3m’, ‘job_blue-collar’, ‘job_housemaid’, ‘marital_unknown’, ‘education_illiterate’, ‘default_no’, ‘default_unknown’,‘contact_cellular’, ‘contact_telephone’, ‘month_apr’, ‘month_aug’, ‘month_dec’, ‘month_jul’, ‘month_jun’, ‘month_mar’,
‘month_may’, ‘month_nov’, ‘month_oct’, “poutcome_failure”, “poutcome_success”]
X=os_data_X[cols]
y=os_data_y[‘y’]
import statsmodels.api as smlogit_model=sm.Logit(y,X)
result=logit_model.fit()
print(result.summary2())
Figure 17
The p-values for most of the variables are smaller than 0.05, except four variables, therefore, we will remove them.
cols=[‘euribor3m’, ‘job_blue-collar’, ‘job_housemaid’, ‘marital_unknown’, ‘education_illiterate’,
‘month_apr’, ‘month_aug’, ‘month_dec’, ‘month_jul’, ‘month_jun’, ‘month_mar’,
‘month_may’, ‘month_nov’, ‘month_oct’, “poutcome_failure”, “poutcome_success”]
X=os_data_X[cols]
y=os_data_y[‘y’]logit_model=sm.Logit(y,X)
result=logit_model.fit()
print(result.summary2())
Figure 18
from sklearn.linear_model import LogisticRegression
from sklearn import metricsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
Figure 19
Predicting the test set results and calculating the accuracy
y_pred = logreg.predict(X_test)
print(‘Accuracy of logistic regression classifier on test set: {:.2f}’.format(logreg.score(X_test, y_test)))
Accuracy of logistic regression classifier on test set: 0.74
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test, y_pred)
print(confusion_matrix)
[[6124 1542]
[2505 5170]]
The result is telling us that we have 6124+5170 correct predictions and 2505+1542 incorrect predictions.
To quote from Scikit Learn:
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 to not label a sample as positive if it is negative.
The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.
The F-beta score can be interpreted as a weighted harmonic mean of the precision and recall, where an F-beta score reaches its best value at 1 and worst score at 0.
The F-beta score weights the recall more than the precision by a factor of beta. beta = 1.0 means recall and precision are equally important.
The support is the number of occurrences of each class in y_test.
from sklearn.metrics import classification_reportprint(classification_report(y_test, y_pred))
Figure 20
Interpretation: Of the entire test set, 74% of the promoted term deposit were the term deposit that the customers liked. Of the entire test set, 74% of the customer’s preferred term deposits that were promoted.
from sklearn.metrics import roc_auc_scorefrom sklearn.metrics import roc_curve
logit_roc_auc = roc_auc_score(y_test, logreg.predict(X_test))
fpr, tpr, thresholds = roc_curve(y_test, logreg.predict_proba(X_test)[:,1])
plt.figure()
plt.plot(fpr, tpr, label=‘Logistic Regression (area = %0.2f)’ % logit_roc_auc)
plt.plot([0, 1], [0, 1],‘r–’)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’)
plt.title(‘Receiver operating characteristic’)
plt.legend(loc=“lower right”)
plt.savefig(‘Log_ROC’)
plt.show()
Figure 21
The receiver operating characteristic (ROC) curve is another common tool used with binary classifiers. The dotted line represents the ROC curve of a purely random classifier; a good classifier stays as far away from that line as possible (toward the top-left corner).
The Jupyter notebook used to make this post is available here. I would be pleased to receive feedback or questions on any of the above.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Follow us on Facebook | Twitter
☞ Machine Learning A-Z™: Hands-On Python & R In Data Science
☞ Python for Data Science and Machine Learning Bootcamp
☞ Machine Learning, Data Science and Deep Learning with Python
☞ Deep Learning A-Z™: Hands-On Artificial Neural Networks
☞ Artificial Intelligence A-Z™: Learn How To Build An AI
☞ A Complete Machine Learning Project Walk-Through in Python
☞ Machine Learning: how to go from Zero to Hero
☞ Top 18 Machine Learning Platforms For Developers
☞ 10 Amazing Articles On Python Programming And Machine Learning
☞ 100+ Basic Machine Learning Interview Questions and Answers
#python #machine-learning