Skip to main content

Python package for Probabilistic Neural Networks and General Regression Neural Networks

Project description

ProbabilisticNN

ProbabilisticNN is a Python library for Probabilistic Neural Networks and General Regression Neural Networks with an sklearn-like interface.

At the current stage, the library is published on TestPyPI and supports:

  • PNN for classification;
  • AdaptivePNN with kernel bandwidth parameter tuning;
  • GRNN for regression;
  • AdaptiveGRNN with kernel bandwidth optimization;
  • execution using the standard numpy implementation;
  • accelerated inference via numba.

Main Features

  • sklearn-like interface: fit, predict, predict_proba;
  • several kernel functions:
    • gaussian;
    • laplacian;
    • exponential;
  • several kernel bandwidth parameterization schemes for AdaptivePNN:
    • per_feature;
    • per_class;
    • per_class_per_feature;
  • accelerated numba backend for the computationally heavy part of inference;
  • testing via pytest and tox.

Requirements

  • Python 3.12+
  • numpy
  • scipy
  • scikit-learn

For the accelerated backend, numba is additionally required.

Installation

Installation from TestPyPI

For regular usage without numba:

python -m pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple/ \
  "ProbabilisticNN"

For installation with the numba backend:

python -m pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple/ \
  "ProbabilisticNN[numba]"

For development and testing:

python -m pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple/ \
  "ProbabilisticNN[dev]"

Local Installation from the Repository

This requires setuptools.

python -m build
python -m pip install -e ".[dev]"

Quick Start

Classification with PNN

import numpy as np

from probabilisticnn.pnn import PNN

X_train = np.array([
    [0.0, 0.0],
    [0.0, 0.2],
    [1.0, 1.0],
    [1.0, 1.2],
])
y_train = np.array(["class_a", "class_a", "class_b", "class_b"])

X_test = np.array([
    [0.0, 0.1],
    [1.0, 1.1],
])

model = PNN(
    bandwidth=0.25,
    kernel="gaussian",
    normalize=False,
)

model.fit(X_train, y_train)

labels = model.predict(X_test)
proba = model.predict_proba(X_test)

print(labels)
print(proba)

Classification with AdaptivePNN

import numpy as np

from probabilisticnn.pnn import AdaptivePNN

X_train = np.array([
    [0.0, 0.0],
    [0.0, 0.2],
    [1.0, 1.0],
    [1.0, 1.2],
])
y_train = np.array([0, 0, 1, 1])

model = AdaptivePNN(
    kernel="gaussian",
    bandwidth_sharing="per_feature",
    loss="correct_class_probability",
    solver="auto",
    normalize=False,
)

model.fit(X_train, y_train)

print(model.predict(X_train))
print(model.predict_proba(X_train))
print(model.bandwidth_)

Regression with GRNN

import numpy as np

from probabilisticnn.grnn import GRNN

X_train = np.array([
    [0.0, 0.0],
    [0.0, 0.2],
    [1.0, 1.0],
    [1.0, 1.2],
])
y_train = np.array([1.0, 1.1, 3.0, 3.1])

X_test = np.array([
    [0.0, 0.1],
    [1.0, 1.1],
])

model = GRNN(
    bandwidth=0.5,
    kernel="gaussian",
    normalize=False,
)

model.fit(X_train, y_train)
pred = model.predict(X_test)

print(pred)

Regression with AdaptiveGRNN

import numpy as np

from probabilisticnn.grnn import AdaptiveGRNN

X_train = np.array([
    [0.0, 0.0],
    [0.0, 0.2],
    [1.0, 1.0],
    [1.0, 1.2],
])
y_train = np.array([1.0, 1.1, 3.0, 3.1])

model = AdaptiveGRNN(
    kernel="gaussian",
    loss="mse",
    solver="auto",
    normalize=False,
)

model.fit(X_train, y_train)

print(model.predict(X_train))
print(model.bandwidth_)

Acceleration with numba

For PNN, AdaptivePNN, GRNN, and AdaptiveGRNN, you can choose:

  • backend="numpy" — the main option;
  • backend="numba" — the accelerated option.

Example:

from probabilisticnn.pnn import PNN

model = PNN(
    bandwidth=0.25,
    kernel="gaussian",
    backend="numba",
    normalize=False,
)

Important:

  • the first predict call for the numba branch may be slower because of JIT compilation;
  • the main speed gain is achieved during the computation of kernel function values;
  • if numba is not installed, the numba backend will be unavailable.

Public Interface

Models should be imported like this:

from probabilisticnn.pnn import PNN, AdaptivePNN
from probabilisticnn.grnn import GRNN, AdaptiveGRNN

Model Parameters

PNN

Main parameters:

  • bandwidth — fixed kernel bandwidth;
  • kernel — kernel type;
  • losses — class weighting scheme / misclassification cost for classes;
  • normalize — whether to use L2 normalization of inputs;
  • backendnumpy or numba;
  • compute_dtypeauto, float32, float64.

AdaptivePNN

Additionally supports:

  • loss — optimization objective function;
  • bandwidth_sharing — method for defining bandwidth parameters;
  • max_iter — maximum number of optimization steps;
  • solver — optimization method;
  • solver_options — additional optimizer parameters.

GRNN

Main parameters:

  • bandwidth;
  • kernel;
  • backend;
  • compute_dtype;
  • normalize.

AdaptiveGRNN

Additionally supports:

  • loss — loss function for regression;
  • max_iter;
  • solver;
  • solver_options;
  • normalize.

AdaptiveGRNN optimizes the kernel bandwidth using the per_feature scheme.

Testing

Preferred way to run tests:

tox

Or run a specific test file:

pytest tests/test_pnn.py

Since the tests import the installed package, it is most convenient to run them in a prepared environment after installing the dependencies.

Performance

The repository contains separate materials for measuring performance:

Examples

An additional usage example is located at:

License

The project is distributed under the MIT license. See the LICENCE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

probabilisticnn-0.0.1.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

probabilisticnn-0.0.1-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file probabilisticnn-0.0.1.tar.gz.

File metadata

  • Download URL: probabilisticnn-0.0.1.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for probabilisticnn-0.0.1.tar.gz
Algorithm Hash digest
SHA256 81ed6c03fc2fa220f01984daaeb26d711e05f8e2c37f38961c1a99ab0ab057aa
MD5 7ba79efb5aa3c840eb3d79ffddee977a
BLAKE2b-256 67abc8ded75d460470523e6b563f491d5aad04e1e6c257004800dfe5f09c5457

See more details on using hashes here.

File details

Details for the file probabilisticnn-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for probabilisticnn-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b07701082087fe2535fa7881d41eaf5e5b84e71c9aef2576781c222a3c55c28
MD5 3c1ac92e63314ebabfbe3f71494d7a9f
BLAKE2b-256 ea10807d8c9605e35972ed7b1d1ea1fd1a594f683e282aca4565e5c8cefc490a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page