pysindy.feature_library.CustomLibrary
- class pysindy.feature_library.CustomLibrary(library_functions, function_names=None, interaction_only=True, include_bias=False)[source]
Generate a library with custom functions.
- Parameters:
library_functions (list of mathematical functions) – Functions to include in the library. Default is to use same functions for all variables. Can also be used so that each variable has an associated library, in this case library_functions is shape (n_input_features, num_library_functions)
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 ]\).
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 CustomLibrary >>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]]) >>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)] >>> lib = CustomLibrary(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
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$') CustomLibrary
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$') CustomLibrary
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)