-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient_descent.py
31 lines (23 loc) · 1.33 KB
/
gradient_descent.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
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
from sklearn.preprocessing import StandardScaler
def sgd(train_features, train_labels, test_features, test_labels):
scale = StandardScaler()
scale.fit(train_features) # fitting of training data to be scaled
train_features = scale.transform(train_features)
test_features = scale.transform(test_features)
model = SGDClassifier(alpha=0.001, eta0=0.001, l1_ratio=0.9,
learning_rate='optimal', loss='modified_huber',
penalty='l1')
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("Stochastic Gradient Descent Accuracy: ", round(acc_score, 2), "%.")
print("Stochastic Gradient Descent Sensitivity: ", round(sens, 2), "%.")
print("Stochastic Gradient Descent Spec: ", round(spec, 2), "%.")
return model, round(auroc, 4), round(acc_score, 2), round(sens, 2), round(spec, 2)