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

fpskope.py 2.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
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
  1. from typing import List
  2. import numpy as np
  3. import pandas as pd
  4. from imodels.rule_set.skope_rules import SkopeRulesClassifier
  5. from imodels.util.convert import itemsets_to_rules
  6. from imodels.util.extract import extract_fpgrowth
  7. from imodels.util.rule import Rule
  8. from imodels.util.score import score_precision_recall
  9. class FPSkopeClassifier(SkopeRulesClassifier):
  10. def __init__(self,
  11. minsupport=0.1,
  12. maxcardinality=2,
  13. verbose=False,
  14. precision_min=0.5,
  15. recall_min=0.01,
  16. n_estimators=10,
  17. max_samples=.8,
  18. max_samples_features=1.,
  19. bootstrap=False,
  20. bootstrap_features=False,
  21. max_depth=3,
  22. max_depth_duplication=None,
  23. max_features=1.,
  24. min_samples_split=2,
  25. n_jobs=1,
  26. random_state=None):
  27. super().__init__(precision_min,
  28. recall_min,
  29. n_estimators,
  30. max_samples,
  31. max_samples_features,
  32. bootstrap,
  33. bootstrap_features,
  34. max_depth,
  35. max_depth_duplication,
  36. max_features,
  37. min_samples_split,
  38. n_jobs,
  39. random_state,
  40. verbose)
  41. self.minsupport = minsupport
  42. self.maxcardinality = maxcardinality
  43. self.verbose = verbose
  44. def fit(self, X, y=None, feature_names=None, undiscretized_features=[], sample_weight=None):
  45. self.undiscretized_features = undiscretized_features
  46. super().fit(X, y, feature_names=feature_names, sample_weight=sample_weight)
  47. return self
  48. def _extract_rules(self, X, y) -> List[str]:
  49. X = pd.DataFrame(X, columns=self.feature_placeholders)
  50. itemsets = extract_fpgrowth(X, minsupport=self.minsupport,
  51. maxcardinality=self.maxcardinality,
  52. verbose=self.verbose)
  53. return [itemsets_to_rules(itemsets)], [np.arange(X.shape[0])], [np.arange(len(self.feature_names))]
  54. def _score_rules(self, X, y, rules) -> List[Rule]:
  55. return score_precision_recall(X, y,
  56. rules,
  57. self.estimators_samples_,
  58. self.estimators_features_,
  59. self.feature_placeholders,
  60. oob=False)
Tip!

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

Comments

Loading...