API Reference

Core Module

class nnapprox.core.base.BaseApproximator(*, input, output, verbose=False)[source]

Bases: ABC

Minimal interface that all backends must implement.

Concrete classes must implement:
  • fit – train the model

  • predict – return predictions for arbitrary input formats

  • save / load – persistence

The __call__ method is provided here so that every subclass can be used like a plain Python function:

>>> y = func(x1, x2)         # scalar call
>>> y_arr = func(df)         # DataFrame call
>>> df_out = func(df, return_dataframe=True)
Parameters:
  • input (Sequence[str])

  • output (Sequence[str])

  • verbose (bool)

set_transform(label, *, predefined=None, forward=None, inverse=None)[source]

Register a transformation for an input or output variable.

Transformations are applied before training/prediction and inverted after prediction. This is useful for handling variables with different scales or non-linear relationships.

Parameters:
  • label (str) – Name of the variable (must be in input_names or output_names)

  • predefined (str, optional) – Predefined transform name. Options: ‘log’, ‘log10’, ‘sqrt’, ‘square’, ‘identity’. Cannot be used with forward/inverse.

  • forward (Callable, optional) – Custom forward transformation function. Must be used with inverse.

  • inverse (Callable, optional) – Custom inverse transformation function. Must be used with forward.

Raises:

ValueError – If label is not a known input or output name, or if both predefined and forward/inverse are provided

Return type:

None

Examples

Return type:

None

Parameters:
  • label (str)

  • predefined (str | None)

  • forward (Any | None)

  • inverse (Any | None)

Using predefined transforms:

>>> func.set_transform('x', predefined='log')
>>> func.set_transform('y', predefined='sqrt')

Using custom transforms (must be defined in a module)

>>> # In my_transforms.py:
>>> # def cube(x): return x**3
>>> # def cube_root(x): return x**(1/3)
>>>
>>> from my_transforms import cube, cube_root
>>> func.set_transform('x', forward=cube, inverse=cube_root)

Notes

Custom transforms defined as lambdas or in notebooks require cloudpickle for serialization and may not be portable across Python versions.

See also

Transform.predefined

Available predefined transforms

score(X, y)[source]

Return the coefficient of determination R² of the prediction.

Return type:

float

Parameters:
abstract fit(data, **kwargs)[source]
Return type:

BaseApproximator

Parameters:

data (Any)

abstract predict(*args, return_dataframe=False, **kwargs)[source]
Return type:

Any

Parameters:
  • args (Any)

  • return_dataframe (bool)

abstract save(path)[source]
Return type:

None

Parameters:

path (str)

abstract load(path)[source]
Return type:

BaseApproximator

Parameters:

path (str)

PyTorch Backend

Utilities

class nnapprox.core.utils.Transform(forward, inverse, spec)[source]

Bases: object

Container for a forward / inverse pair and a serialisable spec.

Parameters:
forward
inverse
spec
classmethod predefined(name)[source]

Factory for the four built‑in transforms.

Return type:

Transform

Parameters:

name (str)

classmethod custom(forward, inverse)[source]

Factory for user‑supplied callables.

Return type:

Transform

Parameters:

Exceptions

Custom exceptions for nnapprox.

exception nnapprox.core.exceptions.NNApproxError[source]

Bases: Exception

Base exception for nnapprox.

exception nnapprox.core.exceptions.BackendNotAvailableError[source]

Bases: NNApproxError

Raised when a requested backend is not available.

exception nnapprox.core.exceptions.ModelNotFittedError[source]

Bases: NNApproxError

Raised when trying to use a model that hasn’t been fitted.