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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
- """
- Testing for SkopeRules algorithm
- """
- import numpy as np
- import pytest
- from numpy.testing import assert_array_equal, assert_no_warnings, assert_raises, suppress_warnings, assert_warns
- from sklearn.datasets import load_iris, make_blobs
- from sklearn.metrics import accuracy_score
- from sklearn.model_selection import ParameterGrid
- from sklearn.utils import check_random_state
- from imodels.rule_set.skope_rules import SkopeRulesClassifier
- rng = check_random_state(0)
- # load the iris dataset
- # and randomly permute it
- iris = load_iris()
- perm = rng.permutation(iris.target.size)
- iris.data = iris.data[perm]
- iris.target = iris.target[perm]
- @pytest.mark.filterwarnings("ignore::UserWarning")
- def test_skope_rules():
- """Check various parameter settings."""
- X_train = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
- [6, 3], [-4, -7]]
- y_train = [0] * 6 + [1] * 2
- X_test = np.array([[2, 1], [1, 1]])
- grid = ParameterGrid({
- "precision_min": [0.],
- "recall_min": [0.],
- "n_estimators": [1],
- "max_samples": [0.5, 4],
- "max_samples_features": [0.5, 2],
- "bootstrap": [True, False],
- "bootstrap_features": [True, False],
- "max_depth": [2],
- "max_features": [None, 1, 0.1],
- "min_samples_split": [2, 0.1],
- "n_jobs": [-1, 2]})
- with suppress_warnings():
- for params in grid:
- SkopeRulesClassifier(random_state=rng,
- **params).fit(X_train, y_train, feature_names=['a', 'b']).predict(X_test)
- # additional parameters:
- SkopeRulesClassifier(n_estimators=50,
- max_samples=1.,
- recall_min=0.,
- precision_min=0.).fit(X_train, y_train).predict(X_test)
- def test_skope_rules_error():
- """Test that it gives proper exception on deficient input."""
- X = iris.data
- y = iris.target
- y = (y != 0)
- # Test max_samples
- assert_raises(ValueError,
- SkopeRulesClassifier(max_samples=-1).fit, X, y)
- assert_raises(ValueError,
- SkopeRulesClassifier(max_samples=0.0).fit, X, y)
- assert_raises(ValueError,
- SkopeRulesClassifier(max_samples=2.0).fit, X, y)
- # explicitly setting max_samples > n_samples should result in a warning.
- assert_warns(UserWarning,
- SkopeRulesClassifier(max_samples=1000).fit, X, y)
- # assert_no_warnings(SkopeRulesClassifier(max_samples=np.int64(2)).fit, X, y)
- assert_raises(ValueError, SkopeRulesClassifier(max_samples='foobar').fit, X, y)
- assert_raises(ValueError, SkopeRulesClassifier(max_samples=1.5).fit, X, y)
- assert_raises(ValueError, SkopeRulesClassifier(max_depth_duplication=1.5).fit, X, y)
- assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).predict, X[:, 1:])
- # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).eval_weighted_rule_sum,
- # X[:, 1:])
- # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).rules_vote, X[:, 1:])
- # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).score_top_rules,
- # X[:, 1:])
- @pytest.mark.filterwarnings("ignore::UserWarning")
- def test_max_samples_attribute():
- X = iris.data
- y = iris.target
- y = (y != 0)
- clf = SkopeRulesClassifier(max_samples=1.).fit(X, y)
- assert clf.max_samples_ == X.shape[0]
- clf = SkopeRulesClassifier(max_samples=500)
- assert_warns(UserWarning,
- clf.fit, X, y)
- assert clf.max_samples_ == X.shape[0]
- clf = SkopeRulesClassifier(max_samples=0.4).fit(X, y)
- assert clf.max_samples_ == 0.4 * X.shape[0]
- @pytest.mark.filterwarnings("ignore::UserWarning")
- def test_skope_rules_works():
- # toy sample (the last two samples are outliers)
- X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1], [6, 3], [4, -7]]
- y = [0] * 6 + [1] * 2
- X_test = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
- [10, 5], [5, -7]]
- # Test LOF
- clf = SkopeRulesClassifier(random_state=rng, max_samples=1., max_samples_features=1.)
- clf.fit(X, y)
- decision_func = clf._eval_weighted_rule_sum(X_test)
- rules_vote = clf._rules_vote(X_test)
- score_top_rules = clf._score_top_rules(X_test)
- pred = clf.predict(X_test)
- pred_score_top_rules = clf._predict_top_rules(X_test, 1)
- # assert detect outliers:
- assert np.min(decision_func[-2:]) > np.max(decision_func[:-2])
- assert np.min(rules_vote[-2:]) > np.max(rules_vote[:-2])
- assert np.min(score_top_rules[-2:]) > np.max(score_top_rules[:-2])
- assert_array_equal(pred, 6 * [0] + 2 * [1])
- assert_array_equal(pred_score_top_rules, 6 * [0] + 2 * [1])
- @pytest.mark.filterwarnings("ignore::UserWarning")
- def test_deduplication_works():
- # toy sample (the last two samples are outliers)
- X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1], [6, 3], [4, -7]]
- y = [0] * 6 + [1] * 2
- X_test = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
- [10, 5], [5, -7]]
- # Test LOF
- clf = SkopeRulesClassifier(random_state=rng, max_samples=1., max_depth_duplication=3)
- clf.fit(X, y)
- decision_func = clf._eval_weighted_rule_sum(X_test)
- rules_vote = clf._rules_vote(X_test)
- score_top_rules = clf._score_top_rules(X_test)
- pred = clf.predict(X_test)
- pred_score_top_rules = clf._predict_top_rules(X_test, 1)
- assert True, 'deduplication works'
- def test_performances():
- X, y = make_blobs(n_samples=1000, random_state=0, centers=2)
- # make labels imbalanced by remove all but 100 instances from class 1
- indexes = np.ones(X.shape[0]).astype(bool)
- ind = np.array([False] * 100 + list(((y == 1)[100:])))
- indexes[ind] = 0
- X = X[indexes]
- y = y[indexes]
- n_samples, n_features = X.shape
- clf = SkopeRulesClassifier()
- # fit
- clf.fit(X, y)
- # with lists
- clf.fit(X.tolist(), y.tolist())
- y_pred = clf.predict(X)
- assert y_pred.shape == (n_samples,)
- # training set performance
- assert accuracy_score(y, y_pred) > 0.83
|