folie.functions.ModelOverlay

class folie.functions.ModelOverlay(model, function_name, output_shape=None, **kwargs)[source]

A class that allow to overlay a model and make it be used as a function.

For example, if a model contain

model.drift =  ModelOverlay(model, "_force", output_shape=output_shape_force)

then we have the following mapping:

  • model.drift(x) -> model._force(x)

  • model.drift.grad_x(x) -> model._force_dx(x)

  • model.drift.hessian_x(x) -> model._force_d2x(x)

  • model.drift.grad_coeffs(x) -> model._force_dcoeffs(x)

  • model.drift.coefficients -> model._force_coefficients

If any of the [function_name]_d* function is not implemented, it would be replaced by a numerical derivative

Parameters:
model: a python class

This is the class to to overlay

function_name: str

The common part of the function name. The model should contain at least the function [function_name]

output_shape: tuple or array

The output shape of the term

copy()[source]

Makes a deep copy of this function.

Returns:
copy

A new copy of this model.

fit(x, *args, y=None, estimator=LinearRegression(copy_X=False, fit_intercept=False), sample_weight=None, **kwargs)[source]

Fit coefficients of the function using linear regression. Use as features the derivative of the function with respect to the coefficients

Parameters:
X{array-like} of shape (n_samples, dim)
Point of evaluation of the training data
yarray-like of shape (n_samples,) or (n_samples, n_targets)
Target values. Will be cast to X’s dtype if necessary.
estimator: sklearn compatible estimator
Defaut to sklearn.linear_model.LinearRegression(copy_X=False, fit_intercept=False) but any compatible estimator can be used.
Estimator should have a coef_ attibutes after fitting
fit_transform(X, y=None, **fit_params)[source]

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=False)[source]

Get the parameters.

Returns:
paramsmapping of string to any

Parameter names mapped to their values.

grad_coeffs(x, *args, **kwargs)[source]

Gradient of the function with respect to the coefficients.

Parameters:
xarray_like

Input data.

Returns:
transformedarray_like

The gradient

resize(new_shape)[source]

Change the output shape of the function.

Parameters:
new_shapetuple, array-like

The new output shape of the function

set_output(*, transform=None)[source]

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”, “polars”}, default=None

Configure output of transform and fit_transform.

  • “default”: Default output format of a transformer

  • “pandas”: DataFrame output

  • “polars”: Polars output

  • None: Transform configuration is unchanged

Added in version 1.4: “polars” option was added.

Returns:
selfestimator instance

Estimator instance.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfobject

Estimator instance.

transform(x, *args, **kwargs)[source]

Transforms the input data.

transform_d2x(x, *args, **kwargs)[source]

Hessian of the function with respect to input data. Implemented by finite difference.

transform_dcoeffs(x, *args, **kwargs)[source]

Jacobian of the force with respect to coefficients

transform_dx(x, *args, **kwargs)[source]

Gradient of the function with respect to input data. Implemented by finite difference.

property coefficients

Access the coefficients