-
Notifications
You must be signed in to change notification settings - Fork 82
/
test_en_classifier.py
125 lines (92 loc) · 3.96 KB
/
test_en_classifier.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import numpy as np
import pytest
from sklearn.linear_model import SGDClassifier as SKElasticNetClassifier
from evalml.model_family import ModelFamily
from evalml.pipelines.components.estimators.classifiers import (
ElasticNetClassifier
)
from evalml.problem_types import ProblemTypes
def test_model_family():
assert ElasticNetClassifier.model_family == ModelFamily.LINEAR_MODEL
def test_en_parameters():
clf = ElasticNetClassifier(alpha=0.75, l1_ratio=0.5, random_state=2)
expected_parameters = {
"alpha": 0.75,
"l1_ratio": 0.5,
'max_iter': 1000,
'n_jobs': -1,
'penalty': 'elasticnet',
'loss': 'log'
}
assert clf.parameters == expected_parameters
def test_problem_types():
assert ProblemTypes.BINARY in ElasticNetClassifier.supported_problem_types
assert ProblemTypes.MULTICLASS in ElasticNetClassifier.supported_problem_types
assert len(ElasticNetClassifier.supported_problem_types) == 2
def test_fit_predict_binary(X_y_binary):
X, y = X_y_binary
sk_clf = SKElasticNetClassifier(loss="log",
penalty="elasticnet",
alpha=0.5,
l1_ratio=0.5,
n_jobs=-1,
random_state=0)
sk_clf.fit(X, y)
y_pred_sk = sk_clf.predict(X)
y_pred_proba_sk = sk_clf.predict_proba(X)
clf = ElasticNetClassifier()
clf.fit(X, y)
y_pred = clf.predict(X)
y_pred_proba = clf.predict_proba(X)
np.testing.assert_almost_equal(y_pred, y_pred_sk, decimal=5)
np.testing.assert_almost_equal(y_pred_proba, y_pred_proba_sk, decimal=5)
def test_fit_predict_multi(X_y_multi):
X, y = X_y_multi
sk_clf = SKElasticNetClassifier(loss="log",
penalty="elasticnet",
alpha=0.5,
l1_ratio=0.5,
n_jobs=-1,
random_state=0)
sk_clf.fit(X, y)
y_pred_sk = sk_clf.predict(X)
y_pred_proba_sk = sk_clf.predict_proba(X)
clf = ElasticNetClassifier()
clf.fit(X, y)
y_pred = clf.predict(X)
y_pred_proba = clf.predict_proba(X)
np.testing.assert_almost_equal(y_pred, y_pred_sk, decimal=5)
np.testing.assert_almost_equal(y_pred_proba, y_pred_proba_sk, decimal=5)
def test_feature_importance(X_y_binary):
X, y = X_y_binary
sk_clf = SKElasticNetClassifier(loss="log",
penalty="elasticnet",
alpha=0.5,
l1_ratio=0.5,
n_jobs=-1,
random_state=0)
sk_clf.fit(X, y)
clf = ElasticNetClassifier()
clf.fit(X, y)
np.testing.assert_almost_equal(sk_clf.coef_.flatten(), clf.feature_importance, decimal=5)
def test_feature_importance_multi(X_y_multi):
X, y = X_y_multi
sk_clf = SKElasticNetClassifier(loss="log",
penalty="elasticnet",
alpha=0.5,
l1_ratio=0.5,
n_jobs=-1,
random_state=0)
sk_clf.fit(X, y)
clf = ElasticNetClassifier()
clf.fit(X, y)
sk_features = np.linalg.norm(sk_clf.coef_, axis=0, ord=2)
np.testing.assert_almost_equal(sk_features, clf.feature_importance, decimal=5)
def test_overwrite_loss_parameter_in_kwargs():
with pytest.warns(expected_warning=UserWarning) as warnings:
en = ElasticNetClassifier(loss="hinge")
assert len(warnings) == 1
# check that the message matches
assert warnings[0].message.args[0] == ("Parameter loss is being set to 'log' so that ElasticNetClassifier can predict probabilities"
". Originally received 'hinge'.")
assert en.parameters['loss'] == 'log'