[Moro et al., 2014] S. Moro, P. Cortez and P. Rita. A Data-Driven Approach to Predict the Success of Bank Telemarketing. Decision Support Systems, Elsevier, 62:22-31, June 2014
Description:
The data is from direct marketing campaigns of a Portuguese banking institution. The marketing campaigns were based on phone calls. Often, more than one contact to the same client was required, in order to access if the product (bank term deposit) would be ‘yes’ or ‘no’ for subscription.
The data set contains the bank-additional-full.csv with all examples (41188) and 20 inputs, ordered by date (from May 2008 to November 2010), very close to the data analyzed in  [Moro et al., 2014]. You can download the data set from the following link:
https://s3.amazonaws.com/acadgildsite/wordpress_images/datasets/bank/bank-additional-full.csv
Attribute Information:
Input variables:
# bank client data:
1 – age (numeric)
2 – job : type of job (categorical: ‘admin.’,’blue-collar’,’entrepreneur’,’housemaid’,’management’,’retired’,’self-employed’,’services’,’student’,’technician’,’unemployed’,’unknown’)
3 – marital : marital status (categorical: ‘divorced’,’married’,’single’,’unknown’; note: ‘divorced’ means divorced or widowed)
4 – education (categorical: ‘basic.4y’,’basic.6y’,’basic.9y’,’high.school’,’illiterate’,’professional.course’,’university.degree’,’unknown’)
5 – default: has credit in default? (categorical: ‘no’,’yes’,’unknown’)
6 – housing: has housing loan? (categorical: ‘no’,’yes’,’unknown’)
7 – loan: has personal loan? (categorical: ‘no’,’yes’,’unknown’)
# related with the last contact of the current campaign:
8 – contact: contact communication type (categorical: ‘cellular’,’telephone’)
9 – month: last contact month of year (categorical: ‘jan’, ‘feb’, ‘mar’, …, ‘nov’, ‘dec’)
10 – day_of_week: last contact day of the week (categorical: ‘mon’,’tue’,’wed’,’thu’,’fri’)
11 – duration: last contact duration, in seconds (numeric). Important note: this attribute highly affects the output target (e.g., if duration=0 then y=’no’). Yet, the duration is not known before a call is performed. Also, after the end of the call y is obviously known. Thus, this input should only be included for benchmark purposes and should be discarded if the intention is to have a realistic predictive model.
# other attributes:
12 – campaign: number of contacts performed during this campaign and for this client (numeric, includes last contact)
13 – pdays: number of days that passed by after the client was last contacted from a previous campaign (numeric; 999 means client was not previously contacted)
14 – previous: number of contacts performed before this campaign and for this client (numeric)
15 – poutcome: outcome of the previous marketing campaign (categorical: ‘failure’,’nonexistent’,’success’)
# social and economic context attributes
16 – emp.var.rate: employment variation rate – quarterly indicator (numeric)
17 – cons.price.idx: consumer price index – monthly indicator (numeric)
18 – cons.conf.idx: consumer confidence index – monthly indicator (numeric)
19 – euribor3m: euribor 3 month rate – daily indicator (numeric)
20 – nr.employed: number of employees – quarterly indicator (numeric)
Output variable (desired target):
21 – y – has the client subscribed a term deposit? (binary: ‘yes’,’no’)
Problem Statement:Â The data is from direct marketing campaigns (phone calls) of a Portuguese banking institution. The classification goal is to predict if the client will subscribe a term deposit.
Â
Import libraries and tools
Output: Index([‘age’, ‘job’, ‘marital’, ‘education’, ‘default’, ‘housing’, ‘loan’,
‘contact’, ‘month’, ‘day_of_week’, ‘duration’, ‘campaign’, ‘pdays’,
‘previous’, ‘poutcome’, ’emp.var.rate’, ‘cons.price.idx’,
‘cons.conf.idx’, ‘euribor3m’, ‘nr.employed’, ‘y’],
dtype=’object’)
Comment on Features
## 1. age
## 2 .job
Output:
job
admin.          0.129726
blue-collar     0.068943
entrepreneur    0.085165
housemaid       0.100000
management      0.112175
retired         0.252326
self-employed   0.104856
services        0.081381
student         0.314286
technician      0.108260
unemployed      0.142012
unknown         0.112121
Name: outcome, dtype: float64
## 3. default
Output:
default
no               0.12879
unknown   0.05153
yes              0.00000
Name: outcome, dtype: float64
Output:
no              32588
unknown    8597
yes                     3
Name: default, dtype: int64
## 4. contact
## 5. month
Output:
month
apr   0.204787
aug   0.106021
dec   0.489011
jul   0.090466
jun   0.105115
mar   0.505495
may   0.064347
nov   0.101439
oct   0.438719
sep   0.449123
Name: outcome, dtype: float64
Output:
## 6. duration
## 7.1. previous
Output:
previous
0Â Â Â 0.088322
1Â Â Â 0.212015
2Â Â Â 0.464191
3Â Â Â 0.592593
4Â Â Â 0.542857
5Â Â Â 0.722222
6Â Â Â 0.600000
7Â Â Â 0.000000
Name: outcome, dtype: float64
## 7.2. poutcome
poutcome
failure       0.142286
nonexistent   0.088322
success       0.651129
Name: outcome, dtype: float64
## 8. euribor3m
Model building
Output:
# evaluate the model by splitting into train and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=12)
Output:
Misclassified samples: 1401
Accuracy: 0.89
The model actually has a 100% accuracy score, since this is a very simplistic data set with distinctly separable classes. But there you have it. That’s how to implement K-Nearest Neighbors with scikit-learn. Load your favorite data set and give it a try!
How to Decide the Value of N-Neighbours
# Choosing a large value of K will lead to greater amount of execution time & under fitting. Selecting the #small value of K will lead to over fitting. There is no guaranteed way to find the best value of K.
Plots of Accuracy v/s Neighbours