pysindy.feature_library.WeakPDELibrary
- class pysindy.feature_library.WeakPDELibrary(function_library: ~pysindy.feature_library.base.BaseFeatureLibrary | None = None, derivative_order=0, spatiotemporal_grid=None, include_bias=False, include_interaction=True, K=100, H_xt=None, p=4, num_pts_per_domain=None, implicit_terms=False, multiindices=None, differentiation_method=<class 'pysindy.differentiation.finite_difference.FiniteDifference'>, diff_kwargs={}, is_uniform=None, periodic=None)[source]
- Generate a weak formulation library with custom functions and,
optionally, any spatial derivatives in arbitrary dimensions.
The features in the weak formulation are integrals of derivatives of input data multiplied by a test function phi, which are evaluated on K subdomains randomly sampled across the spatiotemporal grid. Each subdomain is initial generated with a size H_xt along each axis, and is then shrunk such that the left and right boundaries lie on spatiotemporal grid points. The expressions are integrated by parts to remove as many derivatives from the input data as possible and put the derivatives onto the test functions.
The weak integral features are calculated assuming the function f(x) to integrate against derivatives of the test function dphi(x) is linear between grid points provided by the data: f(x)=f_i+(x-x_i)/(x_{i+1}-x_i)*(f_{i+1}-f_i) Thus f(x)*dphi(x) is approximated as a piecewise polynomial. The piecewise components are integrated analytically. To improve performance, the complete integral is expressed as a dot product of weights against the input data f_i, which enables vectorized evaulations.
- Parameters:
function_library (BaseFeatureLibrary, optional (default) – PolynomialLibrary(degree=3,include_bias=False)) SINDy library with output features representing library_functions to include in the library, in place of library_functions.
derivative_order (int, optional (default 0)) – Order of derivative to take on each input variable, can be arbitrary non-negative integer.
spatiotemporal_grid (np.ndarray (default None)) – The spatiotemporal grid for computing derivatives. This variable must be specified with at least one dimension corresponding to a temporal grid, so that integration by parts can be done in the weak formulation.
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.
include_interaction (boolean, optional (default True)) – This is a different than the use for the PolynomialLibrary. If true, it generates all the mixed derivative terms. If false, the library will consist of only pure no-derivative terms and pure derivative terms, with no mixed terms.
K (int, optional (default 100)) – Number of domain centers, corresponding to subdomain squares of length Hxt. If K is not specified, defaults to 100.
H_xt (array of floats, optional (default None)) – Half of the length of the square subdomains in each spatiotemporal direction. If H_xt is not specified, defaults to H_xt = L_xt / 20, where L_xt is the length of the full domain in each spatiotemporal direction. If H_xt is specified as a scalar, this value will be applied to all dimensions of the subdomains.
p (int, optional (default 4)) – Positive integer to define the polynomial degree of the spatial weights used for weak/integral SINDy.
num_pts_per_domain (int, deprecated (default None)) – Included here to retain backwards compatibility with older code that uses this parameter. However, it merely raises a DeprecationWarning and then is ignored.
implicit_terms (boolean) – Flag to indicate if SINDy-PI (temporal derivatives) is being used for the right-hand side of the SINDy fit.
multiindices (list of integer arrays, (default None)) – Overrides the derivative_order to customize the included derivative orders. Each integer array indicates the order of differentiation along the corresponding axis for each derivative term.
differentiation_method (callable, (default FiniteDifference)) –
Spatial differentiation method.
- diff_kwargs: dictionary, (default {})
Keyword options to supply to differtiantion_method.
- Attributes:
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 WeakPDELibrary >>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]]) >>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)] >>> lib = WeakPDELibrary(library_functions=functions).fit(x) >>> lib.transform(x) array([[ 1. , 0.36787944, -0.84147098], [ 2.71828183, 1. , 0.84147098], [ 7.3890561 , 0.36787944, 0.84147098]]) >>> lib.get_feature_names() ['f0(x0)', 'f0(x1)', 'f1(x0,x1)']
Methods
calc_trajectoryTakes a full set of spatiotemporal fields u(x, t) and finds the weak form of u_dot.
Compute number of output features.
Return feature names for output features.
Configure whether metadata should be requested to be passed to the
fitmethod.Set the parameters of this estimator.
Configure whether metadata should be requested to be passed to the
transformmethod.Transform data to custom features
Attributes
n_features_in_n_output_features_- convert_u_dot_integral(u)[source]
Takes a full set of spatiotemporal fields u(x, t) and finds the weak form of u_dot.
- 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$') WeakPDELibrary
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_params(**kwargs)[source]
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_transform_request(*, x_full: bool | None | str = '$UNCHANGED$') WeakPDELibrary
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)