tinyconformal 0.1.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.
- tinyconformal-0.1.0/LICENSE +21 -0
- tinyconformal-0.1.0/PKG-INFO +176 -0
- tinyconformal-0.1.0/README.md +150 -0
- tinyconformal-0.1.0/pyproject.toml +23 -0
- tinyconformal-0.1.0/setup.cfg +4 -0
- tinyconformal-0.1.0/tests/test_classifiers.py +144 -0
- tinyconformal-0.1.0/tinyconformal/classifier/__init__.py +7 -0
- tinyconformal-0.1.0/tinyconformal/classifier/base.py +433 -0
- tinyconformal-0.1.0/tinyconformal/classifier/class_conditional.py +222 -0
- tinyconformal-0.1.0/tinyconformal/classifier/marginal.py +204 -0
- tinyconformal-0.1.0/tinyconformal/plot/__init__.py +6 -0
- tinyconformal-0.1.0/tinyconformal/plot/plot.py +411 -0
- tinyconformal-0.1.0/tinyconformal/regressor/__init__.py +8 -0
- tinyconformal-0.1.0/tinyconformal/regressor/base.py +238 -0
- tinyconformal-0.1.0/tinyconformal/regressor/cqr.py +127 -0
- tinyconformal-0.1.0/tinyconformal/regressor/exactness_bound.py +111 -0
- tinyconformal-0.1.0/tinyconformal/regressor/icp.py +99 -0
- tinyconformal-0.1.0/tinyconformal.egg-info/PKG-INFO +176 -0
- tinyconformal-0.1.0/tinyconformal.egg-info/SOURCES.txt +20 -0
- tinyconformal-0.1.0/tinyconformal.egg-info/dependency_links.txt +1 -0
- tinyconformal-0.1.0/tinyconformal.egg-info/requires.txt +16 -0
- tinyconformal-0.1.0/tinyconformal.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lucas Leão
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinyconformal
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small toolbox for conformal prediction
|
|
5
|
+
Author-email: Lucas Leão <heylucasleao@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: machine-learning,conformal-prediction
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: numpy>=1.24.4
|
|
13
|
+
Requires-Dist: venn-abers>=1.4.6
|
|
14
|
+
Requires-Dist: scikit-learn>=1.3.0
|
|
15
|
+
Requires-Dist: quantile-forest>=1.4.2
|
|
16
|
+
Provides-Extra: plot
|
|
17
|
+
Requires-Dist: plotly>=5.22.0; extra == "plot"
|
|
18
|
+
Requires-Dist: kaleido<=0.2.1; extra == "plot"
|
|
19
|
+
Provides-Extra: notebook
|
|
20
|
+
Requires-Dist: nbformat>=5.10.4; extra == "notebook"
|
|
21
|
+
Requires-Dist: ipykernel>=6.7.0; extra == "notebook"
|
|
22
|
+
Provides-Extra: all
|
|
23
|
+
Requires-Dist: plot; extra == "all"
|
|
24
|
+
Requires-Dist: notebook; extra == "all"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# TinyConformal
|
|
28
|
+
TinyConformal is an experimental Python library for conformal predictions, providing tools to generate valid prediction sets with a specified significance level (alpha). This project aims to facilitate the implementation of personal and future projects on the topic.
|
|
29
|
+
|
|
30
|
+
For more information on a previous project related to Out-of-Bag (OOB) solutions, visit [this link](https://github.com/HeyLucasLeao/cp-study).
|
|
31
|
+
|
|
32
|
+
## Recent updates
|
|
33
|
+
- Added support for exactness-bound-based calibration through `ExactnessBound` for ICP and CQR workflows.
|
|
34
|
+
- Added `unlabeled_fit` support for conformal classifiers and regressors, enabling calibration without labeled calibration data when an exactness bound is available.
|
|
35
|
+
- Classifiers can now be calibrated from unlabeled data using pseudo-labels derived from model predictions, while regressors can use a pre-estimated exactness bound to build the conformity scores.
|
|
36
|
+
|
|
37
|
+
Previously, `calibrate` used `Balanced Accuracy Score`; it can now also be calibrated with `Matthews Correlation Coefficient` or `Bookmaker Informedness Score` for improved reliability. The `evaluate` method also reports `bm` and `mcc`.
|
|
38
|
+
|
|
39
|
+
Currently, TinyConformal supports Out-of-Bag (OOB) solutions for `RandomForestClassifier` in binary classification problems, as well as `RandomForestRegressor` and `RandomForestQuantileRegressor` for regression tasks. For additional options and advanced features, you may want to explore [Crepes](https://github.com/henrikbostrom/crepes).
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
Install TinyConformal using pip:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install tinyconformal
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
> **Note:** If you want to enable plotting capabilities, you need to install the extras using Poetry:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
poetry install --E plot
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
### Importing Classifiers
|
|
58
|
+
|
|
59
|
+
Import the conformal classifiers from the `tinyconformal.classifier` module:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from tinyconformal.classifier import BinaryClassConditionalConformalClassifier
|
|
63
|
+
from tinyconformal.classifier import BinaryMarginalConformalClassifier
|
|
64
|
+
```
|
|
65
|
+
### Importing Regressors
|
|
66
|
+
|
|
67
|
+
Import the conformal regressors from the `tinyconformal.regressor` module:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from tinyconformal.regressor import ConformalizedRegressor
|
|
71
|
+
from tinyconformal.regressor import ConformalizedQuantileRegressor
|
|
72
|
+
```
|
|
73
|
+
### Example
|
|
74
|
+
|
|
75
|
+
Example usage of `BinaryClassConditionalConformalClassifier`:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
79
|
+
from tinyconformal.classifier import BinaryClassConditionalConformalClassifier
|
|
80
|
+
|
|
81
|
+
# Create and fit a RandomForestClassifier
|
|
82
|
+
learner = RandomForestClassifier(n_estimators=100, oob_score=True)
|
|
83
|
+
X_train, y_train = ... # your training data
|
|
84
|
+
learner.fit(X_train, y_train)
|
|
85
|
+
|
|
86
|
+
# Create and fit the conformal classifier
|
|
87
|
+
conformal_classifier = BinaryClassConditionalConformalClassifier(learner)
|
|
88
|
+
conformal_classifier.fit(X=X_train, y=y_train, oob=True)
|
|
89
|
+
|
|
90
|
+
# Make predictions
|
|
91
|
+
X_test = ... # your test data
|
|
92
|
+
predictions = conformal_classifier.predict(X_test)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Unlabeled calibration example
|
|
96
|
+
|
|
97
|
+
For settings where labeled calibration data are unavailable, you can fit the conformal model directly on unlabeled data:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
101
|
+
from tinyconformal.classifier import BinaryMarginalConformalClassifier
|
|
102
|
+
|
|
103
|
+
learner = RandomForestClassifier(n_estimators=100, oob_score=True)
|
|
104
|
+
learner.fit(X_train, y_train)
|
|
105
|
+
|
|
106
|
+
conformal_classifier = BinaryMarginalConformalClassifier(learner)
|
|
107
|
+
conformal_classifier.unlabeled_fit(X_unlabeled)
|
|
108
|
+
|
|
109
|
+
predictions = conformal_classifier.predict(X_test)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
For regressors, you can combine an exactness bound estimate with unlabeled calibration:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from sklearn.ensemble import RandomForestRegressor
|
|
116
|
+
from tinyconformal import ConformalizedRegressor, ExactnessBound
|
|
117
|
+
|
|
118
|
+
learner = RandomForestRegressor(random_state=42)
|
|
119
|
+
tilde_beta = ExactnessBound.estimate_icp_bound(learner, X_train, y_train, p=0.95, cv=5)
|
|
120
|
+
|
|
121
|
+
regressor = ConformalizedRegressor(learner, alpha=0.05)
|
|
122
|
+
regressor.unlabeled_fit(X_unlabeled, tilde_beta=tilde_beta)
|
|
123
|
+
|
|
124
|
+
intervals = regressor.predict_interval(X_test)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Evaluating the Classifier
|
|
128
|
+
|
|
129
|
+
Evaluate the performance of the conformal classifier using the `evaluate` method:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
results = conformal_classifier.evaluate(X_test, y_test)
|
|
133
|
+
print(results)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Classes
|
|
137
|
+
|
|
138
|
+
### BinaryMarginalConformalClassifier
|
|
139
|
+
|
|
140
|
+
`BinaryMarginalConformalClassifier` is a marginal-coverage conformal classifier that uses a classifier as the underlying learner.
|
|
141
|
+
|
|
142
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
143
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
144
|
+
- Training via unlabeled calibration: `unlabeled_fit(X)`
|
|
145
|
+
|
|
146
|
+
### BinaryClassConditionalConformalClassifier
|
|
147
|
+
|
|
148
|
+
`BinaryClassConditionalConformalClassifier` is a class-conditional conformal classifier that uses a classifier as the underlying learner.
|
|
149
|
+
|
|
150
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
151
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
152
|
+
- Training via unlabeled calibration: `unlabeled_fit(X)` using pseudo-labels derived from the model probabilities
|
|
153
|
+
|
|
154
|
+
### ConformalizedRegressor
|
|
155
|
+
|
|
156
|
+
`ConformalizedRegressor` is a conformal regressor built on a regression learner.
|
|
157
|
+
|
|
158
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
159
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
160
|
+
- Training via unlabeled calibration: `unlabeled_fit(X, tilde_beta=...)` using an exactness bound
|
|
161
|
+
|
|
162
|
+
### ConformalizedQuantileRegressor
|
|
163
|
+
|
|
164
|
+
`ConformalizedQuantileRegressor` is a conformal quantile regressor built on a quantile regressor.
|
|
165
|
+
|
|
166
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
167
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
168
|
+
- Training via unlabeled calibration: `unlabeled_fit(X, tilde_beta=...)` using an exactness bound
|
|
169
|
+
|
|
170
|
+
### ExactnessBound
|
|
171
|
+
|
|
172
|
+
`ExactnessBound` provides helper methods to estimate the exactness bound used in unlabeled conformal calibration for ICP and CQR workflows.
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# TinyConformal
|
|
2
|
+
TinyConformal is an experimental Python library for conformal predictions, providing tools to generate valid prediction sets with a specified significance level (alpha). This project aims to facilitate the implementation of personal and future projects on the topic.
|
|
3
|
+
|
|
4
|
+
For more information on a previous project related to Out-of-Bag (OOB) solutions, visit [this link](https://github.com/HeyLucasLeao/cp-study).
|
|
5
|
+
|
|
6
|
+
## Recent updates
|
|
7
|
+
- Added support for exactness-bound-based calibration through `ExactnessBound` for ICP and CQR workflows.
|
|
8
|
+
- Added `unlabeled_fit` support for conformal classifiers and regressors, enabling calibration without labeled calibration data when an exactness bound is available.
|
|
9
|
+
- Classifiers can now be calibrated from unlabeled data using pseudo-labels derived from model predictions, while regressors can use a pre-estimated exactness bound to build the conformity scores.
|
|
10
|
+
|
|
11
|
+
Previously, `calibrate` used `Balanced Accuracy Score`; it can now also be calibrated with `Matthews Correlation Coefficient` or `Bookmaker Informedness Score` for improved reliability. The `evaluate` method also reports `bm` and `mcc`.
|
|
12
|
+
|
|
13
|
+
Currently, TinyConformal supports Out-of-Bag (OOB) solutions for `RandomForestClassifier` in binary classification problems, as well as `RandomForestRegressor` and `RandomForestQuantileRegressor` for regression tasks. For additional options and advanced features, you may want to explore [Crepes](https://github.com/henrikbostrom/crepes).
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Install TinyConformal using pip:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install tinyconformal
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
> **Note:** If you want to enable plotting capabilities, you need to install the extras using Poetry:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
poetry install --E plot
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Importing Classifiers
|
|
32
|
+
|
|
33
|
+
Import the conformal classifiers from the `tinyconformal.classifier` module:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from tinyconformal.classifier import BinaryClassConditionalConformalClassifier
|
|
37
|
+
from tinyconformal.classifier import BinaryMarginalConformalClassifier
|
|
38
|
+
```
|
|
39
|
+
### Importing Regressors
|
|
40
|
+
|
|
41
|
+
Import the conformal regressors from the `tinyconformal.regressor` module:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from tinyconformal.regressor import ConformalizedRegressor
|
|
45
|
+
from tinyconformal.regressor import ConformalizedQuantileRegressor
|
|
46
|
+
```
|
|
47
|
+
### Example
|
|
48
|
+
|
|
49
|
+
Example usage of `BinaryClassConditionalConformalClassifier`:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
53
|
+
from tinyconformal.classifier import BinaryClassConditionalConformalClassifier
|
|
54
|
+
|
|
55
|
+
# Create and fit a RandomForestClassifier
|
|
56
|
+
learner = RandomForestClassifier(n_estimators=100, oob_score=True)
|
|
57
|
+
X_train, y_train = ... # your training data
|
|
58
|
+
learner.fit(X_train, y_train)
|
|
59
|
+
|
|
60
|
+
# Create and fit the conformal classifier
|
|
61
|
+
conformal_classifier = BinaryClassConditionalConformalClassifier(learner)
|
|
62
|
+
conformal_classifier.fit(X=X_train, y=y_train, oob=True)
|
|
63
|
+
|
|
64
|
+
# Make predictions
|
|
65
|
+
X_test = ... # your test data
|
|
66
|
+
predictions = conformal_classifier.predict(X_test)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Unlabeled calibration example
|
|
70
|
+
|
|
71
|
+
For settings where labeled calibration data are unavailable, you can fit the conformal model directly on unlabeled data:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
75
|
+
from tinyconformal.classifier import BinaryMarginalConformalClassifier
|
|
76
|
+
|
|
77
|
+
learner = RandomForestClassifier(n_estimators=100, oob_score=True)
|
|
78
|
+
learner.fit(X_train, y_train)
|
|
79
|
+
|
|
80
|
+
conformal_classifier = BinaryMarginalConformalClassifier(learner)
|
|
81
|
+
conformal_classifier.unlabeled_fit(X_unlabeled)
|
|
82
|
+
|
|
83
|
+
predictions = conformal_classifier.predict(X_test)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
For regressors, you can combine an exactness bound estimate with unlabeled calibration:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from sklearn.ensemble import RandomForestRegressor
|
|
90
|
+
from tinyconformal import ConformalizedRegressor, ExactnessBound
|
|
91
|
+
|
|
92
|
+
learner = RandomForestRegressor(random_state=42)
|
|
93
|
+
tilde_beta = ExactnessBound.estimate_icp_bound(learner, X_train, y_train, p=0.95, cv=5)
|
|
94
|
+
|
|
95
|
+
regressor = ConformalizedRegressor(learner, alpha=0.05)
|
|
96
|
+
regressor.unlabeled_fit(X_unlabeled, tilde_beta=tilde_beta)
|
|
97
|
+
|
|
98
|
+
intervals = regressor.predict_interval(X_test)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Evaluating the Classifier
|
|
102
|
+
|
|
103
|
+
Evaluate the performance of the conformal classifier using the `evaluate` method:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
results = conformal_classifier.evaluate(X_test, y_test)
|
|
107
|
+
print(results)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Classes
|
|
111
|
+
|
|
112
|
+
### BinaryMarginalConformalClassifier
|
|
113
|
+
|
|
114
|
+
`BinaryMarginalConformalClassifier` is a marginal-coverage conformal classifier that uses a classifier as the underlying learner.
|
|
115
|
+
|
|
116
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
117
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
118
|
+
- Training via unlabeled calibration: `unlabeled_fit(X)`
|
|
119
|
+
|
|
120
|
+
### BinaryClassConditionalConformalClassifier
|
|
121
|
+
|
|
122
|
+
`BinaryClassConditionalConformalClassifier` is a class-conditional conformal classifier that uses a classifier as the underlying learner.
|
|
123
|
+
|
|
124
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
125
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
126
|
+
- Training via unlabeled calibration: `unlabeled_fit(X)` using pseudo-labels derived from the model probabilities
|
|
127
|
+
|
|
128
|
+
### ConformalizedRegressor
|
|
129
|
+
|
|
130
|
+
`ConformalizedRegressor` is a conformal regressor built on a regression learner.
|
|
131
|
+
|
|
132
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
133
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
134
|
+
- Training via unlabeled calibration: `unlabeled_fit(X, tilde_beta=...)` using an exactness bound
|
|
135
|
+
|
|
136
|
+
### ConformalizedQuantileRegressor
|
|
137
|
+
|
|
138
|
+
`ConformalizedQuantileRegressor` is a conformal quantile regressor built on a quantile regressor.
|
|
139
|
+
|
|
140
|
+
- Training via labeled calibration: `fit(X, y)`
|
|
141
|
+
- Training via OOB calibration: `fit(X, y, oob=True)`
|
|
142
|
+
- Training via unlabeled calibration: `unlabeled_fit(X, tilde_beta=...)` using an exactness bound
|
|
143
|
+
|
|
144
|
+
### ExactnessBound
|
|
145
|
+
|
|
146
|
+
`ExactnessBound` provides helper methods to estimate the exactness bound used in unlabeled conformal calibration for ICP and CQR workflows.
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tinyconformal"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A small toolbox for conformal prediction"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
authors = [{name = "Lucas Leão", email = "heylucasleao@gmail.com"}]
|
|
7
|
+
keywords = ["machine-learning", "conformal-prediction"]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Programming Language :: Python :: 3",
|
|
11
|
+
]
|
|
12
|
+
dependencies = [
|
|
13
|
+
"numpy (>=1.24.4)",
|
|
14
|
+
"venn-abers (>=1.4.6)",
|
|
15
|
+
"scikit-learn (>=1.3.0)",
|
|
16
|
+
"quantile-forest>=1.4.2",
|
|
17
|
+
]
|
|
18
|
+
requires-python = ">=3.10"
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
plot = ["plotly (>=5.22.0)", "kaleido (<=0.2.1)"]
|
|
22
|
+
notebook = ["nbformat (>=5.10.4)", "ipykernel (>=6.7.0)"]
|
|
23
|
+
all = ["plot", "notebook"]
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# Copyright (c) 2024-2026 Lucas Leão
|
|
2
|
+
# tinyCP - A small toolbox for conformal prediction
|
|
3
|
+
# Licensed under the MIT License
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import unittest
|
|
7
|
+
import numpy as np
|
|
8
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
9
|
+
from sklearn.model_selection import train_test_split
|
|
10
|
+
from sklearn.datasets import make_classification
|
|
11
|
+
from tinyconformal.classifier.marginal import BinaryMarginalConformalClassifier
|
|
12
|
+
from tinyconformal.classifier.class_conditional import (
|
|
13
|
+
BinaryClassConditionalConformalClassifier,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestClassifiers(unittest.TestCase):
|
|
18
|
+
def setUp(self):
|
|
19
|
+
weights = [0.4, 0.6]
|
|
20
|
+
seed = 42
|
|
21
|
+
|
|
22
|
+
X, y = make_classification(
|
|
23
|
+
n_samples=1500,
|
|
24
|
+
n_features=20,
|
|
25
|
+
n_informative=2,
|
|
26
|
+
weights=weights,
|
|
27
|
+
random_state=seed,
|
|
28
|
+
n_redundant=2,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
32
|
+
X, y, test_size=0.2, random_state=seed, stratify=y
|
|
33
|
+
)
|
|
34
|
+
X_train, X_calib, y_train, y_calib = train_test_split(
|
|
35
|
+
X_train, y_train, test_size=0.25, random_state=seed, stratify=y_train
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
self.X_train = X_train
|
|
39
|
+
self.y_train = y_train
|
|
40
|
+
|
|
41
|
+
self.X_calib = X_calib
|
|
42
|
+
self.y_calib = y_calib
|
|
43
|
+
|
|
44
|
+
self.X_test = X_test
|
|
45
|
+
self.y_test = y_test
|
|
46
|
+
|
|
47
|
+
self.learner = RandomForestClassifier(oob_score=True, n_estimators=10)
|
|
48
|
+
self.learner.fit(self.X_train, self.y_train)
|
|
49
|
+
|
|
50
|
+
def test_marginal_classifier(self):
|
|
51
|
+
classifier = BinaryMarginalConformalClassifier(self.learner)
|
|
52
|
+
classifier.fit(self.X_calib, self.y_calib, oob=False)
|
|
53
|
+
|
|
54
|
+
classifier.calibrate(self.X_calib, self.y_calib)
|
|
55
|
+
self.assertTrue(0 < classifier.alpha <= 0.2)
|
|
56
|
+
|
|
57
|
+
y_proba = classifier.predict_proba(self.X_test)
|
|
58
|
+
self.assertEqual(y_proba.shape, (self.X_test.shape[0], 2))
|
|
59
|
+
|
|
60
|
+
prediction_set = classifier.predict_set(self.X_test)
|
|
61
|
+
self.assertEqual(prediction_set.shape, (self.X_test.shape[0], 2))
|
|
62
|
+
|
|
63
|
+
p_values = classifier.predict_p(self.X_test)
|
|
64
|
+
self.assertEqual(p_values.shape, (self.X_test.shape[0], 2))
|
|
65
|
+
|
|
66
|
+
y_pred = classifier.predict(self.X_test)
|
|
67
|
+
self.assertEqual(y_pred.shape, (self.X_test.shape[0],))
|
|
68
|
+
|
|
69
|
+
eval_dict = classifier.evaluate(self.X_test, self.y_test)
|
|
70
|
+
self.assertTrue(isinstance(eval_dict, dict))
|
|
71
|
+
self.assertEqual(len(eval_dict.keys()), 13)
|
|
72
|
+
|
|
73
|
+
def test_class_cond_classifier(self):
|
|
74
|
+
classifier = BinaryClassConditionalConformalClassifier(self.learner)
|
|
75
|
+
classifier.fit(self.X_calib, self.y_calib, oob=False)
|
|
76
|
+
|
|
77
|
+
classifier.calibrate(self.X_calib, self.y_calib)
|
|
78
|
+
self.assertTrue(0 < classifier.alpha <= 0.2)
|
|
79
|
+
|
|
80
|
+
y_proba = classifier.predict_proba(self.X_test)
|
|
81
|
+
self.assertEqual(y_proba.shape, (self.X_test.shape[0], 2))
|
|
82
|
+
|
|
83
|
+
prediction_set = classifier.predict_set(self.X_test)
|
|
84
|
+
self.assertEqual(prediction_set.shape, (self.X_test.shape[0], 2))
|
|
85
|
+
|
|
86
|
+
p_values = classifier.predict_p(self.X_test)
|
|
87
|
+
self.assertEqual(p_values.shape, (self.X_test.shape[0], 2))
|
|
88
|
+
|
|
89
|
+
y_pred = classifier.predict(self.X_test)
|
|
90
|
+
self.assertEqual(y_pred.shape, (self.X_test.shape[0],))
|
|
91
|
+
|
|
92
|
+
eval_dict = classifier.evaluate(self.X_test, self.y_test)
|
|
93
|
+
self.assertTrue(isinstance(eval_dict, dict))
|
|
94
|
+
self.assertEqual(len(eval_dict.keys()), 13)
|
|
95
|
+
|
|
96
|
+
def test_oob_marginal_classifier(self):
|
|
97
|
+
classifier = BinaryMarginalConformalClassifier(self.learner)
|
|
98
|
+
classifier.fit(y=self.y_train, oob=True)
|
|
99
|
+
|
|
100
|
+
classifier.calibrate(self.X_calib, self.y_calib)
|
|
101
|
+
self.assertTrue(0 < classifier.alpha <= 0.2)
|
|
102
|
+
|
|
103
|
+
y_proba = classifier.predict_proba(self.X_test)
|
|
104
|
+
self.assertEqual(y_proba.shape, (self.X_test.shape[0], 2))
|
|
105
|
+
|
|
106
|
+
prediction_set = classifier.predict_set(self.X_test)
|
|
107
|
+
self.assertEqual(prediction_set.shape, (self.X_test.shape[0], 2))
|
|
108
|
+
|
|
109
|
+
p_values = classifier.predict_p(self.X_test)
|
|
110
|
+
self.assertEqual(p_values.shape, (self.X_test.shape[0], 2))
|
|
111
|
+
|
|
112
|
+
y_pred = classifier.predict(self.X_test)
|
|
113
|
+
self.assertEqual(y_pred.shape, (self.X_test.shape[0],))
|
|
114
|
+
|
|
115
|
+
eval_dict = classifier.evaluate(self.X_test, self.y_test)
|
|
116
|
+
self.assertTrue(isinstance(eval_dict, dict))
|
|
117
|
+
self.assertEqual(len(eval_dict.keys()), 13)
|
|
118
|
+
|
|
119
|
+
def test_oob_class_conditional_classifier(self):
|
|
120
|
+
classifier = BinaryClassConditionalConformalClassifier(self.learner)
|
|
121
|
+
classifier.fit(y=self.y_train, oob=True)
|
|
122
|
+
|
|
123
|
+
classifier.calibrate(self.X_calib, self.y_calib)
|
|
124
|
+
self.assertTrue(0 < classifier.alpha <= 0.2)
|
|
125
|
+
|
|
126
|
+
y_proba = classifier.predict_proba(self.X_test)
|
|
127
|
+
self.assertEqual(y_proba.shape, (self.X_test.shape[0], 2))
|
|
128
|
+
|
|
129
|
+
prediction_set = classifier.predict_set(self.X_test)
|
|
130
|
+
self.assertEqual(prediction_set.shape, (self.X_test.shape[0], 2))
|
|
131
|
+
|
|
132
|
+
p_values = classifier.predict_p(self.X_test)
|
|
133
|
+
self.assertEqual(p_values.shape, (self.X_test.shape[0], 2))
|
|
134
|
+
|
|
135
|
+
y_pred = classifier.predict(self.X_test)
|
|
136
|
+
self.assertEqual(y_pred.shape, (self.X_test.shape[0],))
|
|
137
|
+
|
|
138
|
+
eval_dict = classifier.evaluate(self.X_test, self.y_test)
|
|
139
|
+
self.assertTrue(isinstance(eval_dict, dict))
|
|
140
|
+
self.assertEqual(len(eval_dict.keys()), 13)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if __name__ == "__main__":
|
|
144
|
+
unittest.main()
|