12
12
from sklearn import metrics
13
13
import numpy as np
14
14
15
- from tests .utils import get_test_cases , assert_rules_are_equals , assert_accuracy_is_greater
15
+ from tests .utils import (
16
+ dir_path ,
17
+ get_test_cases ,
18
+ assert_rules_are_equals ,
19
+ assert_accuracy_is_greater ,
20
+ )
16
21
17
22
18
23
class TestClassifier (unittest .TestCase ):
@@ -152,7 +157,7 @@ def test_compare_with_java_results(self):
152
157
def test_predict_proba (self ):
153
158
test_case = get_test_cases ('ClassificationSnCTest' )[0 ]
154
159
params = test_case .induction_params
155
- clf = classification .ExpertRuleClassifier (** params )
160
+ clf = classification .RuleClassifier (** params )
156
161
example_set = test_case .example_set
157
162
clf .fit (
158
163
example_set .values ,
@@ -174,6 +179,24 @@ def test_predict_proba(self):
174
179
'Predicted probabilities should be in range [0, 1]'
175
180
)
176
181
182
+ def test_fit_and_predict_on_boolean_columns (self ):
183
+ test_case = get_test_cases ('ClassificationSnCTest' )[0 ]
184
+ params = test_case .induction_params
185
+ clf = classification .RuleClassifier (** params )
186
+ X , y = test_case .example_set .values , test_case .example_set .labels
187
+ X ['boolean_column' ] = np .random .randint (
188
+ low = 0 , high = 2 , size = X .shape [0 ]).astype (bool )
189
+ clf .fit (X , y )
190
+ clf .predict (X )
191
+
192
+ y = y .astype (bool )
193
+ clf .fit (X , y )
194
+ clf .predict (X )
195
+
196
+ y = pd .Series (y )
197
+ clf .fit (X , y )
198
+ clf .predict (X )
199
+
177
200
178
201
class TestExperClassifier (unittest .TestCase ):
179
202
@@ -222,6 +245,46 @@ def test_predict_proba(self):
222
245
'Predicted probabilities should be in range [0, 1]'
223
246
)
224
247
248
+ # Issue #17
249
+ def test_left_open_intervals_in_expert_induction (self ):
250
+ df = pd .DataFrame (arff .loadarff (
251
+ f'{ dir_path } /resources/data/seismic-bumps-train-minimal.arff' )[0 ]
252
+ )
253
+ X = df .drop ('class' , axis = 1 )
254
+ y = df ['class' ]
255
+
256
+ expert_rules = [
257
+ ('rule-0' , 'IF [[gimpuls = <-inf, 750)]] THEN class = {0}' ),
258
+ ('rule-1' , 'IF [[gimpuls = (750, inf)]] THEN class = {1}' )
259
+ ]
260
+
261
+ expert_preferred_conditions = [
262
+ ('preferred-condition-0' ,
263
+ '1: IF [[seismic = {a}]] THEN class = {0}' ),
264
+ ('preferred-attribute-0' ,
265
+ '1: IF [[gimpuls = Any]] THEN class = {1}' )
266
+ ]
267
+
268
+ expert_forbidden_conditions = [
269
+ ('forb-attribute-0' ,
270
+ '1: IF [[seismoacoustic = Any]] THEN class = {0}' ),
271
+ ('forb-attribute-1' , 'inf: IF [[ghazard = Any]] THEN class = {1}' )
272
+ ]
273
+ clf = classification .ExpertRuleClassifier (
274
+ minsupp_new = 8 ,
275
+ max_growing = 0 ,
276
+ extend_using_preferred = True ,
277
+ extend_using_automatic = True ,
278
+ induce_using_preferred = True ,
279
+ induce_using_automatic = True
280
+ )
281
+ clf .fit (
282
+ X , y ,
283
+ expert_rules = expert_rules ,
284
+ expert_preferred_conditions = expert_preferred_conditions ,
285
+ expert_forbidden_conditions = expert_forbidden_conditions
286
+ )
287
+
225
288
226
289
if __name__ == '__main__' :
227
290
unittest .main ()
0 commit comments