-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomForest.py
109 lines (81 loc) · 2.92 KB
/
RandomForest.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
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_curve, precision_recall_curve, accuracy_score, f1_score, auc
import matplotlib.pyplot as plt
import numpy as np
import os
os.chdir('/Users/alexanderjambor/Desktop/UCSD/SP23/BENG203/GroupProject/BRCAClassifier')
# 629 vs 690 features
# X = np.loadtxt('./data/processed/normal_vs_cancer/X_filtered.csv', delimiter=',')
# y = np.loadtxt('./data/processed/normal_vs_cancer/y_filtered.csv', delimiter=',')
X = np.loadtxt('./data/processed/recurrent_vs_nonrecurrent/X_filtered.csv', delimiter=',')
y = np.loadtxt('./data/processed/recurrent_vs_nonrecurrent/y_filtered.csv', delimiter=',')
X = np.log2(X+1)
auroc_vals = []
auprc_vals = []
accuracy_scores = []
f1_scores = []
fpr_vecs = []
tpr_vecs = []
precision_vecs = []
recall_vecs = []
oob_errors = []
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=2)
model = RandomForestClassifier(oob_score=True)
for train_idx, test_idx in cv.split(X, y):
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
smote = SMOTE(random_state=42)
X_train, y_train = smote.fit_resample(X_train, y_train)
model.fit(X_train, y_train)
y_proba = model.predict_proba(X_test)[:, 1]
y_pred = model.predict(X_test)
oob_error = 1 - model.oob_score_
oob_errors.append(oob_error)
fpr, tpr, _ = roc_curve(y_test, y_proba)
fpr_vecs.append(fpr)
tpr_vecs.append(tpr)
auroc = auc(fpr, tpr)
auroc_vals += [auroc]
precision, recall, _ = precision_recall_curve(y_test, y_proba)
precision_vecs.append(precision)
recall_vecs.append(recall)
auprc = auc(recall, precision)
auprc_vals += [auprc]
accuracy = accuracy_score(y_test, y_pred)
print(y_test)
print(y_pred)
accuracy_scores.append(accuracy)
f1 = f1_score(y_test, y_pred)
f1_scores.append(f1)
avg_accuracy = sum(accuracy_scores) / len(accuracy_scores)
avg_f1 = sum(f1_scores) / len(f1_scores)
avg_auroc = sum(auroc_vals) / len(auroc_vals)
avg_auprc = sum(auprc_vals) / len(auprc_vals)
print("<Accuracy>:", avg_accuracy)
print("<F1 Score>:", avg_f1)
print("<AUROC>:", avg_auroc)
print("<AUPRC>:", avg_auprc)
plt.subplot(1, 2, 1)
for idx, _ in enumerate(recall_vecs):
plt.plot(recall_vecs[idx], precision_vecs[idx])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.subplot(1, 2, 2)
for idx, _ in enumerate(fpr_vecs):
plt.plot(fpr_vecs[idx], tpr_vecs[idx])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend()
plt.show()