import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report

#EVERY TIME THE DATASET IS RETRIEVED FROM GITHUB

input_file = 'https://raw.githubusercontent.com/lcphy/Digital-Innovation-Lab/master/bank-full.csv'
dataset = pd.read_csv(input_file, sep=';', header = 0)

dataset.head()

#DELETE NEXT CALLS DATA

dataset = dataset.drop("contact", axis=1)
dataset = dataset.drop("day", axis=1)
dataset = dataset.drop("month", axis=1)
dataset = dataset.drop("duration", axis=1)
dataset = dataset.drop("campaign", axis=1)
dataset = dataset.drop("pdays", axis=1)
dataset = dataset.drop("previous", axis=1)
dataset = dataset.drop("poutcome", axis=1)

dataset.head()

#FEATURE ENGINEERING

cleanup_nums = {"marital":     {"married": 1, "single": 0, "divorced":-1},
                "education": {"primary": 1, "secondary": 2, "tertiary": 3},
               "default":     {"yes": 1, "no": 0},
               "housing":     {"yes": 1, "no": 0},
               "loan":     {"yes": 1, "no": 0},
               "y":     {"yes": 1, "no": 0}}

dataset.replace(cleanup_nums, inplace=True)
dataset.head()

dataset.dtypes

dataset = dataset[dataset.job != 'unknown']
dataset = dataset[dataset.education != 'unknown']
dataset['education'] = dataset['education'].astype(int)

#COLLERATION MATRIX

plt.figure(figsize=(12,10))
cor = dataset.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()

#CLASSIFIFICATION

X = dataset.iloc[:, 0:7]
y = dataset.iloc[:, 7]

X = pd.get_dummies(X, columns=["job"], prefix=["job"])

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

#DECISION TREE

from sklearn import tree
from sklearn.tree import DecisionTreeClassifier

clf_dt = DecisionTreeClassifier()
clt_dt = clf_dt.fit(X_train,y_train)

esito = clf_dt.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, esito,target_names=target_names))

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, esito)
print(cm)

plt.hist(esito)

#RANDOM FOREST

from sklearn.ensemble import RandomForestClassifier

clf_dt = RandomForestClassifier()
clt_dt = clf_dt.fit(X_train,y_train)

esito = clf_dt.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, esito,target_names=target_names))

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, esito)
print(cm)

plt.hist(esito)

# K-NEAREST NEIGHBOURS

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# TRAINING - TEST
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# SCALING
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# FITTING
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
classifier.fit(X_train, y_train)

# PREDICTION
y_pred = classifier.predict(X_test)

# CONFUSION MATRIX
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, y_pred,target_names=target_names))

print(cm)

plt.hist(y_pred)

#UNDERSAMPLING

from sklearn.utils import resample

dataset_sample = pd.get_dummies(dataset, columns=["job"], prefix=["job"])

#SPLIT FEATURE AND TARGET
y = dataset_sample.y
X = dataset_sample.drop('y', axis=1)

#TRAIN TEST
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

X = pd.concat([X_train, y_train], axis=1)

#SELECTING TARGET CLASSES

not_sub = X[X.y==0]
sub = X[X.y==1]

not_sub_downsampled = resample(not_sub,
                                replace = False,
                                n_samples = len(sub),
                                random_state = 27)

# COMBINE MINORITY AND DOWNSAMPLED MAJORITY
downsampled = pd.concat([not_sub_downsampled, sub])

#DECISION TREE

y_train = downsampled.y
X_train = downsampled.drop('y', axis=1)

clf_dt = DecisionTreeClassifier()
clt_dt = clf_dt.fit(X_train,y_train)

esito = clf_dt.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, esito,target_names=target_names))

#RANDOM FOREST
y_train = downsampled.y
X_train = downsampled.drop('y', axis=1)

clf_dt = RandomForestClassifier()
clt_dt = clf_dt.fit(X_train,y_train)

esito = clf_dt.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, esito,target_names=target_names))

#SMOTE - DECISION TREE

from imblearn.over_sampling import SMOTE

#SPLIT FEATURE TARGET
y = dataset_sample.y
X = dataset_sample.drop('y', axis=1)

#TRAIN TEST
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

#SMOTE
sm = SMOTE(random_state=27, ratio=1.0)
X_train, y_train = sm.fit_sample(X_train, y_train)

clf_dt = DecisionTreeClassifier()

#FIT
smote = clf_dt.fit(X_train,y_train)

#PREDICITON
smote_pred = smote.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, smote_pred,target_names=target_names))

#SMOTE - RANDOM FOREST

from imblearn.over_sampling import SMOTE

y = dataset_sample.y
X = dataset_sample.drop('y', axis=1)

# setting up testing and training sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

sm = SMOTE(random_state=27, ratio=1.0)
X_train, y_train = sm.fit_sample(X_train, y_train)

clf_dt = RandomForestClassifier()

smote = clf_dt.fit(X_train,y_train)

smote_pred = smote.predict(X_test)

target_names = ['NOT-sub', 'Subscribed']
print(classification_report(y_test, smote_pred,target_names=target_names))

#RECAP on RECALL

x = np.arange(3)
plt.bar(x-0.2, [31,65,37], width=0.2, color='b', align='center', label='DT')
plt.bar(x, [18,61,32], width=0.2, color='r', align='center', label='RF')
plt.xticks(x-0.1, ['Normal','Under','Smote'])
plt.legend(loc='upper right')

#RECAP on F1

x = np.arange(3)
plt.bar(x-0.2, [31,26,32], width=0.2, color='b', align='center', label='DT')
plt.bar(x, [24,28,31], width=0.2, color='r', align='center', label='RF')
plt.xticks(x-0.1, ['Normal','Under','Smote'])
plt.legend(loc='lower right')
[((303, 345), 'pandas.read_csv', 'pd.read_csv', (['input_file'], {'sep': '""";"""', 'header': '(0)'}), "(input_file, sep=';', header=0)\n", (314, 345), True, 'import pandas as pd\n'), ((1356, 1384), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(12, 10)'}), '(figsize=(12, 10))\n', (1366, 1384), True, 'import matplotlib.pyplot as plt\n'), ((1405, 1451), 'seaborn.heatmap', 'sns.heatmap', (['cor'], {'annot': '(True)', 'cmap': 'plt.cm.Reds'}), '(cor, annot=True, cmap=plt.cm.Reds)\n', (1416, 1451), True, 'import seaborn as sns\n'), ((1452, 1462), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1460, 1462), True, 'import matplotlib.pyplot as plt\n'), ((1536, 1586), 'pandas.get_dummies', 'pd.get_dummies', (['X'], {'columns': "['job']", 'prefix': "['job']"}), "(X, columns=['job'], prefix=['job'])\n", (1550, 1586), True, 'import pandas as pd\n'), ((1676, 1714), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.25)'}), '(X, y, test_size=0.25)\n', (1692, 1714), False, 'from sklearn.model_selection import train_test_split\n'), ((1815, 1839), 'sklearn.tree.DecisionTreeClassifier', 'DecisionTreeClassifier', ([], {}), '()\n', (1837, 1839), False, 'from sklearn.tree import DecisionTreeClassifier\n'), ((2072, 2103), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', (['y_test', 'esito'], {}), '(y_test, esito)\n', (2088, 2103), False, 'from sklearn.metrics import confusion_matrix\n'), ((2115, 2130), 'matplotlib.pyplot.hist', 'plt.hist', (['esito'], {}), '(esito)\n', (2123, 2130), True, 'import matplotlib.pyplot as plt\n'), ((2210, 2234), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', ([], {}), '()\n', (2232, 2234), False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((2467, 2498), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', (['y_test', 'esito'], {}), '(y_test, esito)\n', (2483, 2498), False, 'from sklearn.metrics import confusion_matrix\n'), ((2510, 2525), 'matplotlib.pyplot.hist', 'plt.hist', (['esito'], {}), '(esito)\n', (2518, 2525), True, 'import matplotlib.pyplot as plt\n'), ((2729, 2783), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.25)', 'random_state': '(0)'}), '(X, y, test_size=0.25, random_state=0)\n', (2745, 2783), False, 'from sklearn.model_selection import train_test_split\n'), ((2853, 2869), 'sklearn.preprocessing.StandardScaler', 'StandardScaler', ([], {}), '()\n', (2867, 2869), False, 'from sklearn.preprocessing import StandardScaler\n'), ((3011, 3071), 'sklearn.neighbors.KNeighborsClassifier', 'KNeighborsClassifier', ([], {'n_neighbors': '(5)', 'metric': '"""minkowski"""', 'p': '(2)'}), "(n_neighbors=5, metric='minkowski', p=2)\n", (3031, 3071), False, 'from sklearn.neighbors import KNeighborsClassifier\n'), ((3231, 3263), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', (['y_test', 'y_pred'], {}), '(y_test, y_pred)\n', (3247, 3263), False, 'from sklearn.metrics import confusion_matrix\n'), ((3389, 3405), 'matplotlib.pyplot.hist', 'plt.hist', (['y_pred'], {}), '(y_pred)\n', (3397, 3405), True, 'import matplotlib.pyplot as plt\n'), ((3476, 3532), 'pandas.get_dummies', 'pd.get_dummies', (['dataset'], {'columns': "['job']", 'prefix': "['job']"}), "(dataset, columns=['job'], prefix=['job'])\n", (3490, 3532), True, 'import pandas as pd\n'), ((3666, 3720), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.25)', 'random_state': '(0)'}), '(X, y, test_size=0.25, random_state=0)\n', (3682, 3720), False, 'from sklearn.model_selection import train_test_split\n'), ((3730, 3767), 'pandas.concat', 'pd.concat', (['[X_train, y_train]'], {'axis': '(1)'}), '([X_train, y_train], axis=1)\n', (3739, 3767), True, 'import pandas as pd\n'), ((4086, 4123), 'pandas.concat', 'pd.concat', (['[not_sub_downsampled, sub]'], {}), '([not_sub_downsampled, sub])\n', (4095, 4123), True, 'import pandas as pd\n'), ((4215, 4239), 'sklearn.tree.DecisionTreeClassifier', 'DecisionTreeClassifier', ([], {}), '()\n', (4237, 4239), False, 'from sklearn.tree import DecisionTreeClassifier\n'), ((4511, 4535), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', ([], {}), '()\n', (4533, 4535), False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((4912, 4966), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.25)', 'random_state': '(0)'}), '(X, y, test_size=0.25, random_state=0)\n', (4928, 4966), False, 'from sklearn.model_selection import train_test_split\n'), ((4984, 5017), 'imblearn.over_sampling.SMOTE', 'SMOTE', ([], {'random_state': '(27)', 'ratio': '(1.0)'}), '(random_state=27, ratio=1.0)\n', (4989, 5017), False, 'from imblearn.over_sampling import SMOTE\n'), ((5079, 5103), 'sklearn.tree.DecisionTreeClassifier', 'DecisionTreeClassifier', ([], {}), '()\n', (5101, 5103), False, 'from sklearn.tree import DecisionTreeClassifier\n'), ((5511, 5565), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': '(0.25)', 'random_state': '(0)'}), '(X, y, test_size=0.25, random_state=0)\n', (5527, 5565), False, 'from sklearn.model_selection import train_test_split\n'), ((5576, 5609), 'imblearn.over_sampling.SMOTE', 'SMOTE', ([], {'random_state': '(27)', 'ratio': '(1.0)'}), '(random_state=27, ratio=1.0)\n', (5581, 5609), False, 'from imblearn.over_sampling import SMOTE\n'), ((5671, 5695), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', ([], {}), '()\n', (5693, 5695), False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((5909, 5921), 'numpy.arange', 'np.arange', (['(3)'], {}), '(3)\n', (5918, 5921), True, 'import numpy as np\n'), ((5922, 6007), 'matplotlib.pyplot.bar', 'plt.bar', (['(x - 0.2)', '[31, 65, 37]'], {'width': '(0.2)', 'color': '"""b"""', 'align': '"""center"""', 'label': '"""DT"""'}), "(x - 0.2, [31, 65, 37], width=0.2, color='b', align='center', label='DT'\n    )\n", (5929, 6007), True, 'import matplotlib.pyplot as plt\n'), ((5999, 6073), 'matplotlib.pyplot.bar', 'plt.bar', (['x', '[18, 61, 32]'], {'width': '(0.2)', 'color': '"""r"""', 'align': '"""center"""', 'label': '"""RF"""'}), "(x, [18, 61, 32], width=0.2, color='r', align='center', label='RF')\n", (6006, 6073), True, 'import matplotlib.pyplot as plt\n'), ((6072, 6121), 'matplotlib.pyplot.xticks', 'plt.xticks', (['(x - 0.1)', "['Normal', 'Under', 'Smote']"], {}), "(x - 0.1, ['Normal', 'Under', 'Smote'])\n", (6082, 6121), True, 'import matplotlib.pyplot as plt\n'), ((6118, 6147), 'matplotlib.pyplot.legend', 'plt.legend', ([], {'loc': '"""upper right"""'}), "(loc='upper right')\n", (6128, 6147), True, 'import matplotlib.pyplot as plt\n'), ((6167, 6179), 'numpy.arange', 'np.arange', (['(3)'], {}), '(3)\n', (6176, 6179), True, 'import numpy as np\n'), ((6180, 6265), 'matplotlib.pyplot.bar', 'plt.bar', (['(x - 0.2)', '[31, 26, 32]'], {'width': '(0.2)', 'color': '"""b"""', 'align': '"""center"""', 'label': '"""DT"""'}), "(x - 0.2, [31, 26, 32], width=0.2, color='b', align='center', label='DT'\n    )\n", (6187, 6265), True, 'import matplotlib.pyplot as plt\n'), ((6257, 6331), 'matplotlib.pyplot.bar', 'plt.bar', (['x', '[24, 28, 31]'], {'width': '(0.2)', 'color': '"""r"""', 'align': '"""center"""', 'label': '"""RF"""'}), "(x, [24, 28, 31], width=0.2, color='r', align='center', label='RF')\n", (6264, 6331), True, 'import matplotlib.pyplot as plt\n'), ((6330, 6379), 'matplotlib.pyplot.xticks', 'plt.xticks', (['(x - 0.1)', "['Normal', 'Under', 'Smote']"], {}), "(x - 0.1, ['Normal', 'Under', 'Smote'])\n", (6340, 6379), True, 'import matplotlib.pyplot as plt\n'), ((6376, 6405), 'matplotlib.pyplot.legend', 'plt.legend', ([], {'loc': '"""lower right"""'}), "(loc='lower right')\n", (6386, 6405), True, 'import matplotlib.pyplot as plt\n'), ((1957, 2020), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'esito'], {'target_names': 'target_names'}), '(y_test, esito, target_names=target_names)\n', (1978, 2020), False, 'from sklearn.metrics import classification_report\n'), ((2352, 2415), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'esito'], {'target_names': 'target_names'}), '(y_test, esito, target_names=target_names)\n', (2373, 2415), False, 'from sklearn.metrics import classification_report\n'), ((3312, 3376), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'y_pred'], {'target_names': 'target_names'}), '(y_test, y_pred, target_names=target_names)\n', (3333, 3376), False, 'from sklearn.metrics import classification_report\n'), ((4357, 4420), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'esito'], {'target_names': 'target_names'}), '(y_test, esito, target_names=target_names)\n', (4378, 4420), False, 'from sklearn.metrics import classification_report\n'), ((4653, 4716), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'esito'], {'target_names': 'target_names'}), '(y_test, esito, target_names=target_names)\n', (4674, 4716), False, 'from sklearn.metrics import classification_report\n'), ((5242, 5310), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'smote_pred'], {'target_names': 'target_names'}), '(y_test, smote_pred, target_names=target_names)\n', (5263, 5310), False, 'from sklearn.metrics import classification_report\n'), ((5817, 5885), 'sklearn.metrics.classification_report', 'classification_report', (['y_test', 'smote_pred'], {'target_names': 'target_names'}), '(y_test, smote_pred, target_names=target_names)\n', (5838, 5885), False, 'from sklearn.metrics import classification_report\n')]