-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_models.py
47 lines (33 loc) · 1.96 KB
/
linear_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
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LinearRegression
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
def lda(train_features, train_labels, test_features, test_labels):
model = LinearDiscriminantAnalysis()
model.fit(train_features, train_labels)
predictions = model.predict(test_features)
# Performance Metrics
acc_score = accuracy_score(test_labels, predictions, normalize=True) * 100
auroc = roc_auc_score(test_labels, predictions)
tn, fp, fn, tp = confusion_matrix(test_labels, predictions).ravel()
sens = tp / (tp + fn) * 100
spec = tn / (tn + fp) * 100
print("Linear Discriminant Analysis AUROC: ", round(auroc, 2), "%.")
print("Linear Discriminant Analysis Accuracy: ", round(acc_score, 2), "%.")
print("Linear Discriminant Analysis Sensitivity: ", round(sens, 2), "%.")
print("Linear Discriminant Analysis Spec: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)
def linear_regression(train_features, train_labels, test_features, test_labels):
model = LinearRegression()
model.fit(train_features, train_labels)
predictions = model.predict(test_features)
# Performance Metrics
acc_score = accuracy_score(test_labels, predictions.round(), normalize=True) * 100
auroc = roc_auc_score(test_labels, predictions)
tn, fp, fn, tp = confusion_matrix(test_labels, predictions.round(), labels=[0, 1]).ravel()
sens = tp / (tp + fn) * 100
spec = tn / (tn + fp) * 100
print("Linear Regression AUROC: ", round(auroc, 2), "%.")
print("Linear Regression Accuracy: ", round(acc_score, 2), "%.")
print("Linear Regression Sensitivity: ", round(sens, 2), "%.")
print("Linear Regression Spec: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)