40 KiB
40 KiB
In [3]:
## prepare env, read and prepare data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
codepath = '.././2_code' ## for import of user defined module
datapath = '../../3_data'
#codepath = '../2_code' ## for import of user defined module
#datapath = '../3_data'
from sys import path; path.insert(1, codepath)
from os import chdir; chdir(datapath)
from bfh_cas_pml import prep_data
X_train, X_test, y_train, y_test = prep_data('bank_data_prep.csv', target='y', seed=1234)In [4]:
## train a model
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(random_state=1234)
model.fit(X_train, y_train)
## prediction using .predict_proba()
y_pred_p_no = model.predict_proba(X_test)[:, 0]In [5]:
## inspect different threshold values
from sklearn.metrics import accuracy_score
thresholds = np.arange(0, 1.01, 0.01)
scores = []
for threshold in thresholds:
y_pred = np.where(y_pred_p_no > threshold, 'no', 'yes')
scores.append(accuracy_score(y_test, y_pred))In [6]:
## results
print('max accuracy %6.4f for threshold %4.2f' % (
max(scores),
thresholds[scores.index(max(scores))]))max accuracy 0.8792 for threshold 0.56
In [7]:
## viszalization
sns.lineplot(x=thresholds, y=scores)
plt.scatter(x=thresholds[scores.index(max(scores))], y=max(scores), color="black")
plt.xlabel('threshold')
plt.ylabel('accuracy');C:\Users\werne\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\werne\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):