pysindy.feature_library package

Submodules

pysindy.feature_library.base module

Base class for feature library classes.

class pysindy.feature_library.base.BaseFeatureLibrary(library_ensemble=None, ensemble_indices=[0])[source]

Bases: TransformerMixin

Base class for feature libraries.

Forces subclasses to implement fit, transform, and get_feature_names methods.

Parameters
  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

validate_input(x, *args, **kwargs)[source]
reshape_samples_to_spatial_grid(x: ndarray) AxesArray[source]

Adapt predictions to fitted spatial grid.

correct_shape(x: AxesArray)[source]

Correct the shape of x, given what we know of the problem

calc_trajectory(diff_method, x, t)[source]
get_spatial_grid()[source]
abstract fit(x, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

abstract transform(x)[source]

Transform data.

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 the combination of inputs.

Return type

np.ndarray, [n_samples, n_output_features]

abstract 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

property size
pysindy.feature_library.base.x_sequence_or_item(wrapped_func)[source]

Allow a feature library’s method to handle list or item inputs.

class pysindy.feature_library.base.ConcatLibrary(libraries: list, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Concatenate multiple libraries into one library. All settings provided to individual libraries will be applied.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import ConcatLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_concat = ConcatLibrary([lib_custom, lib_fourier])
>>> lib_concat.fit()
>>> lib.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]
class pysindy.feature_library.base.TensoredLibrary(libraries: list, library_ensemble=False, inputs_per_library=None, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Tensor multiple libraries together into one library. All settings provided to individual libraries will be applied.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • inputs_per_library_ (numpy nd.array) – Array that specifies which inputs should be used for each of the libraries you are going to tensor together. Used for building GeneralizedLibrary objects.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the product of the numbers of output features for each of the libraries that were tensored together.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import TensoredLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_tensored = lib_custom * lib_fourier
>>> lib_tensored.fit(x)
>>> lib_tensored.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]

pysindy.feature_library.custom_library module

class pysindy.feature_library.custom_library.CustomLibrary(library_functions, function_names=None, interaction_only=True, library_ensemble=False, ensemble_indices=[0], include_bias=False)[source]

Bases: BaseFeatureLibrary

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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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)']
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

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

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)

pysindy.feature_library.fourier_library module

class pysindy.feature_library.fourier_library.FourierLibrary(n_frequencies=1, include_sin=True, include_cos=True, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Generate a library with trigonometric functions.

Parameters
  • n_frequencies (int, optional (default 1)) – Number of frequencies to include in the library. The library will include functions \(\sin(x), \sin(2x), \dots \sin(n_{frequencies}x)\) for each input feature \(x\) (depending on which of sine and/or cosine features are included).

  • include_sin (boolean, optional (default True)) – If True, include sine terms in the library.

  • include_cos (boolean, optional (default True)) – If True, include cosine terms in the library.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default 0)) – The indices to use for ensembling the library.

Attributes
  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is 2 * n_input_features_ * n_frequencies if both sines and cosines are included. Otherwise it is n_input_features * n_frequencies.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary
>>> x = np.array([[0.],[1.],[2.]])
>>> lib = FourierLibrary(n_frequencies=2).fit(x)
>>> lib.transform(x)
array([[ 0.        ,  1.        ,  0.        ,  1.        ],
       [ 0.84147098,  0.54030231,  0.90929743, -0.41614684],
       [ 0.90929743, -0.41614684, -0.7568025 , -0.65364362]])
>>> lib.get_feature_names()
['sin(1 x0)', 'cos(1 x0)', 'sin(2 x0)', 'cos(2 x0)']
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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data to Fourier 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 Fourier features generated from the inputs.

Return type

np.ndarray, shape (n_samples, n_output_features)

pysindy.feature_library.generalized_library module

class pysindy.feature_library.generalized_library.GeneralizedLibrary(libraries: list, tensor_array=None, inputs_per_library=None, library_ensemble=False, ensemble_indices=[0], exclude_libraries=[])[source]

Bases: BaseFeatureLibrary

Put multiple libraries into one library. All settings provided to individual libraries will be applied. Note that this class allows one to specifically choose which input variables are used for each library, and take tensor products of any pair of libraries. Tensored libraries inherit the same input variables specified for the individual libraries.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • tensor_array (2D list of booleans, optional, (default None)) – Default is to not tensor any of the libraries together. Shape equal to the # of tensor libraries and the # feature libraries. Indicates which pairs of libraries to tensor product together and add to the overall library. For instance if you have 5 libraries, and want to do two tensor products, you could use the list [[1, 0, 0, 1, 0], [0, 1, 0, 1, 1]] to indicate that you want two tensored libraries from tensoring libraries 0 and 3 and libraries 1, 3, and 4.

  • inputs_per_library (2D np.ndarray, optional (default None)) – Shape should be equal to # feature libraries by # variable input. Can be used to specify a subset of the variables to use to generate a feature library. If number of feature libraries > 1, then can be used to generate a large number of libraries, each using their own subsets of the input variables. Note that this must be specified for all the individual feature libraries.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • tensor_array_ (2D list of booleans (default None)) – Indicates which pairs of libraries to tensor product together and add to the overall library. For instance if you have 5 libraries, and want to do two tensor products, you could use the list [[1, 0, 0, 1, 0], [0, 1, 0, 1, 1]] to indicate that you want two tensored libraries from tensoring libraries 0 and 3 and libraries 1, 3, and 4. Shape equal to # of tensor libraries to make by the # feature libraries.

  • inputs_per_library_ (2D np.ndarray, (default None)) – Default is that all inputs are used for every library. Can be used to specify a subset of the variables to use to generate a feature library. If number of feature libraries > 1, then can be use to generate a large number of libraries, each using their own subsets of the input variables. Note that this must be specified for all the individual feature libraries. The shape is equal to # feature libraries, # variable inputs.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import GeneralizedLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_generalized = GeneralizedLibrary([lib_custom, lib_fourier])
>>> lib_generalized.fit(x)
>>> lib_generalized.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]
get_spatial_grid()[source]
pysindy.feature_library.generalized_library.has_weak(lib)[source]
pysindy.feature_library.generalized_library.has_nonweak(lib)[source]

pysindy.feature_library.identity_library module

class pysindy.feature_library.identity_library.IdentityLibrary(library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Generate an identity library which maps all input features to themselves.

Attributes
  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is equal to the number of input features.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import IdentityLibrary
>>> x = np.array([[0,-1],[0.5,-1.5],[1.,-2.]])
>>> lib = IdentityLibrary().fit(x)
>>> lib.transform(x)
array([[ 0. , -1. ],
       [ 0.5, -1.5],
       [ 1. , -2. ]])
>>> lib.get_feature_names()
['x0', 'x1']
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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Perform identity transformation (return a copy of the input).

Parameters

x (array-like, shape (n_samples, n_features)) – The data to transform, row by row.

Returns

x – The matrix of features, which is just a copy of the input data.

Return type

np.ndarray, shape (n_samples, n_features)

pysindy.feature_library.parameterized_library module

class pysindy.feature_library.parameterized_library.ParameterizedLibrary(parameter_library=PolynomialLibrary(degree=1), feature_library=PolynomialLibrary(), num_parameters=3, num_features=3, library_ensemble=False, ensemble_indices=[0])[source]

Bases: GeneralizedLibrary

Construct a SINDyCP library to fit multiple trajectories with variable control parameters. The library is composed of a tensor product of a feature library, applied to the input data, and a parameter library, applied to the input control. If the input libraries are weak, the temporal derivatives are automatically rescaled by the appropriate domain volumes.

Parameters
  • feature_library (BaseFeatureLibrary, optional (default PolynomialLibrary).) –

  • features. (Specifies the library function to apply to the input control) –

  • parameter_library (BaseFeatureLibrary, optional (default PolynomialLibrary).) –

  • features.

  • num_features (int, optional (default 3)) –

  • data. (Specifies the number of features in the input) –

  • num_parameters (int, optional (default 3)) –

  • control. (Specifies the number of features in the input) –

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix. Equal to [parameter_library,feature_library].

  • tensor_array_ (2D list of booleans) – Indicates which pairs of libraries to tensor product together and add to the overall library. Equal to [0,1]

  • inputs_per_library_ (2D np.ndarray) – Can be used to specify a subset of the variables to use to generate a feature library. Value determined by num_parameters and num_features.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import ParameterizedLibrary,PolynomialLibrary
>>> from pysindy import AxesArray
>>> xs=[np.random.random((5,3)) for n in range(3)]
>>> us=[np.random.random((5,3)) for n in range(3)]
>>> feature_lib=PolynomialLibrary(degree=3)
>>> parameter_lib=PolynomialLibrary(degree=1)
>>> lib=ParameterizedLibrary(feature_library=feature_lib,
>>>     parameter_library=parameter_lib,num_features=3,num_parameters=3)
>>> xus=[AxesArray(np.concatenate([xs[i],us[i]],axis=-1)) for i in range(3)]
>>> lib.fit(xus)
>>> lib.transform(xus)
calc_trajectory(diff_method, x, t)[source]

pysindy.feature_library.pde_library module

class pysindy.feature_library.pde_library.PDELibrary(library_functions=[], derivative_order=0, spatial_grid=None, temporal_grid=None, interaction_only=True, function_names=None, include_bias=False, include_interaction=True, library_ensemble=False, ensemble_indices=[0], implicit_terms=False, multiindices=None, differentiation_method=<class 'pysindy.differentiation.finite_difference.FiniteDifference'>, diff_kwargs={}, is_uniform=None, periodic=None)[source]

Bases: BaseFeatureLibrary

Generate a PDE library with custom functions.

Parameters
  • library_functions (list of mathematical functions, optional (default None)) – Functions to include in the library. Each function will be applied to each input variable (but not their derivatives)

  • derivative_order (int, optional (default 0)) – Order of derivative to take on each input variable, can be arbitrary non-negative integer.

  • spatial_grid (np.ndarray, optional (default None)) – The spatial grid for computing derivatives

  • temporal_grid (np.ndarray, optional (default None)) – The temporal grid if using SINDy-PI with PDEs.

  • 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.

  • 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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
  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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 PDELibrary
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

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

transform(x_full)[source]

Transform data to pde 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 the tensor product of the derivative terms and the library_functions applied to combinations of the inputs.

Return type

np.ndarray, shape (n_samples, n_output_features)

get_spatial_grid()[source]

pysindy.feature_library.polynomial_library module

class pysindy.feature_library.polynomial_library.PolynomialLibrary(degree=2, include_interaction=True, interaction_only=False, include_bias=True, order='C', library_ensemble=False, ensemble_indices=[0])[source]

Bases: PolynomialFeatures, BaseFeatureLibrary

Generate polynomial and interaction features.

This is the same as sklearn.preprocessing.PolynomialFeatures, but also adds the option to omit interaction features from the library.

Parameters
  • degree (integer, optional (default 2)) – The degree of the polynomial features.

  • include_interaction (boolean, optional (default True)) – Determines whether interaction features are produced. If false, features are all of the form x[i] ** k.

  • interaction_only (boolean, optional (default False)) – If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).

  • include_bias (boolean, optional (default True)) – 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).

  • order (str in {'C', 'F'}, optional (default 'C')) – Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

Attributes
  • powers_ (array, shape (n_output_features, n_input_features)) – powers_[i, j] is the exponent of the jth input in the ith output.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. This number is computed by iterating over all appropriately sized combinations of input features.

property powers_

Exponent for each of the inputs in the output.

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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data to polynomial features.

Parameters

x (array-like or CSR/CSC sparse matrix, shape (n_samples, n_features)) – The data to transform, row by row. Prefer CSR over CSC for sparse input (for speed), but CSC is required if the degree is 4 or higher. If the degree is less than 4 and the input format is CSC, it will be converted to CSR, have its polynomial features generated, then converted back to CSC. If the degree is 2 or 3, the method described in “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.

Returns

xp – shape (n_samples, n_output_features) The matrix of features, where n_output_features is the number of polynomial features generated from the combination of inputs.

Return type

np.ndarray or CSR/CSC sparse matrix,

pysindy.feature_library.sindy_pi_library module

class pysindy.feature_library.sindy_pi_library.SINDyPILibrary(library_functions=None, t=None, x_dot_library_functions=None, function_names=None, interaction_only=True, differentiation_method=None, include_bias=False, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

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.BaseDifferentiation class. 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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']
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

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

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)

pysindy.feature_library.weak_pde_library module

class pysindy.feature_library.weak_pde_library.WeakPDELibrary(library_functions=[], derivative_order=0, spatiotemporal_grid=None, function_names=None, interaction_only=True, include_bias=False, include_interaction=True, K=100, H_xt=None, p=4, library_ensemble=False, ensemble_indices=[0], 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]

Bases: BaseFeatureLibrary

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
  • library_functions (list of mathematical functions, optional (default None)) – Functions to include in the library. Each function will be applied to each input variable (but not their derivatives)

  • 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.

  • 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.

  • 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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
  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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)']
convert_u_dot_integral(u)[source]

Takes a full set of spatiotemporal fields u(x, t) and finds the weak form of u_dot.

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

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

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)

calc_trajectory(diff_method, x, t)[source]

Module contents

class pysindy.feature_library.ConcatLibrary(libraries: list, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Concatenate multiple libraries into one library. All settings provided to individual libraries will be applied.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import ConcatLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_concat = ConcatLibrary([lib_custom, lib_fourier])
>>> lib_concat.fit()
>>> lib.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]
class pysindy.feature_library.TensoredLibrary(libraries: list, library_ensemble=False, inputs_per_library=None, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Tensor multiple libraries together into one library. All settings provided to individual libraries will be applied.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • inputs_per_library_ (numpy nd.array) – Array that specifies which inputs should be used for each of the libraries you are going to tensor together. Used for building GeneralizedLibrary objects.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the product of the numbers of output features for each of the libraries that were tensored together.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import TensoredLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_tensored = lib_custom * lib_fourier
>>> lib_tensored.fit(x)
>>> lib_tensored.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]
class pysindy.feature_library.GeneralizedLibrary(libraries: list, tensor_array=None, inputs_per_library=None, library_ensemble=False, ensemble_indices=[0], exclude_libraries=[])[source]

Bases: BaseFeatureLibrary

Put multiple libraries into one library. All settings provided to individual libraries will be applied. Note that this class allows one to specifically choose which input variables are used for each library, and take tensor products of any pair of libraries. Tensored libraries inherit the same input variables specified for the individual libraries.

Parameters
  • libraries (list of libraries) – Library instances to be applied to the input matrix.

  • tensor_array (2D list of booleans, optional, (default None)) – Default is to not tensor any of the libraries together. Shape equal to the # of tensor libraries and the # feature libraries. Indicates which pairs of libraries to tensor product together and add to the overall library. For instance if you have 5 libraries, and want to do two tensor products, you could use the list [[1, 0, 0, 1, 0], [0, 1, 0, 1, 1]] to indicate that you want two tensored libraries from tensoring libraries 0 and 3 and libraries 1, 3, and 4.

  • inputs_per_library (2D np.ndarray, optional (default None)) – Shape should be equal to # feature libraries by # variable input. Can be used to specify a subset of the variables to use to generate a feature library. If number of feature libraries > 1, then can be used to generate a large number of libraries, each using their own subsets of the input variables. Note that this must be specified for all the individual feature libraries.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library).

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library. For instance, if ensemble_indices = [0], it chops off the first column of the library.

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix.

  • tensor_array_ (2D list of booleans (default None)) – Indicates which pairs of libraries to tensor product together and add to the overall library. For instance if you have 5 libraries, and want to do two tensor products, you could use the list [[1, 0, 0, 1, 0], [0, 1, 0, 1, 1]] to indicate that you want two tensored libraries from tensoring libraries 0 and 3 and libraries 1, 3, and 4. Shape equal to # of tensor libraries to make by the # feature libraries.

  • inputs_per_library_ (2D np.ndarray, (default None)) – Default is that all inputs are used for every library. Can be used to specify a subset of the variables to use to generate a feature library. If number of feature libraries > 1, then can be use to generate a large number of libraries, each using their own subsets of the input variables. Note that this must be specified for all the individual feature libraries. The shape is equal to # feature libraries, # variable inputs.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary, CustomLibrary
>>> from pysindy.feature_library import GeneralizedLibrary
>>> x = np.array([[0.,-1],[1.,0.],[2.,-1.]])
>>> functions = [lambda x : np.exp(x), lambda x,y : np.sin(x+y)]
>>> lib_custom = CustomLibrary(library_functions=functions)
>>> lib_fourier = FourierLibrary()
>>> lib_generalized = GeneralizedLibrary([lib_custom, lib_fourier])
>>> lib_generalized.fit(x)
>>> lib_generalized.transform(x)
fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data with libs provided below.

Parameters

x (array-like, shape [n_samples, n_features]) – The data to transform, row by row.

Returns

xp – The matrix of features, where NP is the number of features generated from applying the custom functions to the inputs.

Return type

np.ndarray, shape [n_samples, NP]

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

calc_trajectory(diff_method, x, t)[source]
get_spatial_grid()[source]
class pysindy.feature_library.CustomLibrary(library_functions, function_names=None, interaction_only=True, library_ensemble=False, ensemble_indices=[0], include_bias=False)[source]

Bases: BaseFeatureLibrary

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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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)']
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

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

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)

class pysindy.feature_library.FourierLibrary(n_frequencies=1, include_sin=True, include_cos=True, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Generate a library with trigonometric functions.

Parameters
  • n_frequencies (int, optional (default 1)) – Number of frequencies to include in the library. The library will include functions \(\sin(x), \sin(2x), \dots \sin(n_{frequencies}x)\) for each input feature \(x\) (depending on which of sine and/or cosine features are included).

  • include_sin (boolean, optional (default True)) – If True, include sine terms in the library.

  • include_cos (boolean, optional (default True)) – If True, include cosine terms in the library.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default 0)) – The indices to use for ensembling the library.

Attributes
  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is 2 * n_input_features_ * n_frequencies if both sines and cosines are included. Otherwise it is n_input_features * n_frequencies.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import FourierLibrary
>>> x = np.array([[0.],[1.],[2.]])
>>> lib = FourierLibrary(n_frequencies=2).fit(x)
>>> lib.transform(x)
array([[ 0.        ,  1.        ,  0.        ,  1.        ],
       [ 0.84147098,  0.54030231,  0.90929743, -0.41614684],
       [ 0.90929743, -0.41614684, -0.7568025 , -0.65364362]])
>>> lib.get_feature_names()
['sin(1 x0)', 'cos(1 x0)', 'sin(2 x0)', 'cos(2 x0)']
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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data to Fourier 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 Fourier features generated from the inputs.

Return type

np.ndarray, shape (n_samples, n_output_features)

class pysindy.feature_library.IdentityLibrary(library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

Generate an identity library which maps all input features to themselves.

Attributes
  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is equal to the number of input features.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import IdentityLibrary
>>> x = np.array([[0,-1],[0.5,-1.5],[1.,-2.]])
>>> lib = IdentityLibrary().fit(x)
>>> lib.transform(x)
array([[ 0. , -1. ],
       [ 0.5, -1.5],
       [ 1. , -2. ]])
>>> lib.get_feature_names()
['x0', 'x1']
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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Perform identity transformation (return a copy of the input).

Parameters

x (array-like, shape (n_samples, n_features)) – The data to transform, row by row.

Returns

x – The matrix of features, which is just a copy of the input data.

Return type

np.ndarray, shape (n_samples, n_features)

class pysindy.feature_library.PolynomialLibrary(degree=2, include_interaction=True, interaction_only=False, include_bias=True, order='C', library_ensemble=False, ensemble_indices=[0])[source]

Bases: PolynomialFeatures, BaseFeatureLibrary

Generate polynomial and interaction features.

This is the same as sklearn.preprocessing.PolynomialFeatures, but also adds the option to omit interaction features from the library.

Parameters
  • degree (integer, optional (default 2)) – The degree of the polynomial features.

  • include_interaction (boolean, optional (default True)) – Determines whether interaction features are produced. If false, features are all of the form x[i] ** k.

  • interaction_only (boolean, optional (default False)) – If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).

  • include_bias (boolean, optional (default True)) – 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).

  • order (str in {'C', 'F'}, optional (default 'C')) – Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

Attributes
  • powers_ (array, shape (n_output_features, n_input_features)) – powers_[i, j] is the exponent of the jth input in the ith output.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. This number is computed by iterating over all appropriately sized combinations of input features.

property powers_

Exponent for each of the inputs in the output.

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

fit(x_full, y=None)[source]

Compute number of output features.

Parameters

x (array-like, shape (n_samples, n_features)) – The data.

Returns

self

Return type

instance

transform(x_full)[source]

Transform data to polynomial features.

Parameters

x (array-like or CSR/CSC sparse matrix, shape (n_samples, n_features)) – The data to transform, row by row. Prefer CSR over CSC for sparse input (for speed), but CSC is required if the degree is 4 or higher. If the degree is less than 4 and the input format is CSC, it will be converted to CSR, have its polynomial features generated, then converted back to CSC. If the degree is 2 or 3, the method described in “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.

Returns

xp – shape (n_samples, n_output_features) The matrix of features, where n_output_features is the number of polynomial features generated from the combination of inputs.

Return type

np.ndarray or CSR/CSC sparse matrix,

class pysindy.feature_library.PDELibrary(library_functions=[], derivative_order=0, spatial_grid=None, temporal_grid=None, interaction_only=True, function_names=None, include_bias=False, include_interaction=True, library_ensemble=False, ensemble_indices=[0], implicit_terms=False, multiindices=None, differentiation_method=<class 'pysindy.differentiation.finite_difference.FiniteDifference'>, diff_kwargs={}, is_uniform=None, periodic=None)[source]

Bases: BaseFeatureLibrary

Generate a PDE library with custom functions.

Parameters
  • library_functions (list of mathematical functions, optional (default None)) – Functions to include in the library. Each function will be applied to each input variable (but not their derivatives)

  • derivative_order (int, optional (default 0)) – Order of derivative to take on each input variable, can be arbitrary non-negative integer.

  • spatial_grid (np.ndarray, optional (default None)) – The spatial grid for computing derivatives

  • temporal_grid (np.ndarray, optional (default None)) – The temporal grid if using SINDy-PI with PDEs.

  • 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.

  • 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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
  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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 PDELibrary
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

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

transform(x_full)[source]

Transform data to pde 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 the tensor product of the derivative terms and the library_functions applied to combinations of the inputs.

Return type

np.ndarray, shape (n_samples, n_output_features)

get_spatial_grid()[source]
class pysindy.feature_library.WeakPDELibrary(library_functions=[], derivative_order=0, spatiotemporal_grid=None, function_names=None, interaction_only=True, include_bias=False, include_interaction=True, K=100, H_xt=None, p=4, library_ensemble=False, ensemble_indices=[0], 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]

Bases: BaseFeatureLibrary

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
  • library_functions (list of mathematical functions, optional (default None)) – Functions to include in the library. Each function will be applied to each input variable (but not their derivatives)

  • 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.

  • 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.

  • 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

  • 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
  • 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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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)']
convert_u_dot_integral(u)[source]

Takes a full set of spatiotemporal fields u(x, t) and finds the weak form of u_dot.

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

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

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)

calc_trajectory(diff_method, x, t)[source]
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, library_ensemble=False, ensemble_indices=[0])[source]

Bases: BaseFeatureLibrary

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.BaseDifferentiation class. 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.

  • library_ensemble (boolean, optional (default False)) – Whether or not to use library bagging (regress on subset of the candidate terms in the library)

  • ensemble_indices (integer array, optional (default [0])) – The indices to use for ensembling the library.

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_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • 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']
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

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

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)

class pysindy.feature_library.ParameterizedLibrary(parameter_library=PolynomialLibrary(degree=1), feature_library=PolynomialLibrary(), num_parameters=3, num_features=3, library_ensemble=False, ensemble_indices=[0])[source]

Bases: GeneralizedLibrary

Construct a SINDyCP library to fit multiple trajectories with variable control parameters. The library is composed of a tensor product of a feature library, applied to the input data, and a parameter library, applied to the input control. If the input libraries are weak, the temporal derivatives are automatically rescaled by the appropriate domain volumes.

Parameters
  • feature_library (BaseFeatureLibrary, optional (default PolynomialLibrary).) –

  • features. (Specifies the library function to apply to the input control) –

  • parameter_library (BaseFeatureLibrary, optional (default PolynomialLibrary).) –

  • features.

  • num_features (int, optional (default 3)) –

  • data. (Specifies the number of features in the input) –

  • num_parameters (int, optional (default 3)) –

  • control. (Specifies the number of features in the input) –

Attributes
  • libraries_ (list of libraries) – Library instances to be applied to the input matrix. Equal to [parameter_library,feature_library].

  • tensor_array_ (2D list of booleans) – Indicates which pairs of libraries to tensor product together and add to the overall library. Equal to [0,1]

  • inputs_per_library_ (2D np.ndarray) – Can be used to specify a subset of the variables to use to generate a feature library. Value determined by num_parameters and num_features.

  • n_input_features_ (int) – The total number of input features. WARNING: This is deprecated in scikit-learn version 1.0 and higher so we check the sklearn.__version__ and switch to n_features_in if needed.

  • n_output_features_ (int) – The total number of output features. The number of output features is the sum of the numbers of output features for each of the concatenated libraries.

Examples

>>> import numpy as np
>>> from pysindy.feature_library import ParameterizedLibrary,PolynomialLibrary
>>> from pysindy import AxesArray
>>> xs=[np.random.random((5,3)) for n in range(3)]
>>> us=[np.random.random((5,3)) for n in range(3)]
>>> feature_lib=PolynomialLibrary(degree=3)
>>> parameter_lib=PolynomialLibrary(degree=1)
>>> lib=ParameterizedLibrary(feature_library=feature_lib,
>>>     parameter_library=parameter_lib,num_features=3,num_parameters=3)
>>> xus=[AxesArray(np.concatenate([xs[i],us[i]],axis=-1)) for i in range(3)]
>>> lib.fit(xus)
>>> lib.transform(xus)
calc_trajectory(diff_method, x, t)[source]