-
Notifications
You must be signed in to change notification settings - Fork 83
/
decision_tree_classifier.py
280 lines (264 loc) · 11.2 KB
/
decision_tree_classifier.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sklearn.tree.tree
import lale.docstrings
import lale.operators
_hyperparams_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'A decision tree classifier.',
'allOf': [{
'type': 'object',
'required': ['class_weight'],
'relevantToOptimizer': ['criterion', 'splitter', 'max_depth', 'min_samples_split', 'min_samples_leaf', 'max_features'],
'additionalProperties': False,
'properties': {
'criterion': {
'enum': ['gini', 'entropy'],
'default': 'gini',
'description': 'The function to measure the quality of a split. Supported criteria are'},
'splitter': {
'enum': ['best', 'random'],
'default': 'best',
'description': 'The strategy used to choose the split at each node. Supported'},
'max_depth': {
'anyOf': [{
'type': 'integer',
'minimumForOptimizer': 3,
'maximumForOptimizer': 5}, {
'enum': [None]}],
'default': None,
'description': 'The maximum depth of the tree. If None, then nodes are expanded until'},
'min_samples_split': {
'anyOf': [{
'type': 'integer',
'minimumForOptimizer': 2,
'maximumForOptimizer': 20,
'distribution': 'uniform'}, {
'type': 'number',
'minimumForOptimizer': 0.01,
'maximumForOptimizer': 0.5}],
'default': 2,
'description': 'The minimum number of samples required to split an internal node:'},
'min_samples_leaf': {
'anyOf': [{
'type': 'integer',
'minimumForOptimizer': 1,
'maximumForOptimizer': 20,
'distribution': 'uniform'}, {
'type': 'number',
'minimumForOptimizer': 0.01,
'maximumForOptimizer': 0.5}],
'default': 1,
'description': 'The minimum number of samples required to be at a leaf node.'},
'min_weight_fraction_leaf': {
'type': 'number',
'default': 0.0,
'description': 'The minimum weighted fraction of the sum total of weights (of all'},
'max_features': {
'anyOf': [{
'type': 'integer',
'forOptimizer': False}, {
'type': 'number',
'minimum': 0.0,
'exclusiveMinimum': True,
'minimumForOptimizer': 0.0,
'maximumForOptimizer': 1.0,
'distribution': 'uniform'}, {
'enum': ['auto', 'sqrt', 'log2', None]}],
'default': None,
'description': 'The number of features to consider when looking for the best split:'},
'random_state': {
'anyOf': [{
'type': 'integer'}, {
'type': 'object'}, {
'enum': [None]}],
'default': None,
'description': 'If int, random_state is the seed used by the random number generator;'},
'max_leaf_nodes': {
'anyOf': [{
'type': 'integer'}, {
'enum': [None]}],
'default': None,
'description': 'Grow a tree with ``max_leaf_nodes`` in best-first fashion.'},
'min_impurity_decrease': {
'type': 'number',
'default': 0.0,
'description': 'A node will be split if this split induces a decrease of the impurity'},
'min_impurity_split': {
'anyOf':[
{'type': 'number'},{
'enum': [None]
}],
'default': None,
'description': 'Threshold for early stopping in tree growth. A node will split'},
'class_weight': {
'anyOf': [{
'type': 'object'}, #dict, list of dicts,
{'enum': ['balanced', 'balanced_subsample', None]}],
'description': 'Weights associated with classes in the form ``{class_label: weight}``.',
'default': None},
'presort': {
'type': 'boolean',
'default': False,
'description': 'Whether to presort the data to speed up the finding of best splits in'},
}}]
}
_input_fit_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Build a decision tree classifier from the training set (X, y).',
'required': ['X', 'y'],
'type': 'object',
'properties': {
'X': {
'anyOf': [{
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
}}],
'description': 'The training input samples. Internally, it will be converted to'},
'y': {
'anyOf': [
{'type': 'array', 'items': {'type': 'number'}},
{'type': 'array', 'items': {'type': 'string'}},
{'type': 'array', 'items': {'type': 'boolean'}}],
'description': 'The target values (class labels) as integers or strings.'},
'sample_weight': {
'anyOf': [{
'type': 'array',
'items': {
'type': 'number'},
}, {
'enum': [None]}],
'description': 'Sample weights. If None, then samples are equally weighted. Splits'},
'check_input': {
'type': 'boolean',
'default': True,
'description': 'Allow to bypass several input checking.'},
'X_idx_sorted': {
'anyOf': [{
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
}}, {
'enum': [None]}],
'default': None,
'description': 'The indexes of the sorted training input samples. If many tree'},
},
}
_input_predict_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Predict class or regression value for X.',
'type': 'object',
'properties': {
'X': {
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
},
'description': 'The input samples. Internally, its dtype will be converted to'},
'check_input': {
'type': 'boolean',
'default': True,
'description': 'Allow to bypass several input checking.'},
},
}
_output_predict_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Predicted class label per sample.',
'anyOf': [
{'type': 'array', 'items': {'type': 'number'}},
{'type': 'array', 'items': {'type': 'string'}},
{'type': 'array', 'items': {'type': 'boolean'}}]}
_input_predict_proba_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Predict class probabilities of the input samples X.',
'type': 'object',
'properties': {
'X': {
'anyOf': [{
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
}}],
'description': 'The input samples. Internally, its dtype will be converted to'},
'check_input': {
'type': 'boolean',
'description': 'Run check_array on X.'},
},
}
_output_predict_proba_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Probability of the sample for each class in the model.',
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
}
}
_combined_schemas = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': """`Decision tree classifier`_ from scikit-learn.
.. _`Decision tree classifier`: https://scikit-learn.org/0.20/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn-tree-decisiontreeclassifier
""",
'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.decision_tree_classifier.html',
'type': 'object',
'tags': {
'pre': [],
'op': ['estimator', 'classifier'],
'post': []},
'properties': {
'hyperparams': _hyperparams_schema,
'input_fit': _input_fit_schema,
'input_predict': _input_predict_schema,
'output_predict': _output_predict_schema,
'input_predict_proba': _input_predict_proba_schema,
'output_predict_proba': _output_predict_proba_schema}}
class DecisionTreeClassifierImpl():
def __init__(self, criterion='gini', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, presort=False):
self._hyperparams = {
'criterion': criterion,
'splitter': splitter,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'min_weight_fraction_leaf': min_weight_fraction_leaf,
'max_features': max_features,
'random_state': random_state,
'max_leaf_nodes': max_leaf_nodes,
'min_impurity_decrease': min_impurity_decrease,
'min_impurity_split': min_impurity_split,
'class_weight': class_weight,
'presort': presort}
self._wrapped_model = sklearn.tree.tree.DecisionTreeClassifier(**self._hyperparams)
def fit(self, X, y, **fit_params):
if fit_params is None:
self._wrapped_model.fit(X, y)
else:
self._wrapped_model.fit(X, y, **fit_params)
return self
def predict(self, X):
return self._wrapped_model.predict(X)
def predict_proba(self, X):
return self._wrapped_model.predict_proba(X)
lale.docstrings.set_docstrings(DecisionTreeClassifierImpl, _combined_schemas)
DecisionTreeClassifier = lale.operators.make_operator(DecisionTreeClassifierImpl, _combined_schemas)