Get your data here. The data is related with 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 not ('no') subscribed. There are four datasets:
1) bank-additional-full.csv with all examples (41188) and 20 inputs, ordered by date (from May 2008 to November 2010)
2) bank-additional.csv with 10% of the examples (4119), randomly selected from 1), and 20 inputs.
3) bank-full.csv with all examples and 17 inputs, ordered by date (older version of this dataset with less inputs).
4) bank.csv with 10% of the examples and 17 inputs, randomly selected from 3 (older version of this dataset with less inputs).
The smallest datasets are provided to test more computationally demanding machine learning algorithms (e.g., SVM).
The classification goal is to predict if the client will subscribe (yes/no) a term deposit (variable y).
LabelEncoder useful)
In [53]:
import pandas as pd
import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix, classification_report
from sklearn import learning_curve
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
In [21]:
df = pd.read_csv('bank-full.csv', sep =";")
In [22]:
df.head()
Out[22]:
In [23]:
df.shape
Out[23]:
In [24]:
df.describe()
Out[24]:
In [25]:
for i in df:
for x in df[i]:
if x == '?':
print i
else:
pass
In [26]:
df.info()
In [27]:
df.columns
Out[27]:
In [28]:
bank_data = pd.DataFrame()
label_encoders = {}
for column in df.columns:
if df[column].dtype == 'object':
label_encoders[column] = preprocessing.LabelEncoder()
bank_data[column] = label_encoders[column].fit_transform(df[column])
else:
bank_data[column] = df[column]
In [29]:
bank_data.shape
Out[29]:
In [37]:
#drop duration column
bank_data.drop([bank_data.columns[11]], axis=1, inplace=True)
bank_data.head()
Out[37]:
In [38]:
bank_data.info()
In [42]:
X=bank_data.ix[:,0:15]
X.head()
Out[42]:
In [44]:
y=bank_data['y']
y.head()
Out[44]:
In [70]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
In [71]:
knn_model = KNeighborsClassifier()
In [72]:
knn_model.fit(X_train,y_train)
Out[72]:
In [73]:
# Use the model to predict Test Data
y_pred = knn_model.predict(X_test)
In [74]:
print classification_report(y_test, y_pred)
In [75]:
rf_model = RandomForestClassifier(n_estimators = 100, max_features="sqrt")
In [76]:
rf_model.fit(X_train, y_train, sample_weight=None)
Out[76]:
In [77]:
# Use the model to predict Test Data
y_pred = rf_model.predict(X_test)
In [78]:
print classification_report(y_test, y_pred)
In [79]:
# This prints the most important features
rf_model.fit(X_train,y_train)
sorted(zip(rf_model.feature_importances_, X.columns), reverse=True)[:20]
# 5 most important features are 'age', 'euribor3m', 'campaign', 'job', 'education'
Out[79]:
In [64]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from sklearn.learning_curve import learning_curve
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score")
plt.legend(loc="best")
return plt
In [65]:
%%time
_ = plot_learning_curve(RandomForestClassifier(n_estimators=100),'test',X_train,y_train)
In [84]:
%%time
_ = plot_learning_curve(KNeighborsClassifier(),'test',X_train,y_train)
In [ ]: