ProbabilisticNN 0.0.1__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.
- probabilisticnn/__init__.py +0 -0
- probabilisticnn/base/__init__.py +35 -0
- probabilisticnn/base/kernels.py +370 -0
- probabilisticnn/base/loss.py +444 -0
- probabilisticnn/base/optim.py +325 -0
- probabilisticnn/base/types.py +15 -0
- probabilisticnn/base/utils.py +103 -0
- probabilisticnn/common/__init__.py +8 -0
- probabilisticnn/common/pattern_layer.py +215 -0
- probabilisticnn/grnn/__init__.py +8 -0
- probabilisticnn/grnn/grnn.py +207 -0
- probabilisticnn/grnn/layers.py +13 -0
- probabilisticnn/numba_backend/__init__.py +9 -0
- probabilisticnn/numba_backend/inference/__init__.py +7 -0
- probabilisticnn/numba_backend/inference/grnn.py +29 -0
- probabilisticnn/numba_backend/inference/pnn.py +34 -0
- probabilisticnn/numba_backend/kernels.py +458 -0
- probabilisticnn/pnn/__init__.py +8 -0
- probabilisticnn/pnn/layers.py +83 -0
- probabilisticnn/pnn/pnn.py +239 -0
- probabilisticnn-0.0.1.dist-info/METADATA +344 -0
- probabilisticnn-0.0.1.dist-info/RECORD +25 -0
- probabilisticnn-0.0.1.dist-info/WHEEL +5 -0
- probabilisticnn-0.0.1.dist-info/licenses/LICENCE +19 -0
- probabilisticnn-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
from sklearn.base import BaseEstimator, ClassifierMixin
|
|
2
|
+
from sklearn.utils.validation import validate_data, check_is_fitted
|
|
3
|
+
from sklearn.utils.multiclass import unique_labels
|
|
4
|
+
|
|
5
|
+
from probabilisticnn.common.pattern_layer import PatternLayer, AdaptivePatternLayer
|
|
6
|
+
from probabilisticnn.pnn.layers import OutputLayer, SummationLayer
|
|
7
|
+
from probabilisticnn.base.optim import BandwidthOptimizer
|
|
8
|
+
from probabilisticnn.base.utils import normalize_l2
|
|
9
|
+
from probabilisticnn.base.utils import cast_to_dtype
|
|
10
|
+
from probabilisticnn.base.utils import validate_backend
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PNN(ClassifierMixin, BaseEstimator):
|
|
14
|
+
"""Classic Probabilistic Neural Network classifier with a fixed scalar bandwidth."""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
bandwidth=0.5,
|
|
19
|
+
kernel="gaussian",
|
|
20
|
+
losses="uniform",
|
|
21
|
+
normalize=True,
|
|
22
|
+
backend="numpy",
|
|
23
|
+
compute_dtype="auto",
|
|
24
|
+
):
|
|
25
|
+
self.bandwidth = bandwidth
|
|
26
|
+
self.kernel = kernel
|
|
27
|
+
self.losses = losses
|
|
28
|
+
self.normalize = normalize
|
|
29
|
+
self.backend = backend
|
|
30
|
+
self.compute_dtype = compute_dtype
|
|
31
|
+
|
|
32
|
+
def fit(self, X, y):
|
|
33
|
+
"""Store training patterns and fit all PNN layers."""
|
|
34
|
+
validate_backend(self.backend)
|
|
35
|
+
X, y = validate_data(self, X, y)
|
|
36
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
37
|
+
self.bandwidth_ = cast_to_dtype(self.bandwidth, self.compute_dtype)
|
|
38
|
+
self.classes_ = unique_labels(y)
|
|
39
|
+
self.y_ = y
|
|
40
|
+
self.pattern_layer_ = PatternLayer(
|
|
41
|
+
bandwidth=self.bandwidth_,
|
|
42
|
+
kernel=self.kernel,
|
|
43
|
+
normalize=self.normalize,
|
|
44
|
+
backend=self.backend
|
|
45
|
+
).fit(X)
|
|
46
|
+
|
|
47
|
+
self.summation_layer_ = SummationLayer().fit(X, y)
|
|
48
|
+
self.output_layer_ = OutputLayer(self.losses, self.compute_dtype).fit(y)
|
|
49
|
+
|
|
50
|
+
return self
|
|
51
|
+
|
|
52
|
+
def predict(self, X):
|
|
53
|
+
"""Predict class labels for input samples."""
|
|
54
|
+
check_is_fitted(
|
|
55
|
+
self,
|
|
56
|
+
["classes_", "y_", "pattern_layer_", "summation_layer_", "output_layer_"],
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
X = validate_data(self, X, reset=False)
|
|
60
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
61
|
+
|
|
62
|
+
if self.backend == "numba":
|
|
63
|
+
from probabilisticnn.numba_backend import pnn_jit_inference
|
|
64
|
+
|
|
65
|
+
X_transformed = normalize_l2(X) if self.normalize else X
|
|
66
|
+
y_pred_encoded = pnn_jit_inference(
|
|
67
|
+
kernel=self.kernel,
|
|
68
|
+
X=X_transformed,
|
|
69
|
+
W=self.pattern_layer_.patterns_,
|
|
70
|
+
y_encoded=self.summation_layer_.y_encoded_,
|
|
71
|
+
n_classes=self.summation_layer_.n_classes_,
|
|
72
|
+
likelihood_multiplier=self.output_layer_.likelihood_multiplier_,
|
|
73
|
+
bandwidth=self.pattern_layer_.bandwidth_,
|
|
74
|
+
bandwidth_sharing="scalar",
|
|
75
|
+
normalized=self.normalize,
|
|
76
|
+
)
|
|
77
|
+
return self.output_layer_.classes_[y_pred_encoded]
|
|
78
|
+
|
|
79
|
+
K = self.pattern_layer_.transform(X)
|
|
80
|
+
f = self.summation_layer_.transform(K)
|
|
81
|
+
return self.output_layer_.transform(f)
|
|
82
|
+
|
|
83
|
+
def predict_proba(self, X):
|
|
84
|
+
"""Predict posterior class probabilities for input samples."""
|
|
85
|
+
check_is_fitted(
|
|
86
|
+
self,
|
|
87
|
+
["classes_", "y_", "pattern_layer_", "summation_layer_", "output_layer_"],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
X = validate_data(self, X, reset=False)
|
|
91
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
92
|
+
|
|
93
|
+
K = self.pattern_layer_.transform(X)
|
|
94
|
+
f = self.summation_layer_.transform(K)
|
|
95
|
+
posteriori = self.output_layer_.posteriori(f)
|
|
96
|
+
return posteriori
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class AdaptivePNN(PNN):
|
|
100
|
+
"""PNN with bandwidth parameters optimized via LOO loss minimization.
|
|
101
|
+
|
|
102
|
+
Bandwidth can be shared per feature, per class, or per class per feature.
|
|
103
|
+
"""
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
kernel="gaussian",
|
|
107
|
+
losses="uniform",
|
|
108
|
+
loss="correct_class_probability",
|
|
109
|
+
bandwidth_sharing="per_feature",
|
|
110
|
+
max_iter=100,
|
|
111
|
+
solver="auto",
|
|
112
|
+
solver_options=None,
|
|
113
|
+
normalize=True,
|
|
114
|
+
backend="numpy",
|
|
115
|
+
compute_dtype="auto",
|
|
116
|
+
):
|
|
117
|
+
super().__init__(
|
|
118
|
+
bandwidth=0,
|
|
119
|
+
kernel=kernel,
|
|
120
|
+
losses=losses,
|
|
121
|
+
normalize=normalize,
|
|
122
|
+
backend=backend,
|
|
123
|
+
compute_dtype=compute_dtype,
|
|
124
|
+
)
|
|
125
|
+
self.loss = loss
|
|
126
|
+
self.bandwidth_sharing = bandwidth_sharing
|
|
127
|
+
self.max_iter = max_iter
|
|
128
|
+
self.normalize = normalize
|
|
129
|
+
self.solver = solver
|
|
130
|
+
self.solver_options = solver_options
|
|
131
|
+
self.backend = backend
|
|
132
|
+
self.compute_dtype = compute_dtype
|
|
133
|
+
|
|
134
|
+
def fit(self, X, y):
|
|
135
|
+
"""Fit AdaptivePNN layers and optimize bandwidth parameters."""
|
|
136
|
+
validate_backend(self.backend)
|
|
137
|
+
X, y = validate_data(self, X, y)
|
|
138
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
139
|
+
self.classes_ = unique_labels(y)
|
|
140
|
+
self.y_ = y
|
|
141
|
+
|
|
142
|
+
self.pattern_layer_ = AdaptivePatternLayer(
|
|
143
|
+
kernel=self.kernel,
|
|
144
|
+
bandwidth_sharing=self.bandwidth_sharing,
|
|
145
|
+
normalize=self.normalize,
|
|
146
|
+
backend=self.backend,
|
|
147
|
+
compute_dtype=self.compute_dtype,
|
|
148
|
+
).fit(X, y)
|
|
149
|
+
self.summation_layer_ = SummationLayer().fit(X, y)
|
|
150
|
+
self.output_layer_ = OutputLayer(self.losses, self.compute_dtype).fit(y)
|
|
151
|
+
|
|
152
|
+
self.optimizer_ = BandwidthOptimizer(
|
|
153
|
+
model=self,
|
|
154
|
+
loss=self.loss,
|
|
155
|
+
max_iter=self.max_iter,
|
|
156
|
+
solver=self.solver,
|
|
157
|
+
solver_options=self.solver_options,
|
|
158
|
+
).optimize()
|
|
159
|
+
self.bandwidth_ = self.optimizer_.bandwidth_
|
|
160
|
+
self.n_iter_ = self.optimizer_.n_iter_
|
|
161
|
+
self.converged_ = self.optimizer_.converged_
|
|
162
|
+
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
def _forward_train(self, bandwidth=None, return_proba=False, return_encoded=False):
|
|
166
|
+
"""Run Leave-One-Out forward pass on the training set."""
|
|
167
|
+
if return_proba and return_encoded:
|
|
168
|
+
raise ValueError("`return_proba` and `return_encoded` cannot both be True.")
|
|
169
|
+
|
|
170
|
+
K_loo = self.pattern_layer_._loo(bandwidth)
|
|
171
|
+
f = self.summation_layer_.transform(K_loo)
|
|
172
|
+
if return_proba:
|
|
173
|
+
out = self.output_layer_.posteriori(f)
|
|
174
|
+
elif return_encoded:
|
|
175
|
+
out = self.output_layer_.transform_encoded(f)
|
|
176
|
+
else:
|
|
177
|
+
out = self.output_layer_.transform(f)
|
|
178
|
+
|
|
179
|
+
return out
|
|
180
|
+
|
|
181
|
+
def predict_proba(self, X):
|
|
182
|
+
"""Predict posterior class probabilities with optimized bandwidths."""
|
|
183
|
+
check_is_fitted(
|
|
184
|
+
self,
|
|
185
|
+
[
|
|
186
|
+
"classes_",
|
|
187
|
+
"y_",
|
|
188
|
+
"pattern_layer_",
|
|
189
|
+
"summation_layer_",
|
|
190
|
+
"output_layer_",
|
|
191
|
+
],
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
X = validate_data(self, X, reset=False)
|
|
195
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
196
|
+
|
|
197
|
+
K = self.pattern_layer_.transform(X)
|
|
198
|
+
f = self.summation_layer_.transform(K)
|
|
199
|
+
posteriori = self.output_layer_.posteriori(f)
|
|
200
|
+
return posteriori
|
|
201
|
+
|
|
202
|
+
def predict(self, X):
|
|
203
|
+
"""Predict class labels with optimized bandwidths."""
|
|
204
|
+
check_is_fitted(
|
|
205
|
+
self,
|
|
206
|
+
[
|
|
207
|
+
"classes_",
|
|
208
|
+
"y_",
|
|
209
|
+
"pattern_layer_",
|
|
210
|
+
"summation_layer_",
|
|
211
|
+
"output_layer_",
|
|
212
|
+
"optimizer_",
|
|
213
|
+
],
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
X = validate_data(self, X, reset=False)
|
|
217
|
+
X = cast_to_dtype(X, self.compute_dtype)
|
|
218
|
+
|
|
219
|
+
if self.backend == "numba":
|
|
220
|
+
from probabilisticnn.numba_backend import pnn_jit_inference
|
|
221
|
+
|
|
222
|
+
X_transformed = normalize_l2(X) if self.normalize else X
|
|
223
|
+
bandwidth = self.pattern_layer_._prepare_bandwidth(self.pattern_layer_.bandwidth_)
|
|
224
|
+
y_pred_encoded = pnn_jit_inference(
|
|
225
|
+
kernel=self.kernel,
|
|
226
|
+
X=X_transformed,
|
|
227
|
+
W=self.pattern_layer_.patterns_,
|
|
228
|
+
y_encoded=self.summation_layer_.y_encoded_,
|
|
229
|
+
n_classes=self.summation_layer_.n_classes_,
|
|
230
|
+
likelihood_multiplier=self.output_layer_.likelihood_multiplier_,
|
|
231
|
+
bandwidth=bandwidth,
|
|
232
|
+
bandwidth_sharing=self.pattern_layer_.bandwidth_sharing,
|
|
233
|
+
normalized=self.normalize,
|
|
234
|
+
)
|
|
235
|
+
return self.output_layer_.classes_[y_pred_encoded]
|
|
236
|
+
|
|
237
|
+
K = self.pattern_layer_.transform(X)
|
|
238
|
+
f = self.summation_layer_.transform(K)
|
|
239
|
+
return self.output_layer_.transform(f)
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ProbabilisticNN
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python package for Probabilistic Neural Networks and General Regression Neural Networks
|
|
5
|
+
Author-email: Grigorii Vinogradov <vinogradov.gr.s@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: PNN,GRNN,Probabilistic Neural Network,General Regression Neural Network,Machine Learning,Adaptive PNN,Adaptive GRNN,Kernels
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENCE
|
|
11
|
+
Requires-Dist: numpy>=1.26.0; python_version == "3.12"
|
|
12
|
+
Requires-Dist: numpy>=2.1.0; python_version == "3.13"
|
|
13
|
+
Requires-Dist: numpy>=2.3.3; python_version >= "3.14"
|
|
14
|
+
Requires-Dist: scipy>=1.11.2; python_version == "3.12"
|
|
15
|
+
Requires-Dist: scipy>=1.14.1; python_version == "3.13"
|
|
16
|
+
Requires-Dist: scipy>=1.17.0; python_version >= "3.14"
|
|
17
|
+
Requires-Dist: scikit-learn>=1.6.1; python_version < "3.14"
|
|
18
|
+
Requires-Dist: scikit-learn>=1.8.0; python_version >= "3.14"
|
|
19
|
+
Provides-Extra: numba
|
|
20
|
+
Requires-Dist: numba>=0.59.0; python_version == "3.12" and extra == "numba"
|
|
21
|
+
Requires-Dist: numba>=0.61.0; python_version == "3.13" and extra == "numba"
|
|
22
|
+
Requires-Dist: numba>=0.63.0; python_version >= "3.14" and extra == "numba"
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=6; extra == "dev"
|
|
25
|
+
Requires-Dist: numba>=0.59.0; python_version == "3.12" and extra == "dev"
|
|
26
|
+
Requires-Dist: numba>=0.61.0; python_version == "3.13" and extra == "dev"
|
|
27
|
+
Requires-Dist: numba>=0.63.0; python_version >= "3.14" and extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# ProbabilisticNN
|
|
31
|
+
|
|
32
|
+
`ProbabilisticNN` is a Python library for Probabilistic Neural Networks and General Regression Neural Networks with an sklearn-like interface.
|
|
33
|
+
|
|
34
|
+
At the current stage, the library is published on TestPyPI and supports:
|
|
35
|
+
|
|
36
|
+
- `PNN` for classification;
|
|
37
|
+
- `AdaptivePNN` with kernel bandwidth parameter tuning;
|
|
38
|
+
- `GRNN` for regression;
|
|
39
|
+
- `AdaptiveGRNN` with kernel bandwidth optimization;
|
|
40
|
+
- execution using the standard `numpy` implementation;
|
|
41
|
+
- accelerated inference via `numba`.
|
|
42
|
+
|
|
43
|
+
## Main Features
|
|
44
|
+
|
|
45
|
+
- sklearn-like interface: `fit`, `predict`, `predict_proba`;
|
|
46
|
+
- several kernel functions:
|
|
47
|
+
- `gaussian`;
|
|
48
|
+
- `laplacian`;
|
|
49
|
+
- `exponential`;
|
|
50
|
+
- several kernel bandwidth parameterization schemes for `AdaptivePNN`:
|
|
51
|
+
- `per_feature`;
|
|
52
|
+
- `per_class`;
|
|
53
|
+
- `per_class_per_feature`;
|
|
54
|
+
- accelerated `numba` backend for the computationally heavy part of inference;
|
|
55
|
+
- testing via `pytest` and `tox`.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
- Python `3.12+`
|
|
60
|
+
- `numpy`
|
|
61
|
+
- `scipy`
|
|
62
|
+
- `scikit-learn`
|
|
63
|
+
|
|
64
|
+
For the accelerated backend, `numba` is additionally required.
|
|
65
|
+
|
|
66
|
+
## Installation
|
|
67
|
+
|
|
68
|
+
### Installation from TestPyPI
|
|
69
|
+
|
|
70
|
+
For regular usage without `numba`:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
python -m pip install \
|
|
74
|
+
--index-url https://test.pypi.org/simple/ \
|
|
75
|
+
--extra-index-url https://pypi.org/simple/ \
|
|
76
|
+
"ProbabilisticNN"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For installation with the `numba` backend:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
python -m pip install \
|
|
83
|
+
--index-url https://test.pypi.org/simple/ \
|
|
84
|
+
--extra-index-url https://pypi.org/simple/ \
|
|
85
|
+
"ProbabilisticNN[numba]"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For development and testing:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
python -m pip install \
|
|
92
|
+
--index-url https://test.pypi.org/simple/ \
|
|
93
|
+
--extra-index-url https://pypi.org/simple/ \
|
|
94
|
+
"ProbabilisticNN[dev]"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Local Installation from the Repository
|
|
98
|
+
|
|
99
|
+
This requires `setuptools`.
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
python -m build
|
|
103
|
+
python -m pip install -e ".[dev]"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Quick Start
|
|
107
|
+
|
|
108
|
+
### Classification with `PNN`
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import numpy as np
|
|
112
|
+
|
|
113
|
+
from probabilisticnn.pnn import PNN
|
|
114
|
+
|
|
115
|
+
X_train = np.array([
|
|
116
|
+
[0.0, 0.0],
|
|
117
|
+
[0.0, 0.2],
|
|
118
|
+
[1.0, 1.0],
|
|
119
|
+
[1.0, 1.2],
|
|
120
|
+
])
|
|
121
|
+
y_train = np.array(["class_a", "class_a", "class_b", "class_b"])
|
|
122
|
+
|
|
123
|
+
X_test = np.array([
|
|
124
|
+
[0.0, 0.1],
|
|
125
|
+
[1.0, 1.1],
|
|
126
|
+
])
|
|
127
|
+
|
|
128
|
+
model = PNN(
|
|
129
|
+
bandwidth=0.25,
|
|
130
|
+
kernel="gaussian",
|
|
131
|
+
normalize=False,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
model.fit(X_train, y_train)
|
|
135
|
+
|
|
136
|
+
labels = model.predict(X_test)
|
|
137
|
+
proba = model.predict_proba(X_test)
|
|
138
|
+
|
|
139
|
+
print(labels)
|
|
140
|
+
print(proba)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Classification with `AdaptivePNN`
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
import numpy as np
|
|
147
|
+
|
|
148
|
+
from probabilisticnn.pnn import AdaptivePNN
|
|
149
|
+
|
|
150
|
+
X_train = np.array([
|
|
151
|
+
[0.0, 0.0],
|
|
152
|
+
[0.0, 0.2],
|
|
153
|
+
[1.0, 1.0],
|
|
154
|
+
[1.0, 1.2],
|
|
155
|
+
])
|
|
156
|
+
y_train = np.array([0, 0, 1, 1])
|
|
157
|
+
|
|
158
|
+
model = AdaptivePNN(
|
|
159
|
+
kernel="gaussian",
|
|
160
|
+
bandwidth_sharing="per_feature",
|
|
161
|
+
loss="correct_class_probability",
|
|
162
|
+
solver="auto",
|
|
163
|
+
normalize=False,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
model.fit(X_train, y_train)
|
|
167
|
+
|
|
168
|
+
print(model.predict(X_train))
|
|
169
|
+
print(model.predict_proba(X_train))
|
|
170
|
+
print(model.bandwidth_)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Regression with `GRNN`
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
import numpy as np
|
|
177
|
+
|
|
178
|
+
from probabilisticnn.grnn import GRNN
|
|
179
|
+
|
|
180
|
+
X_train = np.array([
|
|
181
|
+
[0.0, 0.0],
|
|
182
|
+
[0.0, 0.2],
|
|
183
|
+
[1.0, 1.0],
|
|
184
|
+
[1.0, 1.2],
|
|
185
|
+
])
|
|
186
|
+
y_train = np.array([1.0, 1.1, 3.0, 3.1])
|
|
187
|
+
|
|
188
|
+
X_test = np.array([
|
|
189
|
+
[0.0, 0.1],
|
|
190
|
+
[1.0, 1.1],
|
|
191
|
+
])
|
|
192
|
+
|
|
193
|
+
model = GRNN(
|
|
194
|
+
bandwidth=0.5,
|
|
195
|
+
kernel="gaussian",
|
|
196
|
+
normalize=False,
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
model.fit(X_train, y_train)
|
|
200
|
+
pred = model.predict(X_test)
|
|
201
|
+
|
|
202
|
+
print(pred)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Regression with `AdaptiveGRNN`
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import numpy as np
|
|
209
|
+
|
|
210
|
+
from probabilisticnn.grnn import AdaptiveGRNN
|
|
211
|
+
|
|
212
|
+
X_train = np.array([
|
|
213
|
+
[0.0, 0.0],
|
|
214
|
+
[0.0, 0.2],
|
|
215
|
+
[1.0, 1.0],
|
|
216
|
+
[1.0, 1.2],
|
|
217
|
+
])
|
|
218
|
+
y_train = np.array([1.0, 1.1, 3.0, 3.1])
|
|
219
|
+
|
|
220
|
+
model = AdaptiveGRNN(
|
|
221
|
+
kernel="gaussian",
|
|
222
|
+
loss="mse",
|
|
223
|
+
solver="auto",
|
|
224
|
+
normalize=False,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
model.fit(X_train, y_train)
|
|
228
|
+
|
|
229
|
+
print(model.predict(X_train))
|
|
230
|
+
print(model.bandwidth_)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Acceleration with `numba`
|
|
234
|
+
|
|
235
|
+
For `PNN`, `AdaptivePNN`, `GRNN`, and `AdaptiveGRNN`, you can choose:
|
|
236
|
+
|
|
237
|
+
- `backend="numpy"` — the main option;
|
|
238
|
+
- `backend="numba"` — the accelerated option.
|
|
239
|
+
|
|
240
|
+
Example:
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
from probabilisticnn.pnn import PNN
|
|
244
|
+
|
|
245
|
+
model = PNN(
|
|
246
|
+
bandwidth=0.25,
|
|
247
|
+
kernel="gaussian",
|
|
248
|
+
backend="numba",
|
|
249
|
+
normalize=False,
|
|
250
|
+
)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Important:
|
|
254
|
+
|
|
255
|
+
- the first `predict` call for the `numba` branch may be slower because of JIT compilation;
|
|
256
|
+
- the main speed gain is achieved during the computation of kernel function values;
|
|
257
|
+
- if `numba` is not installed, the `numba` backend will be unavailable.
|
|
258
|
+
|
|
259
|
+
## Public Interface
|
|
260
|
+
|
|
261
|
+
Models should be imported like this:
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
from probabilisticnn.pnn import PNN, AdaptivePNN
|
|
265
|
+
from probabilisticnn.grnn import GRNN, AdaptiveGRNN
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Model Parameters
|
|
269
|
+
|
|
270
|
+
### `PNN`
|
|
271
|
+
|
|
272
|
+
Main parameters:
|
|
273
|
+
|
|
274
|
+
- `bandwidth` — fixed kernel bandwidth;
|
|
275
|
+
- `kernel` — kernel type;
|
|
276
|
+
- `losses` — class weighting scheme / misclassification cost for classes;
|
|
277
|
+
- `normalize` — whether to use L2 normalization of inputs;
|
|
278
|
+
- `backend` — `numpy` or `numba`;
|
|
279
|
+
- `compute_dtype` — `auto`, `float32`, `float64`.
|
|
280
|
+
|
|
281
|
+
### `AdaptivePNN`
|
|
282
|
+
|
|
283
|
+
Additionally supports:
|
|
284
|
+
|
|
285
|
+
- `loss` — optimization objective function;
|
|
286
|
+
- `bandwidth_sharing` — method for defining bandwidth parameters;
|
|
287
|
+
- `max_iter` — maximum number of optimization steps;
|
|
288
|
+
- `solver` — optimization method;
|
|
289
|
+
- `solver_options` — additional optimizer parameters.
|
|
290
|
+
|
|
291
|
+
### `GRNN`
|
|
292
|
+
|
|
293
|
+
Main parameters:
|
|
294
|
+
|
|
295
|
+
- `bandwidth`;
|
|
296
|
+
- `kernel`;
|
|
297
|
+
- `backend`;
|
|
298
|
+
- `compute_dtype`;
|
|
299
|
+
- `normalize`.
|
|
300
|
+
|
|
301
|
+
### `AdaptiveGRNN`
|
|
302
|
+
|
|
303
|
+
Additionally supports:
|
|
304
|
+
|
|
305
|
+
- `loss` — loss function for regression;
|
|
306
|
+
- `max_iter`;
|
|
307
|
+
- `solver`;
|
|
308
|
+
- `solver_options`;
|
|
309
|
+
- `normalize`.
|
|
310
|
+
|
|
311
|
+
`AdaptiveGRNN` optimizes the kernel bandwidth using the `per_feature` scheme.
|
|
312
|
+
|
|
313
|
+
## Testing
|
|
314
|
+
|
|
315
|
+
Preferred way to run tests:
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
tox
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Or run a specific test file:
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
pytest tests/test_pnn.py
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Since the tests import the installed package, it is most convenient to run them in a prepared environment after installing the dependencies.
|
|
328
|
+
|
|
329
|
+
## Performance
|
|
330
|
+
|
|
331
|
+
The repository contains separate materials for measuring performance:
|
|
332
|
+
|
|
333
|
+
- [benchmarks/inference_bemchmarks.py](benchmarks/inference_bemchmarks.py)
|
|
334
|
+
- [benchmarks/benchmarking.ipynb](benchmarks/benchmarking.ipynb)
|
|
335
|
+
|
|
336
|
+
## Examples
|
|
337
|
+
|
|
338
|
+
An additional usage example is located at:
|
|
339
|
+
|
|
340
|
+
- [examples/basic_usage.ipynb](examples/basic_usage.ipynb)
|
|
341
|
+
|
|
342
|
+
## License
|
|
343
|
+
|
|
344
|
+
The project is distributed under the MIT license. See the [LICENCE](LICENCE) file.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
probabilisticnn/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
probabilisticnn/base/__init__.py,sha256=OjdvYSKU1wkH1tDKoG0c6IUlf50F-P1sG1-FIpEVP3M,870
|
|
3
|
+
probabilisticnn/base/kernels.py,sha256=fGjA4Ewuqh2xJHkoYwcOG4rxu0Q8yIzIAKI4g9102YQ,15603
|
|
4
|
+
probabilisticnn/base/loss.py,sha256=bv199VtR3nXQPO7crlDfkgXCd2x6-4WpJCDIur4fYB4,16011
|
|
5
|
+
probabilisticnn/base/optim.py,sha256=brbG-XxeGBT5k1D6QK-31_SBhfb1oTi9KPL7GuOJo2Y,11928
|
|
6
|
+
probabilisticnn/base/types.py,sha256=AXtQHlLSx-rEDnmrbYodbgJ22VBRmf1OTo1TkilAC24,261
|
|
7
|
+
probabilisticnn/base/utils.py,sha256=6vIN4JwvbOC_NQqfJ6Bmo2fypn6KHgjI5OoxTJ8Ttck,3173
|
|
8
|
+
probabilisticnn/common/__init__.py,sha256=3dSc1Ce9fHCr-mSb-3ZgLeqscwkcGrTw6amCZS8eEtI,217
|
|
9
|
+
probabilisticnn/common/pattern_layer.py,sha256=C-jhV1dUy67xOIPHwGk41H7ffV_iRnyMFSiiuralb6k,8554
|
|
10
|
+
probabilisticnn/grnn/__init__.py,sha256=XbGV0OPfvAwCWsNG7G5RX-JxWOQ91oNGo4Cq8HKlIT4,154
|
|
11
|
+
probabilisticnn/grnn/grnn.py,sha256=ANpj_6kKkItvf0V-JCHG1h-FUuXm9q5H2lsKFIOECeo,7312
|
|
12
|
+
probabilisticnn/grnn/layers.py,sha256=hSBQGaoFZPUr7aagQvj08PppRcSabh2iPmDCnha6kYU,319
|
|
13
|
+
probabilisticnn/numba_backend/__init__.py,sha256=3ck_D-p7PslJxAi2Z3abhaV8w46VU-rCd1pusIHHr2s,267
|
|
14
|
+
probabilisticnn/numba_backend/kernels.py,sha256=8QHLHZ2PfYzUJY1IldhPxdNxDwsU6iG6RZS53DIA5Rk,17473
|
|
15
|
+
probabilisticnn/numba_backend/inference/__init__.py,sha256=PKq9Ee07h6qKpbYhBcIPrjuemshs3g0dRB5Xj-NeY-8,138
|
|
16
|
+
probabilisticnn/numba_backend/inference/grnn.py,sha256=D9htgqG3-bVdYThCmeWAQHICUFU65gbdvQ9NyQKqfBA,988
|
|
17
|
+
probabilisticnn/numba_backend/inference/pnn.py,sha256=qKphAzPEoS3cUiw3OU5_EtgSZSlOlXMZIGpuejW9wvQ,1200
|
|
18
|
+
probabilisticnn/pnn/__init__.py,sha256=ab12thGSdQN-0MrSF0vb91wl2-TTX621hMNlkQhOHgg,143
|
|
19
|
+
probabilisticnn/pnn/layers.py,sha256=s08JLGjeEHLS-QyO2EctY2S7H0yWIqH86d7oActy4qU,3067
|
|
20
|
+
probabilisticnn/pnn/pnn.py,sha256=CRF4o_4HKSn-tl2XDAfK4vJ_BwXM5SkFamXH7NSWQ0k,8454
|
|
21
|
+
probabilisticnn-0.0.1.dist-info/licenses/LICENCE,sha256=t7qW5eFcLnKHjlZVaGiXssZ25cJXf84VXcrzm2ud3MU,1062
|
|
22
|
+
probabilisticnn-0.0.1.dist-info/METADATA,sha256=2wEOAHlhD6xk14S7J2EawnmLOluExGRPiUw23MsvHw4,7670
|
|
23
|
+
probabilisticnn-0.0.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
24
|
+
probabilisticnn-0.0.1.dist-info/top_level.txt,sha256=sXfYWmv5MeyG63zAOWmC-smpzS_zAL1VtNGoqXRc9EA,16
|
|
25
|
+
probabilisticnn-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Grigorii Vinogradov
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
probabilisticnn
|