-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathplot_robust_classification_diabete.py
More file actions
85 lines (68 loc) · 2.69 KB
/
Copy pathplot_robust_classification_diabete.py
File metadata and controls
85 lines (68 loc) · 2.69 KB
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
# -*- coding: utf-8 -*-
"""
======================================================================
A demo of Robust Classification on real dataset "diabetes" from OpenML
======================================================================
In this example we compare the RobustWeightedCLassifier
for classification on the real dataset "diabetes".
We only compare the estimator with SGDClassifier as there is no robust
classification estimator in scikit-learn.
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn_extra.robust import RobustWeightedClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import fetch_openml
from sklearn.metrics import roc_auc_score, make_scorer
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import RobustScaler
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
X, y = fetch_openml(name="diabetes", as_frame=False, return_X_y=True)
# replace the label names with 0 or 1
y = (y == "tested_positive").astype(int)
# Scale the dataset with sklearn RobustScaler (important for this algorithm)
X = RobustScaler().fit_transform(X)
# Using GridSearchCV, to tune the parameters alpha, eta0, learning_rate, loss
# and average of SGDClassifier, we get the following parameters.
clf_not_rob = SGDClassifier(average=10, learning_rate="optimal", loss="hinge")
# Then, we use this estimator as base_estimator of RobustWeightedEstimator.
# Using GridSearchCV, we tuned the parameters c and eta0, with the
# choice of "huber" weighting because the sample_size is not very large.
clf_rob = RobustWeightedClassifier(
weighting="huber",
loss="hinge",
c=1.35,
max_iter=300,
)
# We compute M times the cross validations in order to also have an estimate
# of the variance of the loss of the estimators.
M = 10
res = []
for f in range(M):
rng = np.random.RandomState(f)
print("\r Progress: %s / %s" % (f + 1, M), end="")
clf = SGDClassifier(
average=10, learning_rate="optimal", loss="hinge", random_state=rng
)
clf_rob = RobustWeightedClassifier(
weighting="huber",
loss="hinge",
c=1.35,
max_iter=300,
random_state=rng,
)
cv_not_rob = cross_val_score(
clf_not_rob, X, y, cv=10, scoring=make_scorer(roc_auc_score)
)
cv_rob = cross_val_score(
clf_rob, X, y, cv=10, scoring=make_scorer(roc_auc_score)
)
res += [[np.mean(cv_rob), np.mean(cv_not_rob)]]
plt.boxplot(
np.array(res), labels=["RobustWeightedClassifier", "SGDClassifier"]
)
plt.ylabel("AUC")
# Remark : when using accuracy score, the optimal hyperparameters change and
# for example the parameter c changes from 1.35 to 10.
plt.show()