-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
258 lines (211 loc) · 8.04 KB
/
models.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.figure_factory as ff
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier
from sklearn.feature_selection import mutual_info_classif
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import auc, roc_curve
import plotly.express as px
from sklearn.metrics import confusion_matrix
import torch.nn as nn
import torch.nn.functional as F
import dgl
class Results:
def __init__(self):
pass
def accuracy(self, true, preds):
return accuracy_score(true, preds)
def calculate_metrics(self, y_test, y_prob, y_pred, model_name):
auc = roc_auc_score(y_test, y_prob, multi_class='ovr')
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
results = {
'Model': model_name,
'AUC': auc,
'Accuracy': accuracy,
'Precision': precision,
'Recall': recall,
'F1 Score': f1
}
return pd.Series(results)
def print_confusion_matrix(self, y_test, y_pred, labels):
cm = confusion_matrix(y_test, y_pred, labels=labels)
# Plot the confusion matrix using Plotly
fig = ff.create_annotated_heatmap(
z=cm,
x=labels,
y=labels,
colorscale='blues',
annotation_text=cm,
showscale=True
)
fig.update_layout(
title='Confusion Matrix',
xaxis=dict(title='Predicted Label'),
yaxis=dict(title='True Label'),
)
fig.show()
def plot_roc_curve(self, y_true, y_prob, model_name, title="ROC Curve"):
# Convert the true labels to one-hot encoding
n_classes = len(set(y_true))
if n_classes < 2:
raise ValueError("Number of classes should be at least 2 for ROC curve.")
# Calculate micro-average ROC curve and AUC
fpr, tpr, _ = roc_curve(y_true, y_prob[:, 1], pos_label=1)
roc_auc = auc(fpr, tpr)
# Plot the ROC curve
fig = go.Figure()
fig.add_trace(go.Scatter(x=fpr, y=tpr,
mode='lines',
name=f'{model_name} (AUC = {roc_auc:.2f})'))
fig.add_trace(go.Scatter(x=[0, 1], y=[0, 1],
mode='lines',
line=dict(color='navy', width=2, dash='dash'),
showlegend=False))
fig.update_layout(
title=title,
xaxis=dict(title='False Positive Rate'),
yaxis=dict(title='True Positive Rate'),
width=800,
height=600
)
fig.show()
def plot_roc_curves(self, y_true, y_probs, model_names, title="ROC-AUC Curves"):
fig = go.Figure()
for idx, model_name in enumerate(model_names):
fpr, tpr, _ = roc_curve(y_true, y_probs[idx])
roc_auc = auc(fpr, tpr)
fig.add_trace(go.Scatter(x=fpr, y=tpr,
mode='lines',
name=f'{model_name} (AUC = {roc_auc:.2f})'))
fig.add_trace(go.Scatter(x=[0, 1], y=[0, 1],
mode='lines',
line=dict(color='navy', width=2, dash='dash'),
showlegend=False))
fig.update_layout(
title=title,
xaxis=dict(title='False Positive Rate'),
yaxis=dict(title='True Positive Rate'),
width=800,
height=600
)
fig.show()
def plot_hist(self, true_labels, predicted_labels):
# Check if the lengths of true_labels and predicted_labels are the same
if len(true_labels) != len(predicted_labels):
raise ValueError("Lengths of true_labels and predicted_labels must be the same.")
# Identify correct predictions
correct_predictions = (true_labels == predicted_labels)
# Count the occurrences of each correct label
unique_labels, counts = np.unique(true_labels[correct_predictions], return_counts=True)
unique_labels = [str(num) for num in unique_labels]
# Create a Plotly bar chart
fig = px.bar(x=unique_labels, y=counts, labels={'x': 'Categorical Labels', 'y': 'Count'},
title='Histogram of Correct Categorical Labels')
# Show the plot
fig.show()
def plot_3D_scatter(self, x, y, z, x_label, y_label, z_label, color_lambda=None):
fig = go.Figure()
if color_lambda is None:
colors = z
else:
colors = color_lambda
fig.add_trace(
go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(
size=3,
color=colors,
opacity=0.8
)
)
)
# set layout and show plot
fig.update_layout(
height=800,
width=800,
scene=dict(
xaxis_title=x_label,
yaxis_title=y_label,
zaxis_title=z_label
)
)
fig.show()
class Data_Handler:
def __init__(self, df):
self.df = df
def train_test_split(self, target_col, cols2exclude = None, test_size=0.2):
if cols2exclude is None:
cols2exclude = target_col
else:
cols2exclude.append(target_col)
return train_test_split(self.df[self.df.columns.difference(cols2exclude)], self.df[target_col],
test_size=test_size, shuffle=False)
def qcut_data(self, target_col, num_cuts):
target_cuts = pd.qcut(self.df[target_col], num_cuts, labels=False)
return target_cuts
class LR:
def __init__(self):
self.model = LogisticRegression()
self.results = Results()
def train(self, X_train, y_train):
self.model.fit(X_train, y_train)
def predict(self, X_test):
preds = self.model.predict(X_test)
return preds
def predict_prob(self, X_test):
probs = self.model.predict_proba(X_test)
return probs
def grid_search(self, param_grid, X_train, y_train, cv=3, scoring='accuracy'):
grid_search = GridSearchCV(
estimator=self.model,
param_grid=param_grid,
cv=cv,
scoring=scoring
)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
print("Best Hyperparameters:", best_params)
self.model = LogisticRegression(**best_params)
self.model.fit(X_train, y_train)
class XGB:
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42):
self.model = xgb.XGBClassifier(
n_estimators=n_estimators,
learning_rate=learning_rate,
max_depth=max_depth,
random_state=random_state)
self.results = Results()
self.best_params = None
def train(self, X_train, y_train):
self.model.fit(X_train, y_train)
def predict(self, X_test):
preds = self.model.predict(X_test)
return preds
def predict_prob(self, X_test):
probs = self.model.predict_proba(X_test)
return probs
def grid_search(self, param_grid, X_train, y_train, cv=3, scoring='accuracy'):
grid_search = GridSearchCV(
estimator=self.model,
param_grid=param_grid,
cv=cv,
scoring=scoring
)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
print("Best Hyperparameters:", best_params)
self.model = xgb.XGBClassifier(**best_params)
self.model.fit(X_train, y_train)