forked from vkola-lab/brain2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vol_classifier.py
48 lines (39 loc) · 1.38 KB
/
vol_classifier.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
import csv
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from utils import write_raw_score_sk
def read_vol_complete(filename, stage):
with open(filename, 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
filenames, labels, vols = [], [], []
if stage == 'train':
your_list = your_list[:338]
else:
your_list = your_list[338:]
for line in your_list:
try:
vol = list(map(float, line[2:]))
except:
continue
filenames.append(line[0])
label = int(line[1])
labels.append(label)
vols.append(vol)
return filenames, labels, vols
filenames, y_train, X_train = read_vol_complete('./lookupcsv/ADNI_MRI_VOL.csv', 'train')
filenames, y_test, X_test = read_vol_complete('./lookupcsv/ADNI_MRI_VOL.csv', 'test')
print(y_test)
print(y_train)
accu_list = []
for i in range(10):
clf = RandomForestClassifier(max_depth=20)
clf.fit(X_train, y_train)
y_test_pred = clf.predict(X_test)
accu_list.append(accuracy_score(y_test, y_test_pred))
f = open('./checkpoint_dir/Vol_RF/raw_score_{}.txt'.format(i), 'w')
write_raw_score_sk(f, clf.predict_proba(X_test), y_test)
f.close()
print(float(np.mean(accu_list)), float(np.std(accu_list)))