learnedbf 0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- learnedbf/BF/__init__.py +551 -0
- learnedbf/__init__.py +1724 -0
- learnedbf/classifiers/__init__.py +329 -0
- learnedbf/complexity_measures.py +18 -0
- learnedbf-0.6.dist-info/METADATA +20 -0
- learnedbf-0.6.dist-info/RECORD +13 -0
- learnedbf-0.6.dist-info/WHEEL +5 -0
- learnedbf-0.6.dist-info/licenses/LICENSE +201 -0
- learnedbf-0.6.dist-info/top_level.txt +2 -0
- tmp-whl/learnedbf/BF/__init__.py +551 -0
- tmp-whl/learnedbf/__init__.py +1724 -0
- tmp-whl/learnedbf/classifiers/__init__.py +329 -0
- tmp-whl/learnedbf/complexity_measures.py +18 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
|
|
2
|
+
import abc
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
6
|
+
from sklearn.neural_network import MLPRegressor
|
|
7
|
+
from sklearn.svm import LinearSVC
|
|
8
|
+
from sklearn.tree import DecisionTreeClassifier
|
|
9
|
+
from sklearn.utils.validation import check_is_fitted
|
|
10
|
+
|
|
11
|
+
def get_tree_size(tree, float_size=64, int_size=32):
|
|
12
|
+
num_leaves = sum(tree.tree_.feature < 0)
|
|
13
|
+
num_inner_nodes = tree.tree_.node_count - num_leaves
|
|
14
|
+
|
|
15
|
+
# one float per leave (predicted score, fraction of pos items in node)
|
|
16
|
+
# three ints per inner node (pointers to left and right subtrees,
|
|
17
|
+
# split feature index)
|
|
18
|
+
# one float per inner node (threshold)
|
|
19
|
+
|
|
20
|
+
if hasattr(tree, "float_size"):
|
|
21
|
+
float_size = tree.float_size
|
|
22
|
+
if hasattr(tree, "int_size"):
|
|
23
|
+
int_size = tree.int_size
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
space = num_leaves * float_size \
|
|
27
|
+
+ num_inner_nodes * (int_size * 3 + float_size)
|
|
28
|
+
return space
|
|
29
|
+
|
|
30
|
+
class ScoredClassifier:
|
|
31
|
+
__metaclass__ = abc.ABCMeta
|
|
32
|
+
|
|
33
|
+
# TODO add get_time and get_energy_consumption methods and implement
|
|
34
|
+
# them in the subclasses.
|
|
35
|
+
|
|
36
|
+
def __init__(self, float_size=64, int_size=32, **kwargs):
|
|
37
|
+
"""Create an instance of :class:`ScoredClassifier`, corresponding
|
|
38
|
+
to the extension of a binary classifier which outputs a classification
|
|
39
|
+
confidence, called score, intended as a real number. The higher the score, the most confident is the classifier in predicting the
|
|
40
|
+
positive class (that is, the class of keys stored in a Bloom filter).
|
|
41
|
+
|
|
42
|
+
:param classifier: sklearn classifier to be scored
|
|
43
|
+
:type classifier: :class:`sklearn.BaseEstimator`
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
self.float_size = float_size
|
|
47
|
+
self.int_size = int_size
|
|
48
|
+
|
|
49
|
+
@abc.abstractclassmethod
|
|
50
|
+
def get_size(self, float_size=64, int_size=32):
|
|
51
|
+
"""Return the size in bits of the classifier.
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
@abc.abstractclassmethod
|
|
57
|
+
def predict_score(self, X):
|
|
58
|
+
"""Output the prediction score for a list of queries.
|
|
59
|
+
|
|
60
|
+
:param X: queries to be predicted.
|
|
61
|
+
:type X: array of numerical arrays.
|
|
62
|
+
"""
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class ScoredMLP(ScoredClassifier, MLPRegressor):
|
|
67
|
+
"""Score-based MLP classifier for a *binary* problem.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
def __init__(self,
|
|
71
|
+
hidden_layer_sizes=(100,),
|
|
72
|
+
activation="relu",
|
|
73
|
+
*,
|
|
74
|
+
solver='adam',
|
|
75
|
+
alpha=0.0001,
|
|
76
|
+
batch_size='auto',
|
|
77
|
+
learning_rate="constant",
|
|
78
|
+
learning_rate_init=0.001,
|
|
79
|
+
power_t=0.5,
|
|
80
|
+
max_iter=200,
|
|
81
|
+
shuffle=True,
|
|
82
|
+
random_state=None,
|
|
83
|
+
tol=1e-4,
|
|
84
|
+
verbose=False,
|
|
85
|
+
warm_start=False,
|
|
86
|
+
momentum=0.9,
|
|
87
|
+
nesterovs_momentum=True,
|
|
88
|
+
early_stopping=False,
|
|
89
|
+
validation_fraction=0.1,
|
|
90
|
+
beta_1=0.9,
|
|
91
|
+
beta_2=0.999,
|
|
92
|
+
epsilon=1e-8,
|
|
93
|
+
n_iter_no_change=10,
|
|
94
|
+
max_fun=15000,
|
|
95
|
+
float_size=64,
|
|
96
|
+
int_size=32):
|
|
97
|
+
|
|
98
|
+
MLPRegressor.__init__(self,
|
|
99
|
+
hidden_layer_sizes=hidden_layer_sizes,
|
|
100
|
+
activation=activation,
|
|
101
|
+
solver=solver,
|
|
102
|
+
alpha=alpha,
|
|
103
|
+
batch_size=batch_size,
|
|
104
|
+
learning_rate=learning_rate,
|
|
105
|
+
learning_rate_init=learning_rate_init,
|
|
106
|
+
power_t=power_t,
|
|
107
|
+
max_iter=max_iter,
|
|
108
|
+
shuffle=shuffle,
|
|
109
|
+
random_state=random_state,
|
|
110
|
+
tol=tol,
|
|
111
|
+
verbose=verbose,
|
|
112
|
+
warm_start=warm_start,
|
|
113
|
+
momentum=momentum,
|
|
114
|
+
nesterovs_momentum=nesterovs_momentum,
|
|
115
|
+
early_stopping=early_stopping,
|
|
116
|
+
validation_fraction=validation_fraction,
|
|
117
|
+
beta_1=beta_1,
|
|
118
|
+
beta_2=beta_2,
|
|
119
|
+
epsilon=epsilon,
|
|
120
|
+
n_iter_no_change=n_iter_no_change, max_fun=max_fun)
|
|
121
|
+
|
|
122
|
+
ScoredClassifier.__init__(self,
|
|
123
|
+
float_size=float_size,
|
|
124
|
+
int_size=int_size)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def fit(self, X, y):
|
|
128
|
+
super(MLPRegressor, self).fit(X, y)
|
|
129
|
+
self.out_activation_ = 'logistic'
|
|
130
|
+
|
|
131
|
+
def predict_score(self, X):
|
|
132
|
+
check_is_fitted(self, 'n_features_in_')
|
|
133
|
+
return self.predict(X)
|
|
134
|
+
|
|
135
|
+
def get_size(self):
|
|
136
|
+
check_is_fitted(self, 'n_features_in_')
|
|
137
|
+
hidden_layer_sizes = np.array(self.hidden_layer_sizes)
|
|
138
|
+
|
|
139
|
+
first = np.insert(hidden_layer_sizes,
|
|
140
|
+
0, self.n_features_in_)
|
|
141
|
+
# we add 1 to take into account biases
|
|
142
|
+
first += np.ones(len(first)).astype(int)
|
|
143
|
+
second = np.append(hidden_layer_sizes, 1)
|
|
144
|
+
num_connections = np.dot(first, second)
|
|
145
|
+
return num_connections * self.float_size
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class ScoredLinearSVC(ScoredClassifier, LinearSVC):
|
|
149
|
+
"""Score-based linear SV classifier for a *binary* problem.
|
|
150
|
+
"""
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def __init__(self,
|
|
154
|
+
penalty='l2',
|
|
155
|
+
loss='squared_hinge',
|
|
156
|
+
*,
|
|
157
|
+
dual=True,
|
|
158
|
+
tol=1e-4,
|
|
159
|
+
C=1.0,
|
|
160
|
+
multi_class='ovr',
|
|
161
|
+
fit_intercept=True,
|
|
162
|
+
intercept_scaling=1,
|
|
163
|
+
class_weight=None,
|
|
164
|
+
verbose=0,
|
|
165
|
+
random_state=None,
|
|
166
|
+
max_iter=1000,
|
|
167
|
+
float_size=64,
|
|
168
|
+
int_size=32):
|
|
169
|
+
|
|
170
|
+
LinearSVC.__init__(self,
|
|
171
|
+
penalty=penalty,
|
|
172
|
+
loss=loss,
|
|
173
|
+
dual=dual,
|
|
174
|
+
tol=tol,
|
|
175
|
+
C=C,
|
|
176
|
+
multi_class=multi_class,
|
|
177
|
+
fit_intercept=fit_intercept,
|
|
178
|
+
intercept_scaling=intercept_scaling,
|
|
179
|
+
class_weight=class_weight,
|
|
180
|
+
verbose=verbose,
|
|
181
|
+
random_state=random_state,
|
|
182
|
+
max_iter=max_iter)
|
|
183
|
+
|
|
184
|
+
ScoredClassifier.__init__(self,
|
|
185
|
+
float_size=float_size,
|
|
186
|
+
int_size=int_size)
|
|
187
|
+
|
|
188
|
+
def predict_score(self, X):
|
|
189
|
+
check_is_fitted(self, 'n_features_in_')
|
|
190
|
+
|
|
191
|
+
if 1 not in self.classes_ and True not in self.classes_:
|
|
192
|
+
raise ValueError('LinearSVC not trained using'
|
|
193
|
+
'either 1 or True as positive label' )
|
|
194
|
+
|
|
195
|
+
# Note that LinearSVC decides which is the positive class
|
|
196
|
+
# depending on the order of examples passed to fit
|
|
197
|
+
# in any case, the first value in the .classes_ attribute corresponds
|
|
198
|
+
# to the chosen positive class.
|
|
199
|
+
# Therefore, if we know that the positive class is False / 0, the
|
|
200
|
+
# decision function value should be changed in sign.
|
|
201
|
+
|
|
202
|
+
decision = self.decision_function(X)
|
|
203
|
+
if self.classes_[0] is False or self.classes_[0] == 0:
|
|
204
|
+
decision = - decision
|
|
205
|
+
|
|
206
|
+
return 1 / (1 + np.exp(decision))
|
|
207
|
+
|
|
208
|
+
def get_size(self):
|
|
209
|
+
check_is_fitted(self, 'n_features_in_')
|
|
210
|
+
return (1 + self.n_features_in_) * self.float_size
|
|
211
|
+
|
|
212
|
+
class ScoredDecisionTreeClassifier(ScoredClassifier, DecisionTreeClassifier):
|
|
213
|
+
"""Score-based Decision Tree classifier for a *binary* problem.
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
def __init__(self, *,
|
|
217
|
+
criterion="gini",
|
|
218
|
+
splitter="best",
|
|
219
|
+
max_depth=None,
|
|
220
|
+
min_samples_split=2,
|
|
221
|
+
min_samples_leaf=1,
|
|
222
|
+
min_weight_fraction_leaf=0.,
|
|
223
|
+
max_features=None,
|
|
224
|
+
random_state=None,
|
|
225
|
+
max_leaf_nodes=None,
|
|
226
|
+
min_impurity_decrease=0.,
|
|
227
|
+
class_weight=None,
|
|
228
|
+
ccp_alpha=0.0,
|
|
229
|
+
float_size=64,
|
|
230
|
+
int_size=32):
|
|
231
|
+
|
|
232
|
+
DecisionTreeClassifier.__init__(self,
|
|
233
|
+
criterion=criterion,
|
|
234
|
+
splitter=splitter,
|
|
235
|
+
max_depth=max_depth,
|
|
236
|
+
min_samples_split=min_samples_split,
|
|
237
|
+
min_samples_leaf=min_samples_leaf,
|
|
238
|
+
min_weight_fraction_leaf=min_weight_fraction_leaf,
|
|
239
|
+
max_features=max_features,
|
|
240
|
+
random_state=random_state,
|
|
241
|
+
max_leaf_nodes=max_leaf_nodes,
|
|
242
|
+
min_impurity_decrease=min_impurity_decrease,
|
|
243
|
+
class_weight=class_weight,
|
|
244
|
+
ccp_alpha=ccp_alpha)
|
|
245
|
+
|
|
246
|
+
ScoredClassifier.__init__(self, float_size, int_size)
|
|
247
|
+
|
|
248
|
+
def predict_score(self, X):
|
|
249
|
+
check_is_fitted(self, 'classes_')
|
|
250
|
+
score_dict = [{c: p for c, p in zip(self.classes_, x)}
|
|
251
|
+
for x in self.predict_proba(X)]
|
|
252
|
+
|
|
253
|
+
if 1 not in self.classes_ and True not in self.classes_:
|
|
254
|
+
raise ValueError('DecisionTreeClassifier not trained using'
|
|
255
|
+
'either 1 or True as positive label' )
|
|
256
|
+
|
|
257
|
+
pos_key = 1 if 1 in self.classes_ else True
|
|
258
|
+
|
|
259
|
+
return [d[pos_key] for d in score_dict]
|
|
260
|
+
|
|
261
|
+
def get_size(self):
|
|
262
|
+
check_is_fitted(self, 'tree_')
|
|
263
|
+
return get_tree_size(self)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class ScoredRandomForestClassifier(ScoredClassifier, RandomForestClassifier):
|
|
267
|
+
"""Score-based Random Forest classifier for a *binary* problem.
|
|
268
|
+
"""
|
|
269
|
+
def __init__(self,
|
|
270
|
+
n_estimators=100,
|
|
271
|
+
criterion="gini",
|
|
272
|
+
max_depth=None,
|
|
273
|
+
min_samples_split=2,
|
|
274
|
+
min_samples_leaf=1,
|
|
275
|
+
min_weight_fraction_leaf=0.,
|
|
276
|
+
max_features="auto",
|
|
277
|
+
max_leaf_nodes=None,
|
|
278
|
+
min_impurity_decrease=0.,
|
|
279
|
+
bootstrap=True,
|
|
280
|
+
oob_score=False,
|
|
281
|
+
n_jobs=None,
|
|
282
|
+
random_state=None,
|
|
283
|
+
verbose=0,
|
|
284
|
+
warm_start=False,
|
|
285
|
+
class_weight=None,
|
|
286
|
+
ccp_alpha=0.0,
|
|
287
|
+
max_samples=None,
|
|
288
|
+
float_size=64,
|
|
289
|
+
int_size=32):
|
|
290
|
+
|
|
291
|
+
RandomForestClassifier.__init__(self,
|
|
292
|
+
n_estimators=n_estimators,
|
|
293
|
+
criterion=criterion,
|
|
294
|
+
max_depth=max_depth,
|
|
295
|
+
min_samples_split=min_samples_split,
|
|
296
|
+
min_samples_leaf=min_samples_leaf,
|
|
297
|
+
min_weight_fraction_leaf=min_weight_fraction_leaf,
|
|
298
|
+
max_features=max_features,
|
|
299
|
+
max_leaf_nodes=max_leaf_nodes,
|
|
300
|
+
min_impurity_decrease=min_impurity_decrease,
|
|
301
|
+
bootstrap=bootstrap,
|
|
302
|
+
oob_score=oob_score,
|
|
303
|
+
n_jobs=n_jobs,
|
|
304
|
+
random_state=random_state,
|
|
305
|
+
verbose=verbose,
|
|
306
|
+
warm_start=warm_start,
|
|
307
|
+
class_weight=class_weight,
|
|
308
|
+
ccp_alpha=ccp_alpha,
|
|
309
|
+
max_samples=max_samples)
|
|
310
|
+
|
|
311
|
+
ScoredClassifier.__init__(self, float_size, int_size)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def predict_score(self, X):
|
|
315
|
+
check_is_fitted(self, 'classes_')
|
|
316
|
+
score_dict = [{c: p for c, p in zip(self.classes_, x)}
|
|
317
|
+
for x in self.predict_proba(X)]
|
|
318
|
+
|
|
319
|
+
if 1 not in self.classes_ and True not in self.classes_:
|
|
320
|
+
raise ValueError('RandomForestClassifier not trained using'
|
|
321
|
+
'either 1 or True as positive label' )
|
|
322
|
+
|
|
323
|
+
pos_key = 1 if 1 in self.classes_ else True
|
|
324
|
+
|
|
325
|
+
return [d[pos_key] for d in score_dict]
|
|
326
|
+
|
|
327
|
+
def get_size(self):
|
|
328
|
+
check_is_fitted(self, 'estimators_')
|
|
329
|
+
return sum([get_tree_size(t, float_size=self.float_size, int_size=self.int_size) for t in self.estimators_])
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
|
|
3
|
+
import problexity.classification as px
|
|
4
|
+
|
|
5
|
+
class ComplexityMeasure:
|
|
6
|
+
@classmethod
|
|
7
|
+
def compute(cls, X, y):
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
class F1v(ComplexityMeasure):
|
|
11
|
+
@classmethod
|
|
12
|
+
def compute(cls, X, y):
|
|
13
|
+
return px.f1v(X, y)
|
|
14
|
+
|
|
15
|
+
class C2(ComplexityMeasure):
|
|
16
|
+
@classmethod
|
|
17
|
+
def compute(cls, X, y):
|
|
18
|
+
return px.c2(X, y)
|