Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

skope_rules_test.py 5.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Testing for SkopeRules algorithm
  3. """
  4. import numpy as np
  5. import pytest
  6. from numpy.testing import assert_array_equal, assert_no_warnings, assert_raises, suppress_warnings, assert_warns
  7. from sklearn.datasets import load_iris, make_blobs
  8. from sklearn.metrics import accuracy_score
  9. from sklearn.model_selection import ParameterGrid
  10. from sklearn.utils import check_random_state
  11. from imodels.rule_set.skope_rules import SkopeRulesClassifier
  12. rng = check_random_state(0)
  13. # load the iris dataset
  14. # and randomly permute it
  15. iris = load_iris()
  16. perm = rng.permutation(iris.target.size)
  17. iris.data = iris.data[perm]
  18. iris.target = iris.target[perm]
  19. @pytest.mark.filterwarnings("ignore::UserWarning")
  20. def test_skope_rules():
  21. """Check various parameter settings."""
  22. X_train = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
  23. [6, 3], [-4, -7]]
  24. y_train = [0] * 6 + [1] * 2
  25. X_test = np.array([[2, 1], [1, 1]])
  26. grid = ParameterGrid({
  27. "precision_min": [0.],
  28. "recall_min": [0.],
  29. "n_estimators": [1],
  30. "max_samples": [0.5, 4],
  31. "max_samples_features": [0.5, 2],
  32. "bootstrap": [True, False],
  33. "bootstrap_features": [True, False],
  34. "max_depth": [2],
  35. "max_features": [None, 1, 0.1],
  36. "min_samples_split": [2, 0.1],
  37. "n_jobs": [-1, 2]})
  38. with suppress_warnings():
  39. for params in grid:
  40. SkopeRulesClassifier(random_state=rng,
  41. **params).fit(X_train, y_train, feature_names=['a', 'b']).predict(X_test)
  42. # additional parameters:
  43. SkopeRulesClassifier(n_estimators=50,
  44. max_samples=1.,
  45. recall_min=0.,
  46. precision_min=0.).fit(X_train, y_train).predict(X_test)
  47. def test_skope_rules_error():
  48. """Test that it gives proper exception on deficient input."""
  49. X = iris.data
  50. y = iris.target
  51. y = (y != 0)
  52. # Test max_samples
  53. assert_raises(ValueError,
  54. SkopeRulesClassifier(max_samples=-1).fit, X, y)
  55. assert_raises(ValueError,
  56. SkopeRulesClassifier(max_samples=0.0).fit, X, y)
  57. assert_raises(ValueError,
  58. SkopeRulesClassifier(max_samples=2.0).fit, X, y)
  59. # explicitly setting max_samples > n_samples should result in a warning.
  60. assert_warns(UserWarning,
  61. SkopeRulesClassifier(max_samples=1000).fit, X, y)
  62. # assert_no_warnings(SkopeRulesClassifier(max_samples=np.int64(2)).fit, X, y)
  63. assert_raises(ValueError, SkopeRulesClassifier(max_samples='foobar').fit, X, y)
  64. assert_raises(ValueError, SkopeRulesClassifier(max_samples=1.5).fit, X, y)
  65. assert_raises(ValueError, SkopeRulesClassifier(max_depth_duplication=1.5).fit, X, y)
  66. assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).predict, X[:, 1:])
  67. # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).eval_weighted_rule_sum,
  68. # X[:, 1:])
  69. # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).rules_vote, X[:, 1:])
  70. # assert_raises(ValueError, SkopeRulesClassifier().fit(X, y).score_top_rules,
  71. # X[:, 1:])
  72. @pytest.mark.filterwarnings("ignore::UserWarning")
  73. def test_max_samples_attribute():
  74. X = iris.data
  75. y = iris.target
  76. y = (y != 0)
  77. clf = SkopeRulesClassifier(max_samples=1.).fit(X, y)
  78. assert clf.max_samples_ == X.shape[0]
  79. clf = SkopeRulesClassifier(max_samples=500)
  80. assert_warns(UserWarning,
  81. clf.fit, X, y)
  82. assert clf.max_samples_ == X.shape[0]
  83. clf = SkopeRulesClassifier(max_samples=0.4).fit(X, y)
  84. assert clf.max_samples_ == 0.4 * X.shape[0]
  85. @pytest.mark.filterwarnings("ignore::UserWarning")
  86. def test_skope_rules_works():
  87. # toy sample (the last two samples are outliers)
  88. X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1], [6, 3], [4, -7]]
  89. y = [0] * 6 + [1] * 2
  90. X_test = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
  91. [10, 5], [5, -7]]
  92. # Test LOF
  93. clf = SkopeRulesClassifier(random_state=rng, max_samples=1., max_samples_features=1.)
  94. clf.fit(X, y)
  95. decision_func = clf._eval_weighted_rule_sum(X_test)
  96. rules_vote = clf._rules_vote(X_test)
  97. score_top_rules = clf._score_top_rules(X_test)
  98. pred = clf.predict(X_test)
  99. pred_score_top_rules = clf._predict_top_rules(X_test, 1)
  100. # assert detect outliers:
  101. assert np.min(decision_func[-2:]) > np.max(decision_func[:-2])
  102. assert np.min(rules_vote[-2:]) > np.max(rules_vote[:-2])
  103. assert np.min(score_top_rules[-2:]) > np.max(score_top_rules[:-2])
  104. assert_array_equal(pred, 6 * [0] + 2 * [1])
  105. assert_array_equal(pred_score_top_rules, 6 * [0] + 2 * [1])
  106. @pytest.mark.filterwarnings("ignore::UserWarning")
  107. def test_deduplication_works():
  108. # toy sample (the last two samples are outliers)
  109. X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1], [6, 3], [4, -7]]
  110. y = [0] * 6 + [1] * 2
  111. X_test = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1],
  112. [10, 5], [5, -7]]
  113. # Test LOF
  114. clf = SkopeRulesClassifier(random_state=rng, max_samples=1., max_depth_duplication=3)
  115. clf.fit(X, y)
  116. decision_func = clf._eval_weighted_rule_sum(X_test)
  117. rules_vote = clf._rules_vote(X_test)
  118. score_top_rules = clf._score_top_rules(X_test)
  119. pred = clf.predict(X_test)
  120. pred_score_top_rules = clf._predict_top_rules(X_test, 1)
  121. assert True, 'deduplication works'
  122. def test_performances():
  123. X, y = make_blobs(n_samples=1000, random_state=0, centers=2)
  124. # make labels imbalanced by remove all but 100 instances from class 1
  125. indexes = np.ones(X.shape[0]).astype(bool)
  126. ind = np.array([False] * 100 + list(((y == 1)[100:])))
  127. indexes[ind] = 0
  128. X = X[indexes]
  129. y = y[indexes]
  130. n_samples, n_features = X.shape
  131. clf = SkopeRulesClassifier()
  132. # fit
  133. clf.fit(X, y)
  134. # with lists
  135. clf.fit(X.tolist(), y.tolist())
  136. y_pred = clf.predict(X)
  137. assert y_pred.shape == (n_samples,)
  138. # training set performance
  139. assert accuracy_score(y, y_pred) > 0.83
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...