-
Notifications
You must be signed in to change notification settings - Fork 28
/
complexity.py
281 lines (214 loc) · 13.2 KB
/
complexity.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
"""
This file is part of the TPOT library.
The current version of TPOT was developed at Cedars-Sinai by:
- Pedro Henrique Ribeiro (https://github.com/perib, https://www.linkedin.com/in/pedro-ribeiro/)
- Anil Saini (anil.saini@cshs.org)
- Jose Hernandez (jgh9094@gmail.com)
- Jay Moran (jay.moran@cshs.org)
- Nicholas Matsumoto (nicholas.matsumoto@cshs.org)
- Hyunjun Choi (hyunjun.choi@cshs.org)
- Miguel E. Hernandez (miguel.e.hernandez@cshs.org)
- Jason Moore (moorejh28@gmail.com)
The original version of TPOT was primarily developed at the University of Pennsylvania by:
- Randal S. Olson (rso@randalolson.com)
- Weixuan Fu (weixuanf@upenn.edu)
- Daniel Angell (dpa34@drexel.edu)
- Jason Moore (moorejh28@gmail.com)
- and many more generous open-source contributors
TPOT is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
TPOT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with TPOT. If not, see <http://www.gnu.org/licenses/>.
"""
from tpot2 import GraphPipeline
import numpy as np
import sklearn
import warnings
from functools import reduce # Valid in Python 2.6+, required in Python 3
import operator
from sklearn.multiclass import OneVsOneClassifier, OneVsRestClassifier
from sklearn.linear_model import SGDClassifier, LogisticRegression, SGDRegressor, Ridge, Lasso, ElasticNet, Lars, LassoLars, LassoLarsCV, RidgeCV, ElasticNetCV, PassiveAggressiveClassifier, ARDRegression
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier, ExtraTreesRegressor, ExtraTreesClassifier, AdaBoostRegressor, AdaBoostClassifier, GradientBoostingRegressor,RandomForestRegressor, BaggingRegressor, ExtraTreesRegressor, HistGradientBoostingClassifier, HistGradientBoostingRegressor
from sklearn.neural_network import MLPClassifier, MLPRegressor
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from xgboost import XGBClassifier, XGBRegressor
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.svm import SVC, SVR, LinearSVR, LinearSVC
from lightgbm import LGBMClassifier, LGBMRegressor
from sklearn.naive_bayes import GaussianNB, BernoulliNB, MultinomialNB
from sklearn.ensemble import StackingClassifier, StackingRegressor, VotingClassifier, VotingRegressor
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.gaussian_process import GaussianProcessRegressor, GaussianProcessClassifier
# MultinomialNB: params_MultinomialNB,
from sklearn.base import is_classifier, is_regressor
#https://scikit-learn.org/stable/auto_examples/applications/plot_model_complexity_influence.html
def _count_nonzero_coefficients_and_intercept(est):
n_coef = np.count_nonzero(est.coef_)
if hasattr(est, 'intercept_'):
n_coef += np.count_nonzero(est.intercept_)
return n_coef
#https://stackoverflow.com/questions/51139875/sklearn-randomforestregressor-number-of-trainable-parameters
def tree_complexity(tree):
return tree.tree_.node_count * 5 #each node has 5 parameters
#https://stackoverflow.com/questions/51139875/sklearn-randomforestregressor-number-of-trainable-parameters
def forest_complexity(forest):
all_trees = np.array(forest.estimators_)
if len(all_trees.shape)>1:
all_trees = all_trees.ravel()
return sum(tree_complexity(tree) for tree in all_trees)
def histgradientboosting_complexity(forest):
all_trees = np.array(forest._predictors)
if len(all_trees.shape)>1:
all_trees = all_trees.ravel()
return sum(len(tree.nodes)*5 for tree in all_trees)
def knn_complexity(knn):
return knn.n_neighbors
def support_vector_machine_complexity(svm):
count = 0
count += sum(svm.n_support_)
if svm.kernel == 'linear':
count += np.count_nonzero(svm.coef_)
return count
def sklearn_MLP_complexity(mlp):
n_layers = len(mlp.coefs_)
n_params = 0
for i in range(n_layers):
n_params += len(mlp.coefs_[i]) + len(mlp.intercepts_[i])
return n_params
def calculate_xgb_model_complexity(est):
df = est.get_booster().trees_to_dataframe()
cols_to_remove = ['Tree','Node', 'ID', 'count', 'Gain', 'Cover']
#keeps ['Feature', 'Split', 'Yes', 'No', 'Missing', 'Category']
#category is the specific category for a given feature. takes the place of split for categorical features
for col in cols_to_remove:
if col in df.columns:
df = df.drop(col, axis=1)
df = ~df.isna()
return df.sum().sum()
def BernoulliNB_Complexity(model):
num_coefficients = len(model.class_log_prior_) + len(model.feature_log_prob_)
return num_coefficients
def GaussianNB_Complexity(model):
num_coefficients = len(model.class_prior_) + len(model.theta_) + len(model.var_)
return num_coefficients
def MultinomialNB_Complexity(model):
num_coefficients = len(model.class_log_prior_) + len(model.feature_log_prob_)
return num_coefficients
def BaggingComplexity(est):
return sum([calculate_model_complexity(bagged) for bagged in est.estimators_])
def lightgbm_complexity(est):
df = est.booster_.trees_to_dataframe()
#remove tree_index and node_depth
cols_to_remove = ['node_index','tree_index', 'node_depth', 'count', 'parent_index']
for col in cols_to_remove:
if col in df.columns:
df = df.drop(col, axis=1)
s = df.shape
return s[0] * s[1]
def QuadraticDiscriminantAnalysis_complexity(est):
count = reduce(operator.mul,np.array(est.rotations_).shape) + reduce(operator.mul,np.array(est.scalings_).shape) + reduce(operator.mul,np.array(est.means_).shape) + reduce(operator.mul,np.array(est.priors_).shape)
return count
#TODO consider the complexity of the kernel?
def gaussian_process_classifier_complexity(est):
if isinstance(est.base_estimator_, OneVsOneClassifier) or isinstance(est.base_estimator_, OneVsRestClassifier):
count = 0
for clf in est.base_estimator_.estimators_:
count += len(clf.pi_)
return count
return len(est.base_estimator_.pi_)
#TODO consider the complexity of the kernel?
def gaussian_process_regressor_complexity(est):
return len(est.alpha_)
def adaboost_complexity(est):
return len(est.estimator_weights_) + sum(calculate_model_complexity(bagged) for bagged in est.estimators_)
def ensemble_complexity(est):
return sum(calculate_model_complexity(bagged) for bagged in est.estimators_)
complexity_objective_per_estimator = { LogisticRegression: _count_nonzero_coefficients_and_intercept,
SGDClassifier: _count_nonzero_coefficients_and_intercept,
LinearSVC : _count_nonzero_coefficients_and_intercept,
LinearSVR : _count_nonzero_coefficients_and_intercept,
ARDRegression: _count_nonzero_coefficients_and_intercept, #When predicting mean, only coef and intercept used. Though there are more params for the variance/covariance matrix
LinearDiscriminantAnalysis: _count_nonzero_coefficients_and_intercept,
QuadraticDiscriminantAnalysis: QuadraticDiscriminantAnalysis_complexity,
SGDRegressor: _count_nonzero_coefficients_and_intercept,
Ridge: _count_nonzero_coefficients_and_intercept,
Lasso: _count_nonzero_coefficients_and_intercept,
ElasticNet: _count_nonzero_coefficients_and_intercept,
Lars: _count_nonzero_coefficients_and_intercept,
LassoLars: _count_nonzero_coefficients_and_intercept,
LassoLarsCV: _count_nonzero_coefficients_and_intercept,
RidgeCV: _count_nonzero_coefficients_and_intercept,
ElasticNetCV: _count_nonzero_coefficients_and_intercept,
PassiveAggressiveClassifier: _count_nonzero_coefficients_and_intercept,
KNeighborsClassifier: knn_complexity,
KNeighborsRegressor: knn_complexity,
DecisionTreeClassifier: tree_complexity,
DecisionTreeRegressor: tree_complexity,
GradientBoostingRegressor: forest_complexity,
GradientBoostingClassifier: forest_complexity,
RandomForestClassifier : forest_complexity,
RandomForestRegressor: forest_complexity,
HistGradientBoostingClassifier: histgradientboosting_complexity,
HistGradientBoostingRegressor: histgradientboosting_complexity,
ExtraTreesRegressor: forest_complexity,
ExtraTreesClassifier: forest_complexity,
XGBClassifier: calculate_xgb_model_complexity,
XGBRegressor: calculate_xgb_model_complexity,
SVC : support_vector_machine_complexity,
SVR : support_vector_machine_complexity,
MLPClassifier: sklearn_MLP_complexity,
MLPRegressor: sklearn_MLP_complexity,
BaggingRegressor: BaggingComplexity,
BaggingClassifier: BaggingComplexity,
BernoulliNB: BernoulliNB_Complexity,
GaussianNB: GaussianNB_Complexity,
MultinomialNB: MultinomialNB_Complexity,
LGBMClassifier: lightgbm_complexity,
LGBMRegressor: lightgbm_complexity,
GaussianProcessClassifier: gaussian_process_classifier_complexity,
GaussianProcessRegressor: gaussian_process_regressor_complexity,
AdaBoostClassifier: adaboost_complexity,
AdaBoostRegressor: adaboost_complexity,
# StackingClassifier: ensemble_complexity,
# StackingRegressor: ensemble_complexity,
# VotingClassifier: ensemble_complexity,
# VotingRegressor: ensemble_complexity
}
def calculate_model_complexity(est):
if isinstance(est, sklearn.pipeline.Pipeline):
return sum(calculate_model_complexity(estimator) for _,estimator in est.steps)
if isinstance(est, sklearn.pipeline.FeatureUnion):
return sum(calculate_model_complexity(estimator) for _,estimator in est.transformer_list)
if isinstance(est, GraphPipeline):
return sum(calculate_model_complexity(est.graph.nodes[node]['instance']) for node in est.graph.nodes)
model_type = type(est)
if is_classifier(est) or is_regressor(est):
if model_type not in complexity_objective_per_estimator:
warnings.warn(f"Complexity objective not defined for this classifier/regressor: {model_type}")
if model_type in complexity_objective_per_estimator:
return complexity_objective_per_estimator[model_type](est)
#else, if is subclass of sklearn selector
elif issubclass(model_type, sklearn.feature_selection.SelectorMixin):
return 0
else:
return 1
def complexity_scorer(est, X=None, y=None):
"""
Estimates the number of learned parameters across all classifiers and regressors in the pipelines.
Additionally, currently transformers add 1 point and selectors add 0 points (since they don't affect the complexity of the "final" predictive pipeline.
Parameters
----------
est: sklearn.base.BaseEstimator
The estimator or pipeline to compute the complexity for
X: array-like
The input samples (unused)
y: array-like
The target values (unused)
"""
return calculate_model_complexity(est)