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

rulefit_test.py 2.2 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
  1. import numpy as np
  2. from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
  3. from sklearn.exceptions import ConvergenceWarning
  4. from sklearn.utils._testing import ignore_warnings
  5. from imodels.rule_set.rule_fit import RuleFitRegressor
  6. from imodels.util.transforms import FriedScale
  7. ## Testing FriedScale():
  8. def test_fried_scale():
  9. x_scale_test = np.zeros([100, 2])
  10. x_scale_test[0:5, 0] = -100
  11. x_scale_test[5:10, 0] = 100
  12. x_scale_test[10:55, 0] = 1
  13. x_scale_test[5:55,
  14. 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
  15. fs = FriedScale() # trim_quantile=0.1)
  16. fs.train(x_scale_test)
  17. '''
  18. np.testing.assert_array_equal(fs.scale(x_scale_test),
  19. np.hstack([x_scale_test[:, 1].reshape([-1, 1]) * 0.4 / np.std(x_scale_test[:, 1]),
  20. x_scale_test[:, 1].reshape([-1, 1])]))
  21. '''
  22. @ignore_warnings(category=ConvergenceWarning)
  23. def test_integration():
  24. X = np.array([[1, 99, 43, 34],
  25. [1, 76, 22, 10],
  26. [0, 83, 11, 0],
  27. [0, 99, 74, 33],
  28. [0, 53, 40, 34]])
  29. y = np.array([1, 0, 1, 1, 0])
  30. rfr = RuleFitRegressor(exp_rand_tree_size=False, n_estimators=500, random_state=1, include_linear=False,
  31. max_rules=None, alpha=0.1)
  32. rfr.fit(X, y)
  33. print(len(rfr._get_rules()))
  34. expected = np.array([0.83333333, 0.25, 0.83333333, 0.83333333, 0.25])
  35. assert np.allclose(rfr.predict(X), expected, atol=1.0e-04)
  36. rfr = RuleFitRegressor(exp_rand_tree_size=False, n_estimators=5, random_state=0, max_rules=None, alpha=0.01)
  37. rfr.fit(X, y)
  38. expected = np.array([0.89630491, 0.15375469, 0.89624531, 1.05000033, 0.00369476])
  39. assert np.allclose(rfr.predict(X), expected)
  40. rfr = RuleFitRegressor(exp_rand_tree_size=False, n_estimators=5, random_state=0,
  41. max_rules=None, alpha=0.01, tree_generator=RandomForestClassifier())
  42. rfr.fit(X, y)
  43. # expected = np.array([0.89630491, 0.15375469, 0.89624531, 1.05000033, 0.00369476])
  44. # assert np.allclose(rfr.predict(X), expected)
Tip!

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

Comments

Loading...