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

automl.py 4.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  1. from sklearn.base import BaseEstimator
  2. from sklearn.base import RegressorMixin, ClassifierMixin
  3. from imodels import (
  4. RuleFitClassifier,
  5. TreeGAMClassifier,
  6. FIGSClassifier,
  7. HSTreeClassifier,
  8. RuleFitRegressor,
  9. TreeGAMRegressor,
  10. FIGSRegressor,
  11. HSTreeRegressor,
  12. )
  13. from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
  14. from sklearn.linear_model import LogisticRegression, ElasticNet, Ridge
  15. import imodels
  16. from sklearn.model_selection import GridSearchCV, train_test_split
  17. import numpy as np
  18. from sklearn.pipeline import Pipeline
  19. class AutoInterpretableModel(BaseEstimator):
  20. """Automatically fit and select a classifier that is interpretable.
  21. Note that all preprocessing should be done beforehand.
  22. This is basically a wrapper around GridSearchCV, with some preselected models.
  23. """
  24. def __init__(self, param_grid=None, refit=True):
  25. if param_grid is None:
  26. if isinstance(self, ClassifierMixin):
  27. self.param_grid = self.PARAM_GRID_DEFAULT_CLASSIFICATION
  28. elif isinstance(self, RegressorMixin):
  29. self.param_grid = self.PARAM_GRID_DEFAULT_REGRESSION
  30. else:
  31. self.param_grid = param_grid
  32. self.refit = refit
  33. def fit(self, X, y, cv=5):
  34. self.pipe_ = Pipeline([("est", BaseEstimator())]
  35. ) # Placeholder Estimator
  36. if isinstance(self, ClassifierMixin):
  37. scoring = "roc_auc"
  38. elif isinstance(self, RegressorMixin):
  39. scoring = "r2"
  40. self.est_ = GridSearchCV(
  41. self.pipe_, self.param_grid, scoring=scoring, cv=cv, refit=self.refit)
  42. self.est_.fit(X, y)
  43. return self
  44. def predict(self, X):
  45. return self.est_.predict(X)
  46. def predict_proba(self, X):
  47. return self.est_.predict_proba(X)
  48. def score(self, X, y):
  49. return self.est_.score(X, y)
  50. PARAM_GRID_LINEAR_CLASSIFICATION = [
  51. {
  52. "est": [
  53. LogisticRegression(
  54. solver="saga", penalty="elasticnet", max_iter=100, random_state=42)
  55. ],
  56. "est__C": [0.1, 1, 10],
  57. "est__l1_ratio": [0, 0.5, 1],
  58. },
  59. ]
  60. PARAM_GRID_DEFAULT_CLASSIFICATION = [
  61. {
  62. "est": [DecisionTreeClassifier(random_state=42)],
  63. "est__max_leaf_nodes": [2, 5, 10],
  64. },
  65. {
  66. "est": [RuleFitClassifier(random_state=42)],
  67. "est__max_rules": [10, 100],
  68. "est__n_estimators": [20],
  69. },
  70. {
  71. "est": [TreeGAMClassifier(random_state=42)],
  72. "est__n_boosting_rounds": [10, 100],
  73. },
  74. {
  75. "est": [HSTreeClassifier(random_state=42)],
  76. "est__max_leaf_nodes": [5, 10],
  77. },
  78. {
  79. "est": [FIGSClassifier(random_state=42)],
  80. "est__max_rules": [5, 10],
  81. },
  82. ] + PARAM_GRID_LINEAR_CLASSIFICATION
  83. PARAM_GRID_LINEAR_REGRESSION = [
  84. {
  85. "est": [
  86. ElasticNet(max_iter=100, random_state=42)
  87. ],
  88. "est__alpha": [0.1, 1, 10],
  89. "est__l1_ratio": [0.5, 1],
  90. },
  91. {
  92. "est": [
  93. Ridge(max_iter=100, random_state=42)
  94. ],
  95. "est__alpha": [0, 0.1, 1, 10],
  96. },
  97. ]
  98. PARAM_GRID_DEFAULT_REGRESSION = [
  99. {
  100. "est": [DecisionTreeRegressor()],
  101. "est__max_leaf_nodes": [2, 5, 10],
  102. },
  103. {
  104. "est": [HSTreeRegressor()],
  105. "est__max_leaf_nodes": [5, 10],
  106. },
  107. {
  108. "est": [RuleFitRegressor()],
  109. "est__max_rules": [10, 100],
  110. "est__n_estimators": [20],
  111. },
  112. {
  113. "est": [TreeGAMRegressor()],
  114. "est__n_boosting_rounds": [10, 100],
  115. },
  116. {
  117. "est": [FIGSRegressor()],
  118. "est__max_rules": [5, 10],
  119. },
  120. ] + PARAM_GRID_LINEAR_REGRESSION
  121. class AutoInterpretableClassifier(AutoInterpretableModel, ClassifierMixin):
  122. ...
  123. class AutoInterpretableRegressor(AutoInterpretableModel, RegressorMixin):
  124. ...
  125. if __name__ == "__main__":
  126. X, y, feature_names = imodels.get_clean_dataset("heart")
  127. print("shapes", X.shape, y.shape, "nunique", np.unique(y).size)
  128. X_train, X_test, y_train, y_test = train_test_split(
  129. X, y, random_state=42, test_size=0.2
  130. )
  131. m = AutoInterpretableClassifier()
  132. # m = AutoInterpretableRegressor()
  133. m.fit(X_train, y_train)
  134. print("best params", m.est_.best_params_)
  135. print("best score", m.est_.best_score_)
  136. print("best estimator", m.est_.best_estimator_)
  137. print("best estimator params", m.est_.best_estimator_.get_params())
Tip!

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

Comments

Loading...