-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboosting.py
83 lines (58 loc) · 3.25 KB
/
boosting.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
import lightgbm as lgb
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
def adaboost(train_features, train_labels, test_features, test_labels):
model = AdaBoostClassifier(n_estimators=500, learning_rate=0.1, algorithm='SAMME.R')
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("AdaBoost Accuracy: ", round(acc_score, 2), "%.")
print("AdaBoost Sensitivity: ", round(sens, 2), "%.")
print("AdaBoost Spec: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)
def xgb(train_features, train_labels, test_features, test_labels):
model = XGBClassifier(subsample=1, max_depth=3, learning_rate=0.1, gamma=0.1, colsample_bytree=1)
model.fit(train_features, train_labels)
predictions = model.predict(test_features)
predictions = (predictions > 0.5)
# 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("XGBClassifier AUROC: ", round(auroc, 2), "%.")
print("XGBClassifier Accuracy: ", round(acc_score, 2), "%.")
print("XGBClassifier Sensitivity: ", round(sens, 2), "%.")
print("XGBClassifier Specificity: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)
def lightgbm(train_features, train_labels, test_features, test_labels):
sc = StandardScaler()
sc.fit(train_features) # fitting of training data to be scaled
train_features = sc.transform(train_features)
test_features = sc.transform(test_features)
d_train = lgb.Dataset(train_features, label=train_labels)
params = {'learning_rate': 0.003, 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'binary_logloss',
'sub_feature': 0.5, 'num_leaves': 10, 'min_data': 50, 'max_depth': 10}
model = lgb.train(params, d_train, 100)
# Prediction
predictions = model.predict(test_features)
predictions = (predictions > 0.5)
# 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("lgb AUROC: ", round(auroc, 2), "%.")
print("lgb Accuracy: ", round(acc_score, 2), "%.")
print("lgb Sensitivity: ", round(sens, 2), "%.")
print("lgb Specificity: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)