pysindy.feature_library.SINDyPILibrary
- class pysindy.feature_library.SINDyPILibrary(library_functions=None, t=None, x_dot_library_functions=None, function_names=None, interaction_only=True, differentiation_method=None, include_bias=False)[source]
WARNING: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead.
Generate a library with custom functions. The Library takes custom libraries for X and Xdot respectively, and then tensor-products them together. For a 3D system, a library of constant and linear terms in x_dot, i.e. [1, x_dot0, …, x_dot3], is good enough for most problems and implicit terms. The function names list should include both X and Xdot functions, without the mixed terms.
- Parameters:
library_functions (list of mathematical functions) – Functions to include in the library. Each function will be applied to each input variable x.
x_dot_library_functions (list of mathematical functions) – Functions to include in the library. Each function will be applied to each input variable x_dot.
t (np.ndarray of time slices) – Time base to compute Xdot from X for the implicit terms
differentiation_method (differentiation object, optional) – Method for differentiating the data. This must be a class extending
pysindy.differentiation_methods.base.BaseDifferentiationclass. The default option is centered difference.function_names (list of functions, optional (default None)) – List of functions used to generate feature names for each library function. Each name function must take a string input (representing a variable name), and output a string depiction of the respective mathematical function applied to that variable. For example, if the first library function is sine, the name function might return \(\sin(x)\) given \(x\) as input. The function_names list must be the same length as library_functions. If no list of function names is provided, defaults to using \([ f_0(x),f_1(x), f_2(x), \ldots ]\). For SINDy-PI, function_names should include the names of the functions in both the x and x_dot libraries (library_functions and x_dot_library_functions), but not the mixed terms, which are computed in the code.
interaction_only (boolean, optional (default True)) – Whether to omit self-interaction terms. If True, function evaulations of the form \(f(x,x)\) and \(f(x,y,x)\) will be omitted, but those of the form \(f(x,y)\) and \(f(x,y,z)\) will be included. If False, all combinations will be included.
include_bias (boolean, optional (default False)) – If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model). This is hard to do with just lambda functions, because if the system is not 1D, lambdas will generate duplicates.
- Attributes:
functions (list of functions) – Mathematical library functions to be applied to each input feature.
function_names (list of functions) – Functions for generating string representations of each library function.
n_features_in_ (int) – The total number of input features.
n_output_features_ (int) – The total number of output features. The number of output features is the product of the number of library functions and the number of input features.
Examples
>>> import numpy as np >>> from pysindy.feature_library import SINDyPILibrary >>> t = np.linspace(0, 1, 5) >>> x = np.ones((5, 2)) >>> functions = [lambda x: 1, lambda x : np.exp(x), lambda x,y : np.sin(x+y)] >>> x_dot_functions = [lambda x: 1, lambda x : x] >>> function_names = [lambda x: '', lambda x : 'exp(' + x + ')', lambda x, y : 'sin(' + x + y + ')', lambda x: '', lambda x : x] >>> lib = ps.SINDyPILibrary(library_functions=functions, x_dot_library_functions=x_dot_functions, function_names=function_names, t=t ).fit(x) >>> lib.transform(x) [[ 1.00000000e+00 2.71828183e+00 2.71828183e+00 9.09297427e-01 2.22044605e-16 6.03579815e-16 6.03579815e-16 2.01904588e-16 2.22044605e-16 6.03579815e-16 6.03579815e-16 2.01904588e-16] [ 1.00000000e+00 2.71828183e+00 2.71828183e+00 9.09297427e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 1.00000000e+00 2.71828183e+00 2.71828183e+00 9.09297427e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 1.00000000e+00 2.71828183e+00 2.71828183e+00 9.09297427e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 1.00000000e+00 2.71828183e+00 2.71828183e+00 9.09297427e-01 -2.22044605e-16 -6.03579815e-16 -6.03579815e-16 -2.01904588e-16 -2.22044605e-16 -6.03579815e-16 -6.03579815e-16 -2.01904588e-16]] >>> lib.get_feature_names() ['', 'exp(x0)', 'exp(x1)', 'sin(x0x1)', 'x0_dot', 'exp(x0)x0_dot', 'exp(x1)x0_dot', 'sin(x0x1)x0_dot', 'x1_dot', 'exp(x0)x1_dot', 'exp(x1)x1_dot', 'sin(x0x1)x1_dot']
Methods
Compute number of output features.
Return feature names for output features.
Configure whether metadata should be requested to be passed to the
fitmethod.Configure whether metadata should be requested to be passed to the
transformmethod.Transform data to custom features
Attributes
n_features_in_n_output_features_- fit(x_full, y=None)[source]
Compute number of output features.
- Parameters:
x (array-like, shape (n_samples, n_features)) – Measurement data.
- Returns:
self
- Return type:
instance
- get_feature_names(input_features=None)[source]
Return feature names for output features.
- Parameters:
input_features (list of string, length n_features, optional) – String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.
- Returns:
output_feature_names
- Return type:
list of string, length n_output_features
- set_fit_request(*, x_full: bool | None | str = '$UNCHANGED$') SINDyPILibrary
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
x_full (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
x_fullparameter infit.- Returns:
self – The updated object.
- Return type:
object
- set_transform_request(*, x_full: bool | None | str = '$UNCHANGED$') SINDyPILibrary
Configure whether metadata should be requested to be passed to the
transformmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
x_full (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
x_fullparameter intransform.- Returns:
self – The updated object.
- Return type:
object
- transform(x_full)[source]
Transform data to custom features
- Parameters:
x (array-like, shape (n_samples, n_features)) – The data to transform, row by row.
- Returns:
xp – The matrix of features, where n_output_features is the number of features generated from applying the custom functions to the inputs.
- Return type:
np.ndarray, shape (n_samples, n_output_features)