Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • K-Nearest Neighbors Case Study 1

K-Nearest Neighbors Case Study 1

 July 14  | 0 Comments

[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



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:

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’,’’,’illiterate’,’professional.course’,’’,’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

import pandas as pd
#read the csv file and store it in ‘bank’ dataframe
bank = pd.read_csv(‘datasets/bank-additional/bank-additional-full.csv’,sep=‘;’)
# list all columns (for reference)

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’],


# y (response)
# convert the response to numeric values and store as a new column
bank[‘outcome’] ={‘no’:0, ‘yes’:1})

Comment on Features

## 1. age

%matplotlib inline
# probably not a great feature since lot of outliers

## 2 .job

## useful features as all values revolve around same space



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

# create job_dummies (we will add it to the bank DataFrame later)
job_dummies = pd.get_dummies(bank.job, prefix=‘job’)
job_dummies.drop(job_dummies.columns[0], axis=1, inplace=True)

## 3. default

# looks like a useful feature



no                0.12879

unknown    0.05153

yes               0.00000

Name: outcome, dtype: float64

# but only one person in the dataset has a status of yes


no               32588

unknown     8597

yes                      3

Name: default, dtype: int64

# so, let’s treat this as a 2-class feature rather than a 3-class feature
bank[‘default’] ={‘no’:0, ‘unknown’:1, ‘yes’:1})

## 4. contact

# convert the feature to numeric values
bank[‘contact’] ={‘cellular’:0, ‘telephone’:1})

## 5. month

# looks like a useful feature at first glance



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

# but, it looks like their success rate is actually just correlated with number of calls
# thus, the month feature is unlikely to generalize
bank.groupby(‘month’).outcome.agg([‘count’, ‘mean’]).sort_values(‘count’)



## 6.  duration

# looks like an excellent feature, but you can’t know the duration of a call beforehand, thus it can’t be used in your model
bank.boxplot(column=‘duration’, by=‘outcome’)



## 7.1. previous

# looks like a useful feature



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

# looks like a useful feature


failure        0.142286

nonexistent    0.088322

success        0.651129

Name: outcome, dtype: float64

# create poutcome_dummies
poutcome_dummies = pd.get_dummies(bank.poutcome, prefix=‘poutcome’)
poutcome_dummies.drop(poutcome_dummies.columns[0], axis=1, inplace=True)
# concatenate bank DataFrame with job_dummies and poutcome_dummies
bank = pd.concat([bank, job_dummies, poutcome_dummies], axis=1)

## 8. euribor3m

# prepare a boxplot on euribor3m by outcome, and comment on the ‘euribor3m’ feature
# looks like an excellent feature
bank.boxplot(column=‘euribor3m’, by=‘outcome’)


Model building


## create X dataframe having ‘default’, ‘contact’, ‘previous’, ‘euribor3m’ and including 13 dummy #columns
feature_cols = [‘default’, ‘contact’, ‘previous’, ‘euribor3m’] + list(bank.columns[-13:])
X = bank[feature_cols]
# create y
y = bank.outcome



# 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)

## Import the Classifier
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
## Instantiate the model with 5 neighbors.
model = KNeighborsClassifier(n_neighbors=5)
## Fit the model on the training data., y_train)
# use the model to make predictions with the test data
y_pred = model.predict(X_test)
# how did our model perform?
count_misclassified = (y_test != y_pred).sum()
print(‘Misclassified samples: {}’.format(count_misclassified))
accuracy = metrics.accuracy_score(y_test, y_pred)
print(‘Accuracy: {:.2f}’.format(accuracy))


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.

from sklearn.metrics import accuracy_score
accuracy = []
for K in range(25):
K_value = K+1
neigh = KNeighborsClassifier(n_neighbors = K_value), y_train)
y_pred = neigh.predict(X_test)
print(“Accuracy is “, accuracy_score(y_test,y_pred)*100,“% for K-Value:”,K_value)

Plots of Accuracy v/s Neighbours

import matplotlib.pyplot as plt
plt.xlabel(“Number of neighbors”)
plt.title(“Accuracy vs # Neighbors”)