-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelling.py
131 lines (95 loc) · 4.94 KB
/
modelling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import numpy as np
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import cross_val_score
from sklearn.metrics import roc_auc_score
from scipy import stats
class modelling():
def __init__(self):
print ("Welcome to modelling part of the pacakage")
def open_svm(self, df_train, response_var, dict_paramters = {}, performCV = 1, cv_folds = 10, printKS = 1, printAUCcurve = 1, random_state = 29):
X = df_train.copy()
X_train = X.loc[:, X.columns != response_var]
col_names = X.columns
y_train = np.array(X.loc[:,response_var])
if 'C' in dict_paramters:
c = dict_paramters['C']
else:
c = 0.01
if 'kernel' in dict_paramters:
kernel = dict_paramters['kernel']
else:
kernel = 'rbf'
if 'degree' in dict_paramters:
degree = dict_paramters['degree']
else:
degree = 3
if 'random_State' in dict_paramters:
random_state = dict_paramters['random_state']
else:
random_state = 29
if 'max_iter' in dict_paramters:
max_iter = dict_paramters['max_iter']
else:
max_iter = 100
if 'probability' in dict_paramters:
probability = dict_paramters['probability']
else:
probability = False
clf_open_svm = SVC(C= c, kernel = kernel, degree = degree, probability = probability, max_iter = max_iter, random_state = random_state)
clf_open_svm.fit(X_train, y_train)
# prob > 0.5 => 1 else 0
hard_predictions_train = clf_open_svm.predict(X_train)
# considering only class = 1: either binary or one-vs-all
soft_predictions_train = clf_open_svm.predict_proba(X_train)[:,1]
if performCV:
cv_score = cross_val_score(clf_open_svm, X_train, y_train, cv = cv_folds, scoring = 'roc_auc')
print ("\n###########################################")
print ("\n#############TRAINING RESULTS##############")
print ("\n###########################################")
print ("\nModel Report")
print ("AUC Score (Train): %f" % roc_auc_score(y_train, soft_predictions_train))
if performCV:
print ("CV Score : Mean - %.7g | Std - %.7g | Min - %.7g | Max - %.7g" % (np.mean(cv_score),np.std(cv_score),np.min(cv_score),np.max(cv_score)))
if printKS:
print ("\n#### KS and p-val on Train set####")
metric_ks(soft = soft_predictions_train, target = y_train)
if printAUCcurve:
print ("\n#### ROC curve (Train set)####")
metric_auc(soft = soft_predictions_train, target = y_train)
return clf_open_svm, soft_predictions_train, hard_predictions_train
def open_naive_bayes(self, df_train, response_var, dict_paramters = {}, performCV = 1, cv_folds = 10, printKS = 1, printAUCcurve = 1):
X = df_train.copy()
X_train = X.loc[:, X.columns != response_var]
y_train = np.array(X.loc[:, response_var])
if 'priors' in dict_paramters:
priors = dict_paramters['priors']
else:
priors=None,
if 'var_smoothing' in dict_paramters:
var_smoothing = dict_paramters['var_smoothing']
else:
var_smoothing = 1e-09
clf_open_nb = GaussianNB(priors = priors, var_smoothing = var_smoothing)
clf_open_nb.fit(X_train, y_train)
# prob > 0.5 => 1 else 0
hard_predictions_train = clf_open_nb.predict(X_train)
# considering only class = 1: either binary or one-vs-all
soft_predictions_train = clf_open_nb.predict_proba(X_train)[:,1]
if performCV:
cv_score = cross_val_score(clf_open_nb, X_train, y_train, cv = cv_folds, scoring = 'roc_auc')
print ("\n###########################################")
print ("\n#############TRAINING RESULTS##############")
print ("\n###########################################")
print ("\nModel Report")
print ("AUC Score (Train): %f" % roc_auc_score(y_train, soft_predictions_train))
if performCV:
print ("CV Score : Mean - %.7g | Std - %.7g | Min - %.7g | Max - %.7g" % (np.mean(cv_score),np.std(cv_score),np.min(cv_score),np.max(cv_score)))
if printKS:
print ("\n#### KS and p-val on Train set####")
metric_ks(soft = soft_predictions_train, target = y_train)
if printAUCcurve:
print ("\n#### ROC curve (Train set)####")
metric_auc(soft = soft_predictions_train, target = y_train)
return clf_open_nb, soft_predictions_train, hard_predictions_train