pykoopman.observables package¶
Module contents¶
- class pykoopman.observables.CustomObservables(observables, observable_names=None, interaction_only=True)[source]¶
Bases:
BaseObservables
Initialize a CustomObservables instance.
- Parameters:
observables (list of callable) – List of functions mapping state variables to observables. Univariate functions are applied to each state variable, and multivariable functions are applied to combinations of state variables. The identity map is automatically included in this list.
observable_names (list of callable, optional) – List of functions mapping from names of state variables to names of observables. For example, the observable name lambda x: f”{x}^2” would correspond to the function x^2. If None, the names “f0(…)”, “f1(…)”, … will be used. Default is None.
interaction_only (bool, optional) – If True, omits self-interaction terms. Function evaluations 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. Default is True.
- fit(x, y=None)[source]¶
Fit the model to the measurement data.
This method calculates the number of input and output features and generates default values for ‘observable_names’ if necessary. It also prepares the measurement matrix for data transformation.
- Parameters:
x (array-like, shape (n_samples, n_input_features)) – Measurement data to be fitted.
y (None) – This is a dummy parameter added for compatibility with sklearn’s API. Default is None.
- Returns:
This method returns the fitted instance.
- Return type:
self (CustomObservables)
- transform(x)[source]¶
Apply custom transformations to data, computing observables.
This method applies the user-defined observables functions to the input data, effectively transforming the state variables into observable ones.
- Parameters:
x (array-like, shape (n_samples, n_input_features)) – The measurement data to be transformed.
- Returns:
- The
transformed data, i.e., the computed observables.
- Return type:
x_transformed (array-like, shape (n_samples, n_output_features))
- get_feature_names(input_features=None)[source]¶
Get the names of the output features.
This method returns the names of the output features as defined by the observable functions. If names for the input features are provided, they are used in the output feature names. Otherwise, default names (“x0”, “x1”, …, “xn_input_features”) are used.
- Parameters:
input_features (list of string, length n_input_features, optional) – String names for input features, if available. By default, the names “x0”, “x1”, … ,”xn_input_features” are used.
- Returns:
Output feature names.
- Return type:
output_feature_names (list of string, length n_output_features)
- class pykoopman.observables.Identity[source]¶
Bases:
BaseObservables
Initialize the Identity class.
This constructor initializes the Identity class which simply returns its input when transformed.
- fit(x, y=None)[source]¶
Fit the model to the provided measurement data.
- Parameters:
x (array-like) – The measurement data to be fit. It must have a shape of (n_samples, n_input_features).
y (None) – This parameter is retained for sklearn compatibility.
- Returns:
Returns a fit instance of the class
pykoopman.observables.Identity
.- Return type:
self
- transform(x)[source]¶
Apply Identity transformation to the provided data.
- Parameters:
x (array-like) – The measurement data to be transformed. It must have a shape of (n_samples, n_input_features).
- Returns:
- Returns the transformed data which is the same as the input
data in this case.
- Return type:
array-like
- get_feature_names(input_features=None)[source]¶
Get the names of the output features.
- Parameters:
input_features (list of string, optional) – The string names for input features, if available. By default, the names “x0”, “x1”, … , “xn_input_features” are used.
- Returns:
Returns the output feature names.
- Return type:
list of string
- class pykoopman.observables.Polynomial(degree=2, interaction_only=False, include_bias=True, order='C')[source]¶
Bases:
PolynomialFeatures
,BaseObservables
Initialize the Polynomial object.
- Parameters:
degree (int, optional) – The degree of the polynomial features. Default is 2.
interaction_only (bool, optional) – If True, only interaction features are produced: features that are products of at most
degree
distinct input features (so notx[1] ** 2
,x[0] * x[2] ** 3
, etc.). Default is False.include_bias (bool, optional) – If True, 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). Default is True.
order (str in {'C', 'F'}, optional) – Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators. Default is ‘C’.
- Raises:
ValueError – If degree is less than 1.
- fit(x, y=None)[source]¶
Compute number of output features.
This method fits the
Polynomial
instance to the input datax
. It calls thefit
method of the superclass (PolynomialFeatures
fromsklearn.preprocessing
), which computes the number of output features based on the degree of the polynomial and the interaction_only flag. It also setsn_input_features_
andn_output_features_
attributes. Then, it initializesmeasurement_matrix_
as a zero matrix of sizen_input_features_
byn_output_features_
and sets the main diagonal to 1, depending on theinclude_bias
attribute. The inputy
is not used in this method; it is only included to maintain compatibility with the usual interface offit
methods in scikit-learn.- Parameters:
x (np.ndarray) – The measurement data to be fit, with shape (n_samples, n_features).
y (array-like, optional) – Dummy input. Defaults to None.
- Returns:
A fit instance of
Polynomial
.- Return type:
self
- Raises:
ValueError – If the input data is not valid.
- transform(x)[source]¶
Transforms the data to polynomial features.
This method transforms the data
x
into polynomial features. It first checks if the fit method has been called by checking then_input_features_
attribute, then it validates the inputx
. Ifx
is a CSR sparse matrix and the degree is less than 4, it uses a method based on “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes. Ifx
is a CSC sparse matrix and the degree is less than 4, it convertsx
to CSR, generates the polynomial features, then converts back to CSC. For dense arrays or CSC sparse matrix with a degree of 4 or more, it generates the polynomial features through a slower process.- Parameters:
x (array-like or CSR/CSC sparse matrix) – The data to transform, row by row.
be (The shape should) –
input (sparse) –
- Returns:
The matrix of features, where n_output_features is the number of polynomial features generated from the combination of inputs. The shape is (n_samples, n_output_features).
- Return type:
xp (np.ndarray or CSR/CSC sparse matrix)
- Raises:
ValueError – If the input data is not valid or the shape of
x
does notmatch training shape. –
- property powers_¶
Get the exponent for each of the inputs in the output.
This property method returns the exponents for each input feature in the polynomial output. It first checks whether the model has been fitted, then uses the
_combinations
method to get the combinations of features, and finally calculates the exponents for each input feature.- Returns:
A 2D array where each row represents a feature and each column represents an output of the polynomial transformation. The values are the exponents of the input features.
- Return type:
np.ndarray
- Raises:
NotFittedError – If the model has not been fitted.
- class pykoopman.observables.RadialBasisFunction(rbf_type='gauss', n_centers=10, centers=None, kernel_width=1.0, polyharmonic_coeff=1.0, include_state=True)[source]¶
Bases:
BaseObservables
Initialize a BaseObservables instance.
Initializes the parent classes with the super function.
- fit(x, y=None)[source]¶
Initializes the RadialBasisFunction with specified parameters.
- Parameters:
rbf_type (str, optional) – The type of radial basis functions to be used. Options are: ‘gauss’, ‘thinplate’, ‘invquad’, ‘invmultquad’, ‘polyharmonic’. Defaults to ‘gauss’.
n_centers (int, optional) – The number of centers to compute RBF with. Must be a non-negative integer. Defaults to 10.
centers (numpy array, optional) – The centers to compute RBF with. If provided, it should have a shape of (n_input_features, n_centers). Defaults to None, in which case the centers are uniformly distributed over input data.
kernel_width (float, optional) – The kernel width for Gaussian RBFs. Must be a non-negative float. Defaults to 1.0.
polyharmonic_coeff (float, optional) – The polyharmonic coefficient for polyharmonic RBFs. Must be a non-negative float. Defaults to 1.0.
include_state (bool, optional) – Whether to include the input coordinates as additional coordinates in the observable. Defaults to True.
- Raises:
TypeError – If rbf_type is not a string, n_centers is not an int, or include_state is not a bool.
ValueError – If n_centers, kernel_width or polyharmonic_coeff is negative, rbf_type is not of available type, or centers is provided but n_centers is not equal to centers.shape[1].
- transform(x)[source]¶
Apply radial basis function transformation to the data.
- Parameters:
x (array-like) – Measurement data to be transformed, with shape (n_samples, n_input_features). It is assumed that rows correspond to examples, which are not required to be equi-spaced in time or in sequential order.
- Returns:
Transformed data, with shape (n_samples, n_output_features).
- Return type:
array-like
- Raises:
NotFittedError – If the ‘fit’ method has not been called before the ‘transform’ method.
ValueError – If the number of features in ‘x’ does not match the number of input features expected by the transformer.
- get_feature_names(input_features=None)[source]¶
Get the names of the output features.
- Parameters:
input_features (list of str, optional) – String names for input features, if available. By default, the names “x0”, “x1”, … , “xn_input_features” are used.
- Returns:
Output feature names.
- Return type:
list of str
- Raises:
NotFittedError – If the ‘fit’ method has not been called before the ‘get_feature_names’ method.
ValueError – If the length of ‘input_features’ does not match the number of input features expected by the transformer.
- class pykoopman.observables.RandomFourierFeatures(include_state=True, gamma=1.0, D=100, random_state=None)[source]¶
Bases:
BaseObservables
Initialize the RandomFourierFeatures class with given parameters.
- Parameters:
include_state (bool, optional) – If True, includes the system state. Defaults to True.
gamma (float, optional) – The scale of the Gaussian kernel. Defaults to 1.0.
D (int, optional) – The number of random samples in Monte Carlo approximation. Defaults to 100.
random_state (int or None, optional) – The seed of the random number for repeatable experiments. Defaults to None.
- fit(x, y=None)[source]¶
Set up observable.
- Parameters:
x (numpy.ndarray) – Measurement data to be fit. Shape (n_samples, n_input_features_).
y (numpy.ndarray, optional) – Time-shifted measurement data to be fit. Defaults to None.
- Returns:
Returns a fitted RandomFourierFeatures instance.
- Return type:
self
- transform(x)[source]¶
Evaluate observable at
x
.- Parameters:
x (numpy.ndarray) – Measurement data to be fit. Shape (n_samples, n_input_features_).
- Returns:
- Evaluation of observables at
x
. Shape (n_samples,
- Evaluation of observables at
- Return type:
y (numpy.ndarray)
- get_feature_names(input_features=None)[source]¶
Return names of observables.
- Parameters:
input_features (list of string of length n_features, optional) – Default list is “x0”, “x1”, …, “xn”, where n = n_features.
- Returns:
Returns a list of observable names.
- Return type:
output_feature_names (list of string of length n_output_features)
- class pykoopman.observables.TimeDelay(delay=1, n_delays=2)[source]¶
Bases:
BaseObservables
Initialize the TimeDelay class with given parameters.
- Parameters:
delay (int, optional) – The length of each delay. Defaults to 1.
n_delays (int, optional) – The number of delays to compute for each variable. Defaults to 2.
- Raises:
ValueError – If delay or n_delays are negative.
- fit(x, y=None)[source]¶
Fit the model to measurement data.
- Parameters:
x (array-like) – The input data, shape (n_samples, n_input_features).
y (None) – Dummy parameter for sklearn compatibility.
- Returns:
The fitted instance.
- Return type:
- transform(x)[source]¶
Add time-delay features to the data, dropping the first
delay - n_delays
samples.- Parameters:
x (array-like) – The input data, shape (n_samples, n_input_features). It is assumed that rows correspond to examples that are equi-spaced in time and are in sequential order.
- Returns:
The transformed data, shape (n_samples - delay * n_delays, n_output_features).
- Return type:
y (array-like)
- get_feature_names(input_features=None)[source]¶
Get the names of the output features.
- Parameters:
input_features (list of str, optional) – Names for input features. If None, defaults to “x0”, “x1”, … ,”xn_input_features”.
- Returns:
Names of the output features.
- Return type:
list of str
- property n_consumed_samples¶
The number of samples that are consumed as “initial conditions” for other samples, i.e., the number of samples for which time delays cannot be computed.
- Returns:
The number of consumed samples.
- Return type:
int