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

util_test.py 1.6 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
  1. from imodels import SkopeRulesClassifier
  2. from imodels.util.prune import deduplicate, find_similar_rulesets, f1_score
  3. from imodels.util.rule import Rule
  4. def test_similarity_tree():
  5. # Test that rules are well splitted
  6. rules = [Rule("a <= 2 and b > 45 and c <= 3 and a > 4", args=(1, 1, 0)),
  7. Rule("a <= 2 and b > 45 and c <= 3 and a > 4", (1, 1, 0)),
  8. Rule("a > 2 and b > 45", (0.5, 0.3, 0)),
  9. Rule("a > 2 and b > 40", (0.5, 0.2, 0)),
  10. Rule("a <= 2 and b <= 45", (1, 1, 0)),
  11. Rule("a > 2 and c <= 3", (1, 1, 0)),
  12. Rule("b > 45", (1, 1, 0))]
  13. sk = SkopeRulesClassifier(max_depth_duplication=2)
  14. rulesets = find_similar_rulesets(rules, max_depth_duplication=2)
  15. # Assert some couples of rules are in the same bag
  16. idx_bags_rules = []
  17. for idx_rule, r in enumerate(rules):
  18. idx_bags_for_rule = []
  19. for idx_bag, bag in enumerate(rulesets):
  20. if r in bag:
  21. idx_bags_for_rule.append(idx_bag)
  22. idx_bags_rules.append(idx_bags_for_rule)
  23. assert idx_bags_rules[0] == idx_bags_rules[1]
  24. assert not idx_bags_rules[0] == idx_bags_rules[2]
  25. # Assert the best rules are kept
  26. final_rules = deduplicate(rules, sk.max_depth_duplication)
  27. assert rules[0] in final_rules
  28. assert rules[2] in final_rules
  29. assert not rules[3] in final_rules
  30. def test_f1_score():
  31. rule0 = Rule('a > 0', (0, 0, 0))
  32. rule1 = Rule('a > 0', (0.5, 0.5, 0))
  33. rule2 = Rule('a > 0', (0.5, 0, 0))
  34. assert f1_score(rule0) == 0
  35. assert f1_score(rule1) == 0.5
  36. assert f1_score(rule2) == 0
Tip!

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

Comments

Loading...