learnedbf 0.6.2__tar.gz → 1.0.0__tar.gz
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-1.0.0/PKG-INFO +197 -0
- learnedbf-1.0.0/README.md +173 -0
- learnedbf-1.0.0/docs/conf.py +34 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf/BF/__init__.py +5 -3
- learnedbf-1.0.0/learnedbf/BF/fast_plbf_bloom_filter_adapter.py +15 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf/__init__.py +906 -225
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf/classifiers/__init__.py +1 -1
- learnedbf-1.0.0/learnedbf/fastPLBF/FastPLBF.py +122 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/FastPLBF_M.py +124 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/FastPLBFpp.py +124 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/FastPLBFpp_M.py +126 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/PLBF.py +166 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/PLBF_M.py +173 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/ExpectedFPR.py +27 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/OptimalFPR.py +87 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/OptimalFPR_M.py +93 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/SpaceUsed.py +34 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/ThresMaxDivDP.py +124 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/calc_DPKL.py +47 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/const.py +3 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/fast_calc_DPKL.py +65 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/matrix_problem_on_monotone_matrix.py +40 -0
- learnedbf-1.0.0/learnedbf/fastPLBF/utils/prList.py +125 -0
- learnedbf-1.0.0/learnedbf.egg-info/PKG-INFO +197 -0
- learnedbf-1.0.0/learnedbf.egg-info/SOURCES.txt +46 -0
- learnedbf-1.0.0/learnedbf.egg-info/requires.txt +6 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf.egg-info/top_level.txt +1 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/pyproject.toml +6 -6
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_AdaBF.py +8 -8
- learnedbf-1.0.0/tests/test_FastPLBF.py +70 -0
- learnedbf-1.0.0/tests/test_FastPLBF_M.py +77 -0
- learnedbf-1.0.0/tests/test_FastPLBFpp.py +70 -0
- learnedbf-1.0.0/tests/test_FastPLBFpp_M.py +77 -0
- learnedbf-1.0.0/tests/test_PLBF.py +71 -0
- learnedbf-1.0.0/tests/test_PLBF_M.py +79 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_SLBF.py +7 -7
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_UntrainedClassifiers.py +3 -3
- learnedbf-0.6.2/PKG-INFO +0 -26
- learnedbf-0.6.2/README.md +0 -2
- learnedbf-0.6.2/learnedbf.egg-info/PKG-INFO +0 -26
- learnedbf-0.6.2/learnedbf.egg-info/SOURCES.txt +0 -22
- learnedbf-0.6.2/learnedbf.egg-info/requires.txt +0 -6
- {learnedbf-0.6.2 → learnedbf-1.0.0}/LICENSE +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/MANIFEST.in +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf/complexity_measures.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/learnedbf.egg-info/dependency_links.txt +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/setup.cfg +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_BloomFilter.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_ClassicalBloomFilter.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_ScoredDT.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_ScoredLinearSVC.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_ScoredMLP.py +0 -0
- {learnedbf-0.6.2 → learnedbf-1.0.0}/tests/test_ScoredRF.py +0 -0
learnedbf-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: learnedbf
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A python package for Learned Bloom Filters
|
|
5
|
+
Author-email: SLIMlaboratory <slim@di.unimi.it>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/SLIMlaboratory/learnedbf
|
|
8
|
+
Project-URL: Documentation, https://learnedbf.readthedocs.io/
|
|
9
|
+
Project-URL: Issues, https://github.com/SLIMlaboratory/learnedbf/issues
|
|
10
|
+
Keywords: Bloom filters,Learned Bloom filters,machine learning
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: mmh3>=5.2
|
|
18
|
+
Requires-Dist: numpy>=1.22
|
|
19
|
+
Requires-Dist: packaging>=26
|
|
20
|
+
Requires-Dist: problexity>=0.5.8
|
|
21
|
+
Requires-Dist: pybloom_live>=4
|
|
22
|
+
Requires-Dist: scikit-learn>=1.8
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# learnedbf
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
<p align="center">
|
|
29
|
+
<a href="https://www.python.org"><img alt="Python version" src="https://img.shields.io/badge/Python-3.9-3776AB.svg?style=flat&logo=python&logoColor=white"></a>
|
|
30
|
+
<a href="https://github.com/SLIMlaboratory/learnedbf/actions"><img alt="Actions Status" src="https://github.com/psf/black/workflows/Test/badge.svg"></a>
|
|
31
|
+
<a href="https://learnedbf.readthedocs.io/en/latest/?badge=latest"><img alt="Documentation Status" src="https://readthedocs.org/projects/learnedbf/badge/?version=latest"></a>
|
|
32
|
+
<a href="https://pypi.org/project/learnedbf/"><img alt="PyPI" src="https://img.shields.io/pypi/v/learnedbf"></a>
|
|
33
|
+
<a href="https://github.com/SLIMlaboratory/learnedbf/blob/main/LICENSE"><img alt="License: Apache2.0" src="https://img.shields.io/badge/license-Apache%202.0-blue.svg"></a>
|
|
34
|
+
|
|
35
|
+
</p>
|
|
36
|
+
|
|
37
|
+
A python package for Learned Bloom Filters
|
|
38
|
+
|
|
39
|
+
`learnedbf` is a Python package for Learned Bloom Filters (LBF), intended as
|
|
40
|
+
[Bloom Filters](https://en.wikipedia.org/wiki/Bloom_filter) learned from
|
|
41
|
+
data, as originally proposed by
|
|
42
|
+
[Kraska et al., 2018](https://arxiv.org/abs/1712.01208).
|
|
43
|
+
|
|
44
|
+
This page provides a quick start guide. For more comprehensive information,
|
|
45
|
+
please refer to the [documentation](https://learned.readthedocs.io/en/latest/).
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
pip install learnedbf
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
### Library import
|
|
56
|
+
|
|
57
|
+
The following code imports all libraries used in the subsequent snippets.
|
|
58
|
+
|
|
59
|
+
```pycon
|
|
60
|
+
>>> import numpy as np
|
|
61
|
+
>>>
|
|
62
|
+
>>> from sklearn.datasets import make_classification
|
|
63
|
+
>>> from sklearn.metrics import accuracy_score
|
|
64
|
+
>>> from sklearn.model_selection import train_test_split
|
|
65
|
+
>>>
|
|
66
|
+
>>> import learnedbf as lbf
|
|
67
|
+
>>> from learnedbf.classifiers import ScoredLinearSVC, ScoredMLP
|
|
68
|
+
>>> from learnedbf import complexity_measures as cpl
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Evaluating the complexity of a dataset
|
|
72
|
+
|
|
73
|
+
The following code generates datasets of decrerasing complexity using the
|
|
74
|
+
`make_classification` function available in Scikit-learn, evaluating for each
|
|
75
|
+
the corresponding F1v measure.
|
|
76
|
+
|
|
77
|
+
```pycon
|
|
78
|
+
>>> f1v = cpl.F1v()
|
|
79
|
+
>>>
|
|
80
|
+
>>> sep = np.linspace(0.001, 1.5, 10)
|
|
81
|
+
>>> for s in sep:
|
|
82
|
+
... X, y = make_classification(n_samples=20000, n_features=2, n_redundant=0,
|
|
83
|
+
... class_sep=s)
|
|
84
|
+
... c = f1v.compute(X, y)
|
|
85
|
+
... print(f'class separation {s:.2f}, F1V {c:.2f}')
|
|
86
|
+
...
|
|
87
|
+
class separation 0.00, F1V 1.00
|
|
88
|
+
class separation 0.17, F1V 0.86
|
|
89
|
+
class separation 0.33, F1V 0.59
|
|
90
|
+
class separation 0.50, F1V 0.33
|
|
91
|
+
class separation 0.67, F1V 0.27
|
|
92
|
+
class separation 0.83, F1V 0.20
|
|
93
|
+
class separation 1.00, F1V 0.11
|
|
94
|
+
class separation 1.17, F1V 0.11
|
|
95
|
+
class separation 1.33, F1V 0.10
|
|
96
|
+
class separation 1.50, F1V 0.08
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Training classifiers
|
|
100
|
+
|
|
101
|
+
The following code generates the dataset used in the rest of the examples,
|
|
102
|
+
dividing it in three splits. The first two ones will be used for training a
|
|
103
|
+
classifier and evaluating its performance; the third one will be used afterwards
|
|
104
|
+
to estimate the FPR of the built filters.
|
|
105
|
+
|
|
106
|
+
```pycon
|
|
107
|
+
>>> X, y = make_classification(n_samples=20000, n_features=2, n_redundant=0,
|
|
108
|
+
>>> class_sep=0.5)
|
|
109
|
+
>>> y = y.astype(bool)
|
|
110
|
+
>>> X_build, X_evaluate, y_build, y_evaluate = train_test_split(X, y,
|
|
111
|
+
... test_size=0.1)
|
|
112
|
+
>>> X_train, X_test, y_train, y_test = train_test_split(X_build, y_build,
|
|
113
|
+
... test_size=0.1)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The following code trains a linear SVC and a multi-layer perceptron, comparing
|
|
117
|
+
their performance on the test set using accuracy.
|
|
118
|
+
|
|
119
|
+
```pycon
|
|
120
|
+
>>> svc = ScoredLinearSVC()
|
|
121
|
+
>>> svc.fit(X_train, y_train)
|
|
122
|
+
>>>
|
|
123
|
+
>>> mlp = ScoredMLP()
|
|
124
|
+
>>> mlp.fit(X_train, y_train)
|
|
125
|
+
>>>
|
|
126
|
+
>>> threshold = 0.65
|
|
127
|
+
>>>
|
|
128
|
+
>>> svc_pred = (svc.predict_score(X_test) > threshold).astype(int)
|
|
129
|
+
>>> mlp_pred = (mlp.predict_score(X_test) > threshold).astype(int)
|
|
130
|
+
>>>
|
|
131
|
+
>>> svc_score = accuracy_score(y_test, svc_pred)
|
|
132
|
+
>>> mlp_score = accuracy_score(y_test, mlp_pred)
|
|
133
|
+
>>>
|
|
134
|
+
>>> print(f'SVC score = {svc_score:.2f}, MLP score = {mlp_score:.2f}')
|
|
135
|
+
SVC score = 0.56, MLP score = 0.85
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Building a learned Bloom filter
|
|
139
|
+
|
|
140
|
+
The following code builds a LBF using the previously learned MLP, and estimates
|
|
141
|
+
its empirical FPR.
|
|
142
|
+
|
|
143
|
+
```pycon
|
|
144
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
145
|
+
>>> filter.fit(X_build, y_build)
|
|
146
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7520010828581488)
|
|
147
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
148
|
+
FPR:0.009
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The following code builds a LBF backed by a multi-layer perceptron, now training
|
|
152
|
+
the latter on the provided data.
|
|
153
|
+
|
|
154
|
+
```pycon
|
|
155
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
156
|
+
>>> filter.fit(X_build, y_build)
|
|
157
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7520010828581488)
|
|
158
|
+
>>> mlp = ScoredMLP()
|
|
159
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size=0.2)
|
|
160
|
+
>>> filter.fit(X_build, y_build)
|
|
161
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7239171235297011)
|
|
162
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
163
|
+
FPR:0.009
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The following code repeats the previous operation, now also performing a model
|
|
167
|
+
selection on the learning rate of the multi-layer perceptron.
|
|
168
|
+
|
|
169
|
+
```pycon
|
|
170
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
171
|
+
>>> filter.fit(X_build, y_build)
|
|
172
|
+
>>>
|
|
173
|
+
>>> mlp = ScoredMLP()
|
|
174
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp,
|
|
175
|
+
... threshold_test_size=0.2,
|
|
176
|
+
... hyperparameters={
|
|
177
|
+
... 'learning_rate_init':[0.01, 0.005, 0.001, 0.0005]})
|
|
178
|
+
>>> filter.fit(X_build, y_build)
|
|
179
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), hyperparameters={'learning_rate_init': [0.01, 0.005, 0.001, 0.0005]}, threshold=0.741263017250332)
|
|
180
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
181
|
+
FPR:0.009
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
The library is distributed under the
|
|
187
|
+
[Apache 2.0](https://github.com/SLIMlaboratory/learnedbf/blob/master/LICENSE)
|
|
188
|
+
license. This project includes portions of code from
|
|
189
|
+
[FastPLBF] (https://github.com/atsukisato/FastPLBF), under the MIT License.
|
|
190
|
+
See THIRD_PARTY_LICENSES for details.
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
## Authors
|
|
195
|
+
|
|
196
|
+
`learnedbf` has been designed and implemented by D. Malchiodi, M. Frasca, N. Rinaldi and R. Giancarlo.
|
|
197
|
+
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# learnedbf
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
<p align="center">
|
|
5
|
+
<a href="https://www.python.org"><img alt="Python version" src="https://img.shields.io/badge/Python-3.9-3776AB.svg?style=flat&logo=python&logoColor=white"></a>
|
|
6
|
+
<a href="https://github.com/SLIMlaboratory/learnedbf/actions"><img alt="Actions Status" src="https://github.com/psf/black/workflows/Test/badge.svg"></a>
|
|
7
|
+
<a href="https://learnedbf.readthedocs.io/en/latest/?badge=latest"><img alt="Documentation Status" src="https://readthedocs.org/projects/learnedbf/badge/?version=latest"></a>
|
|
8
|
+
<a href="https://pypi.org/project/learnedbf/"><img alt="PyPI" src="https://img.shields.io/pypi/v/learnedbf"></a>
|
|
9
|
+
<a href="https://github.com/SLIMlaboratory/learnedbf/blob/main/LICENSE"><img alt="License: Apache2.0" src="https://img.shields.io/badge/license-Apache%202.0-blue.svg"></a>
|
|
10
|
+
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
A python package for Learned Bloom Filters
|
|
14
|
+
|
|
15
|
+
`learnedbf` is a Python package for Learned Bloom Filters (LBF), intended as
|
|
16
|
+
[Bloom Filters](https://en.wikipedia.org/wiki/Bloom_filter) learned from
|
|
17
|
+
data, as originally proposed by
|
|
18
|
+
[Kraska et al., 2018](https://arxiv.org/abs/1712.01208).
|
|
19
|
+
|
|
20
|
+
This page provides a quick start guide. For more comprehensive information,
|
|
21
|
+
please refer to the [documentation](https://learned.readthedocs.io/en/latest/).
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
pip install learnedbf
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Library import
|
|
32
|
+
|
|
33
|
+
The following code imports all libraries used in the subsequent snippets.
|
|
34
|
+
|
|
35
|
+
```pycon
|
|
36
|
+
>>> import numpy as np
|
|
37
|
+
>>>
|
|
38
|
+
>>> from sklearn.datasets import make_classification
|
|
39
|
+
>>> from sklearn.metrics import accuracy_score
|
|
40
|
+
>>> from sklearn.model_selection import train_test_split
|
|
41
|
+
>>>
|
|
42
|
+
>>> import learnedbf as lbf
|
|
43
|
+
>>> from learnedbf.classifiers import ScoredLinearSVC, ScoredMLP
|
|
44
|
+
>>> from learnedbf import complexity_measures as cpl
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Evaluating the complexity of a dataset
|
|
48
|
+
|
|
49
|
+
The following code generates datasets of decrerasing complexity using the
|
|
50
|
+
`make_classification` function available in Scikit-learn, evaluating for each
|
|
51
|
+
the corresponding F1v measure.
|
|
52
|
+
|
|
53
|
+
```pycon
|
|
54
|
+
>>> f1v = cpl.F1v()
|
|
55
|
+
>>>
|
|
56
|
+
>>> sep = np.linspace(0.001, 1.5, 10)
|
|
57
|
+
>>> for s in sep:
|
|
58
|
+
... X, y = make_classification(n_samples=20000, n_features=2, n_redundant=0,
|
|
59
|
+
... class_sep=s)
|
|
60
|
+
... c = f1v.compute(X, y)
|
|
61
|
+
... print(f'class separation {s:.2f}, F1V {c:.2f}')
|
|
62
|
+
...
|
|
63
|
+
class separation 0.00, F1V 1.00
|
|
64
|
+
class separation 0.17, F1V 0.86
|
|
65
|
+
class separation 0.33, F1V 0.59
|
|
66
|
+
class separation 0.50, F1V 0.33
|
|
67
|
+
class separation 0.67, F1V 0.27
|
|
68
|
+
class separation 0.83, F1V 0.20
|
|
69
|
+
class separation 1.00, F1V 0.11
|
|
70
|
+
class separation 1.17, F1V 0.11
|
|
71
|
+
class separation 1.33, F1V 0.10
|
|
72
|
+
class separation 1.50, F1V 0.08
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Training classifiers
|
|
76
|
+
|
|
77
|
+
The following code generates the dataset used in the rest of the examples,
|
|
78
|
+
dividing it in three splits. The first two ones will be used for training a
|
|
79
|
+
classifier and evaluating its performance; the third one will be used afterwards
|
|
80
|
+
to estimate the FPR of the built filters.
|
|
81
|
+
|
|
82
|
+
```pycon
|
|
83
|
+
>>> X, y = make_classification(n_samples=20000, n_features=2, n_redundant=0,
|
|
84
|
+
>>> class_sep=0.5)
|
|
85
|
+
>>> y = y.astype(bool)
|
|
86
|
+
>>> X_build, X_evaluate, y_build, y_evaluate = train_test_split(X, y,
|
|
87
|
+
... test_size=0.1)
|
|
88
|
+
>>> X_train, X_test, y_train, y_test = train_test_split(X_build, y_build,
|
|
89
|
+
... test_size=0.1)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The following code trains a linear SVC and a multi-layer perceptron, comparing
|
|
93
|
+
their performance on the test set using accuracy.
|
|
94
|
+
|
|
95
|
+
```pycon
|
|
96
|
+
>>> svc = ScoredLinearSVC()
|
|
97
|
+
>>> svc.fit(X_train, y_train)
|
|
98
|
+
>>>
|
|
99
|
+
>>> mlp = ScoredMLP()
|
|
100
|
+
>>> mlp.fit(X_train, y_train)
|
|
101
|
+
>>>
|
|
102
|
+
>>> threshold = 0.65
|
|
103
|
+
>>>
|
|
104
|
+
>>> svc_pred = (svc.predict_score(X_test) > threshold).astype(int)
|
|
105
|
+
>>> mlp_pred = (mlp.predict_score(X_test) > threshold).astype(int)
|
|
106
|
+
>>>
|
|
107
|
+
>>> svc_score = accuracy_score(y_test, svc_pred)
|
|
108
|
+
>>> mlp_score = accuracy_score(y_test, mlp_pred)
|
|
109
|
+
>>>
|
|
110
|
+
>>> print(f'SVC score = {svc_score:.2f}, MLP score = {mlp_score:.2f}')
|
|
111
|
+
SVC score = 0.56, MLP score = 0.85
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Building a learned Bloom filter
|
|
115
|
+
|
|
116
|
+
The following code builds a LBF using the previously learned MLP, and estimates
|
|
117
|
+
its empirical FPR.
|
|
118
|
+
|
|
119
|
+
```pycon
|
|
120
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
121
|
+
>>> filter.fit(X_build, y_build)
|
|
122
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7520010828581488)
|
|
123
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
124
|
+
FPR:0.009
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The following code builds a LBF backed by a multi-layer perceptron, now training
|
|
128
|
+
the latter on the provided data.
|
|
129
|
+
|
|
130
|
+
```pycon
|
|
131
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
132
|
+
>>> filter.fit(X_build, y_build)
|
|
133
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7520010828581488)
|
|
134
|
+
>>> mlp = ScoredMLP()
|
|
135
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size=0.2)
|
|
136
|
+
>>> filter.fit(X_build, y_build)
|
|
137
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), threshold=0.7239171235297011)
|
|
138
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
139
|
+
FPR:0.009
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The following code repeats the previous operation, now also performing a model
|
|
143
|
+
selection on the learning rate of the multi-layer perceptron.
|
|
144
|
+
|
|
145
|
+
```pycon
|
|
146
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp, threshold_test_size = 0.2)
|
|
147
|
+
>>> filter.fit(X_build, y_build)
|
|
148
|
+
>>>
|
|
149
|
+
>>> mlp = ScoredMLP()
|
|
150
|
+
>>> filter = lbf.LBF(epsilon=0.01, classifier=mlp,
|
|
151
|
+
... threshold_test_size=0.2,
|
|
152
|
+
... hyperparameters={
|
|
153
|
+
... 'learning_rate_init':[0.01, 0.005, 0.001, 0.0005]})
|
|
154
|
+
>>> filter.fit(X_build, y_build)
|
|
155
|
+
LBF(epsilon=0.01, classifier=ScoredMLP(), hyperparameters={'learning_rate_init': [0.01, 0.005, 0.001, 0.0005]}, threshold=0.741263017250332)
|
|
156
|
+
>>> print(f"FPR:{filter.estimate_FPR(X_evaluate[y_evaluate==0]):.3f}")
|
|
157
|
+
FPR:0.009
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
The library is distributed under the
|
|
163
|
+
[Apache 2.0](https://github.com/SLIMlaboratory/learnedbf/blob/master/LICENSE)
|
|
164
|
+
license. This project includes portions of code from
|
|
165
|
+
[FastPLBF] (https://github.com/atsukisato/FastPLBF), under the MIT License.
|
|
166
|
+
See THIRD_PARTY_LICENSES for details.
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
## Authors
|
|
171
|
+
|
|
172
|
+
`learnedbf` has been designed and implemented by D. Malchiodi, M. Frasca, N. Rinaldi and R. Giancarlo.
|
|
173
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Configuration file for the Sphinx documentation builder.
|
|
2
|
+
#
|
|
3
|
+
# For the full list of built-in configuration values, see the documentation:
|
|
4
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
|
5
|
+
|
|
6
|
+
# -- Project information -----------------------------------------------------
|
|
7
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
sys.path.insert(0, os.path.abspath('..'))
|
|
12
|
+
|
|
13
|
+
project = 'learnedbf'
|
|
14
|
+
copyright = '2025, SLIM laboratory'
|
|
15
|
+
author = 'SLIM laboratory'
|
|
16
|
+
release = '1.0.0'
|
|
17
|
+
|
|
18
|
+
# -- General configuration ---------------------------------------------------
|
|
19
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
|
20
|
+
|
|
21
|
+
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon',
|
|
22
|
+
'sphinx_autodoc_typehints']
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
templates_path = ['_templates']
|
|
26
|
+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# -- Options for HTML output -------------------------------------------------
|
|
31
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
|
32
|
+
|
|
33
|
+
html_theme = 'sphinx_rtd_theme'
|
|
34
|
+
html_static_path = ['_static']
|
|
@@ -167,10 +167,11 @@ class ClassicalBloomFilterImpl():
|
|
|
167
167
|
'''
|
|
168
168
|
Add an item in the filter
|
|
169
169
|
'''
|
|
170
|
+
item_bytes = item if isinstance(item, bytes) else item.tobytes()
|
|
170
171
|
digests = []
|
|
171
172
|
for i in range(self.k):
|
|
172
173
|
# i works as seed to mmh3.hash() function
|
|
173
|
-
digest = mmh3.hash(
|
|
174
|
+
digest = mmh3.hash(item_bytes, i) % self.m
|
|
174
175
|
digests.append(digest)
|
|
175
176
|
|
|
176
177
|
# set the bit True in bit_array
|
|
@@ -180,8 +181,9 @@ class ClassicalBloomFilterImpl():
|
|
|
180
181
|
'''
|
|
181
182
|
Check for existence of an item in filter
|
|
182
183
|
'''
|
|
184
|
+
item_bytes = item if isinstance(item, bytes) else item.tobytes()
|
|
183
185
|
for i in range(self.k):
|
|
184
|
-
digest = mmh3.hash(
|
|
186
|
+
digest = mmh3.hash(item_bytes, i) % self.m
|
|
185
187
|
if not self.bit_array[digest]:
|
|
186
188
|
# if any of bit is False then,its not present
|
|
187
189
|
# in filter
|
|
@@ -215,7 +217,7 @@ class hashfunc():
|
|
|
215
217
|
self.seed = randint(1, 99999999)
|
|
216
218
|
|
|
217
219
|
def __call__(self, x):
|
|
218
|
-
return mmh3.hash(x, self.seed) % self.m
|
|
220
|
+
return mmh3.hash(x.tobytes(), self.seed) % self.m
|
|
219
221
|
|
|
220
222
|
class VarhashBloomFilter(BaseEstimator, ClassifierMixin, BloomFilter):
|
|
221
223
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from learnedbf.BF import ClassicalBloomFilter
|
|
2
|
+
|
|
3
|
+
class BloomFilter:
|
|
4
|
+
def __init__(self, max_elements: int, error_rate: float):
|
|
5
|
+
self.bloom_filter = ClassicalBloomFilter(epsilon=error_rate,
|
|
6
|
+
n=max_elements)
|
|
7
|
+
|
|
8
|
+
def add(self, item):
|
|
9
|
+
self.bloom_filter.add(item)
|
|
10
|
+
|
|
11
|
+
def __contains__(self, item):
|
|
12
|
+
return self.bloom_filter.check(item)
|
|
13
|
+
|
|
14
|
+
def get_size(self):
|
|
15
|
+
return self.bloom_filter.get_size()
|