-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcross_valid.py
225 lines (153 loc) · 5.65 KB
/
cross_valid.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
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
from torchnet import meter
import pickle as pkl
from custom_model import *
from loaders import *
import time
import torch
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
from torch import nn
from torchvision import transforms
from sklearn.decomposition import PCA
from torchvision import transforms
from sklearn.model_selection import KFold
tran = transforms.Compose([transforms.ToTensor()])
#sET NUMBER OF FOLDS FOR CROSS-VALIDATION
folds = 4
def preprocess(df):
df['bare_nuclei'].replace({'?': np.nan}, inplace = True)
df.dropna(inplace=True)
df["bare_nuclei"] = df["bare_nuclei"].astype(int)
df.drop(["id"], axis = 1, inplace=True)
df["class"] = df["class"].map({2:0, 4:1})
return df
load = loaders("data/data.csv", preprocess)
a = open("data/datasets", "rb")
datasets = pkl.load(a)
drop_cols = ["marg_adhesion", "single_epith_cell_size", "mitoses"]
for x in datasets:
x.drop(drop_cols, axis = 1, inplace = True)
datasets = [pca_dataframe(x,2).iloc[:,:] for x in datasets]
train_valid_data = pd.concat(datasets[0:2])
tran = transforms.Compose([transforms.ToTensor()])
comb_data = pd.concat(datasets)
D_in, H, D_out = datasets[0].shape[1] - 1, 30, 2
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.Tanh(),
torch.nn.Linear(H, D_out),
torch.nn.Softmax()
)
lr = 0.1
loss_fn = nn.CrossEntropyLoss()
wd = 0.1
optimizer = optim.Adam(model.parameters(), lr, weight_decay=wd)
def init_weights(m):
if type(m) == nn.Linear:
m.weight.data.normal_(0, 2/float(12))
m.bias.data.normal_(0, 2/float(12))
kf = KFold(folds)
cross_val_accu = []
cross_val_models = []
best_accuracy = 0
best_accuracy_model = None
print("LR:", lr, "WD:", wd)
for tr, te in kf.split(train_valid_data):
train = train_valid_data.iloc[tr,:] #define the training set
valid = train_valid_data.iloc[te,:] #define the set of test as well as validation
datasets = [train, valid, valid]
trainloader, testloader, validloader = get_dataloaders(datasets, tran, batch_size = 30)
a = custom_model(model, loss_fn)
a.model.apply(init_weights)
a.train(trainloader, testloader, validloader, optimizer, 30, plot = True)
cross_val_models.append(a.model.state_dict())
accuracy, ct, auc, cm = a.metrics_val(testloader)
if accuracy > best_accuracy:
best_accuracy_model = a.model.state_dict()
best_accuracy = accuracy
cross_val_accu.append(accuracy)
print ("Accuracy:", accuracy, ct)
print ("Average Accuracy:", sum(cross_val_accu)/len(cross_val_accu))
#cross_val_models = pkl.load(open("cross_models_best", "rb"))
am = meter.AUCMeter()
cm = meter.ConfusionMeter(2)
correct = 0
total = 0
Y_ = []
a = custom_model(model, loss_fn)
for data in testloader:
Y_ = []
x,y = data
# a.model.load_state_dict(cross_val_models)
# y_ = a.model(Variable(x))
for mod in cross_val_models:
a.model.load_state_dict(mod)
Y_.append(a.model(Variable(x)))
y_ = Y_[0]
_, predicted = torch.max(y_.data, 1)
cm.add(y_.data, y)
am.add(y_.data[:,1].clone(),y)
total += y.size(0)
correct += (predicted == y).sum()
print (correct, total)
combset = WBCDataset(comb_data, tran)
combloader = DataLoader(combset, shuffle= True, batch_size=30, num_workers=4)
am = meter.AUCMeter()
cm = meter.ConfusionMeter(2)
correct = 0
total = 0
Y_ = []
a = custom_model(model, loss_fn)
for data in combloader:
Y_ = []
x,y = data
for mod in cross_val_models:
a.model.load_state_dict(mod)
Y_.append(a.model(Variable(x)))
y_ = Y_[0]
_, predicted = torch.max(y_.data, 1)
cm.add(y_.data, y)
am.add(y_.data[:,1].clone(),y)
total += y.size(0)
correct += (predicted == y).sum()
print (correct, total)
print("Accuracy for the model is", round(correct/float(total)*100, 4), correct, "/", total)
print("Area under ROC curve for the given model is", round(am.value()[0],4))
print ("Confusion Matrix for the given model is\n", cm.value())
def decision_boundary_2d(models, df, f1, f2, label = "class", h = 0.2):
"""
Renders a 2-dimensional decision boundary generated by
the Neural Network for given data.
df: Dataframe containing the data with labels as well as
the class.
xx: Column name of the feature to be plotted on the x-axis
yy: Column name of the label to be plotted on the y-axis
label: name of the column containing the class
Returns: Plots the decision boundary with the points colored
with class
"""
color = {1: "red", 0: "blue"}
x = df[f1]
y = df[f2]
x_min, x_max = x.min() - 1, x.max() + 1
y_min, y_max = y.min() - 1, y.max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
grid = np.c_[xx.ravel(), yy.ravel()]
grid_tensor = Variable(torch.Tensor(grid))
a = custom_model(model, loss_fn)
Y_ = []
for mod in models:
a.model.load_state_dict(mod)
Y_.append(a.model(grid_tensor).data)
y_ = sum(Y_)/len(Y_)
results = torch.max(y_, 1)[1].numpy()
plt.contourf(xx, yy, results.reshape(xx.shape), cmap=plt.cm.coolwarm, alpha=0.8)
plt.scatter(df[f1], df[f2], c=df[label].apply(lambda x: color[x]))
decision_boundary_2d(cross_val_models, comb_data, "PCA0", "PCA1")