forked from christophM/rulefit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rulefit.py
84 lines (63 loc) · 2.86 KB
/
test_rulefit.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
from rulefit import FriedScale, RuleCondition, Rule, RuleEnsemble, RuleFit
import numpy as np
rule_condition_smaller = RuleCondition(1, 5, "<=", 0.4)
rule_condition_greater = RuleCondition(0, 1, ">", 0.1)
X = np.array([[1,2,3], [4,5,6], [7,8,9]])
## Testing RuleCondition
def test_rule_condition_hashing_equal1():
assert (RuleCondition(1, 5, "<=", 0.4) == RuleCondition(1, 5, "<=", 0.4))
def test_rule_condition_hashing_equal2():
assert (RuleCondition(1, 5, "<=", 0.5) == RuleCondition(1, 5, "<=", 0.4))
def test_rule_condition_hashing_different1():
assert (RuleCondition(1, 4, "<=", 0.4) != RuleCondition(1, 5, "<=", 0.4))
def test_rule_condition_hashing_different2():
assert (RuleCondition(1, 5, ">", 0.4) != RuleCondition(1, 5, "<=", 0.4))
def test_rule_condition_hashing_different3():
assert (RuleCondition(2, 5, ">", 0.4) != RuleCondition(1, 5, ">", 0.4))
def test_rule_condition_smaller():
np.testing.assert_array_equal(rule_condition_smaller.transform(X),
np.array([1,1,0]))
def test_rule_condition_greater():
np.testing.assert_array_equal(rule_condition_greater.transform(X),
np.array([0,1,1]))
## Testing rule
rule = Rule([rule_condition_smaller, rule_condition_greater],0)
def test_rule_transform():
np.testing.assert_array_equal(rule.transform(X),
np.array([0,1,0]))
def test_rule_equality():
rule2 = Rule([rule_condition_greater, rule_condition_smaller],0)
assert rule == rule2
## Testing FriedScale():
def test_fried_scale():
x_scale_test=np.zeros([100,2])
x_scale_test[0:5,0]=-100
x_scale_test[5:10,0]=100
x_scale_test[10:55,0]=1
x_scale_test[5:55,1]=1 # winsorised version of first column at trim=0.1: note, will not be scaled because it is already an indicator function, as per FP004
fs=FriedScale(trim_quantile=0.1)
fs.train(x_scale_test)
np.testing.assert_array_equal(fs.scale(x_scale_test),
np.hstack([x_scale_test[:,1].reshape([-1,1])*0.4/np.std(x_scale_test[:,1]),x_scale_test[:,1].reshape([-1,1])]))
def run_all_tests():
test_rule_condition_hashing_equal1()
test_rule_condition_hashing_equal2()
test_rule_condition_hashing_different1()
test_rule_condition_hashing_different2()
test_rule_condition_hashing_different3()
test_rule_condition_smaller()
test_rule_condition_greater()
test_rule_transform()
test_rule_equality()
# test_fried_scale() TODO: Fix this test as FriedScale signature has changed.
run_all_tests()
## Test rule extractions function
## TODO
## RuleEnsemble
## - Construct ensemble with 2 short trees and test results
## - Test filter rules with only rules that only have the "<=" operator
## - Test filter short rules
## - Test transform function
## RuleFit
## - Throw value error when tree generator wrong class
## -