pykoopman.regression package

Module contents

class pykoopman.regression.PyDMDRegressor(regressor, tikhonov_regularization=None)[source]

Bases: BaseRegressor

Initializes a PyDMDRegressor instance.

Parameters:
  • regressor (DMDBase) – A regressor instance from DMDBase in pydmd.

  • tikhonov_regularization (bool or None, optional) – Indicates if Tikhonov regularization should be applied. Defaults to None.

Raises:

ValueError – If regressor is not a subclass of DMDBase from pydmd.

fit(x, y=None, dt=1)[source]

Fit the PyDMDRegressor model according to the given training data.

Parameters:
  • x (np.ndarray) – Measurement data input. Should be of shape (n_samples, n_features).

  • y (np.ndarray, optional) – Measurement data output to be fitted. Should be of shape (n_samples, n_features). Defaults to None.

  • dt (float, optional) – Time interval between x and y. Defaults to 1.

Returns:

Returns the instance itself.

Return type:

self

predict(x)[source]

Predict the future values based on the input measurement data.

Parameters:

x (np.ndarray) – Measurement data upon which to base the prediction. Should be of shape (n_samples, n_features).

Returns:

Predicted values of x one timestep in the future. The shape

is (n_samples, n_features).

Return type:

np.ndarray

property coef_

The weight vectors of the regression problem.

This method checks if the regressor is fitted before returning the coefficient.

Returns:

The coefficient matrix.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property state_matrix_

The DMD state transition matrix.

This method checks if the regressor is fitted before returning the state matrix.

Returns:

The state transition matrix.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property eigenvalues_

The identified Koopman eigenvalues.

This method checks if the regressor is fitted before returning the eigenvalues.

Returns:

The Koopman eigenvalues.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property eigenvectors_

The identified Koopman eigenvectors.

This method checks if the regressor is fitted before returning the eigenvectors.

Returns:

The Koopman eigenvectors.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property unnormalized_modes

The raw DMD V with each column as one DMD mode.

This method checks if the regressor is fitted before returning the unnormalized

modes.

Returns:

The unnormalized modes.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property ur

The left singular vectors ‘U’.

This method checks if the regressor is fitted before returning ‘U’.

Returns:

The left singular vectors ‘U’.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

class pykoopman.regression.EDMD(svd_rank=1.0, tlsq_rank=0)[source]

Bases: BaseRegressor

Initialize the EDMD regressor.

Parameters:
  • svd_rank (float) – Rank parameter for singular value decomposition. Default is 1.0.

  • tlsq_rank (int) – Rank parameter for total least squares. Default is 0.

fit(x, y=None, dt=None)[source]

Fit the EDMD regressor to the given data.

Parameters:
  • x (numpy.ndarray) – Measurement data to be fit.

  • y (numpy.ndarray, optional) – Time-shifted measurement data to be fit. Defaults to None.

  • dt (scalar, optional) – Discrete time-step. Defaults to None.

Returns:

Fitted EDMD instance.

Return type:

self

predict(x)[source]

Predict the next timestep based on the given data.

Parameters:

x (numpy.ndarray) – Measurement data upon which to base prediction.

Returns:

Prediction of x one timestep in the future.

Return type:

y (numpy.ndarray)

property coef_

Weight vectors of the regression problem. Corresponds to either [A] or [A,B].

property state_matrix_

The EDMD state transition matrix.

This method checks if the regressor is fitted before returning the state matrix.

Returns:

The state transition matrix.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property eigenvalues_

The identified Koopman eigenvalues.

This method checks if the regressor is fitted before returning the eigenvalues.

Returns:

The Koopman eigenvalues.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property eigenvectors_

The identified Koopman eigenvectors.

This method checks if the regressor is fitted before returning the eigenvectors.

Returns:

The Koopman eigenvectors.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property unnormalized_modes

The raw EDMD V with each column as one EDMD mode.

This method checks if the regressor is fitted before returning the unnormalized

modes. Note that this will combined with the measurement matrix from the observer to give you the true Koopman modes

Returns:

The unnormalized modes.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property ur

The left singular vectors ‘U’.

This method checks if the regressor is fitted before returning ‘U’.

Returns:

The left singular vectors ‘U’.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

class pykoopman.regression.KDMD(svd_rank=1.0, tlsq_rank=0, forward_backward=False, tikhonov_regularization=None, kernel=RBF(length_scale=1))[source]

Bases: BaseRegressor

Kernel Dynamic Mode Decomposition.

Parameters:
  • svd_rank (int, optional) – The rank for the truncation. If set to 0, the method computes the optimal rank and uses it for truncation. If set to a positive integer, the method uses the specified rank for truncation. If set to a float between 0 and 1, the rank is determined based on the specified energy level. If set to -1, no truncation is performed. Default is 1.0.

  • tlsq_rank (int, optional) – The rank for the truncation used in the total least squares preprocessing. If set to 0, no noise reduction is performed. If set to a positive integer, the method uses the specified rank for the SVD truncation in the TLSQ method. Default is 0.

  • forward_backward (bool, optional) – Whether to compute the low-rank operator using the forward-backward method similar to fbDMD. If set to True, the low-rank operator is computed with forward-backward DMD. If set to False, standard DMD is used. Default is False.

  • tikhonov_regularization (float or None, optional) – Tikhonov regularization parameter for regularization. If set to None, no regularization is applied. If set to a float, it is used as the regularization parameter. Default is None.

  • kernel (Kernel, optional) – An instance of the kernel class from sklearn.gaussian_process. Default is RBF().

fit(x, y=None, dt=1)[source]

Fits the KDMD model to the provided training data.

Parameters:
  • x – numpy.ndarray, shape (n_samples, n_features) Measurement data input.

  • y – numpy.ndarray, shape (n_samples, n_features), optional Measurement data output to be fitted. Defaults to None.

  • dt – float, optional Time interval between x and y. Defaults to 1.

Returns:

The fitted KDMD instance.

Return type:

KDMD

predict(x)[source]

Predicts the future states based on the given input data.

Parameters:

x – numpy.ndarray, shape (n_samples, n_features) Measurement data upon which to base the prediction.

Returns:

numpy.ndarray, shape (n_samples, n_features)

Prediction of the future states.

property coef_

Getter property for the coef_ attribute.

Returns:

numpy.ndarray, shape (svd_rank, svd_rank)

Reduced Koopman state transition matrix.

property state_matrix_

Getter property for the state_matrix_ attribute.

Returns:

numpy.ndarray, shape (svd_rank, svd_rank)

Reduced Koopman state transition matrix.

property eigenvalues_

Getter property for the eigenvalues_ attribute.

Returns:

numpy.ndarray, shape (svd_rank,)

Koopman eigenvalues.

property eigenvectors_

Getter property for the eigenvectors_ attribute.

Returns:

numpy.ndarray, shape (svd_rank, svd_rank)

Koopman eigenvectors.

property unnormalized_modes

Getter property for the unnormalized_modes attribute.

Returns:

numpy.ndarray, shape (svd_rank, n_input_features_)

Koopman unnormalized modes.

property ur

Getter property for the ur attribute.

Returns:

numpy.ndarray, shape (n_samples_, n_input_features_)

Linear matrix that maps kernel product features to eigenfunctions.

class pykoopman.regression.DMDc(svd_rank=None, svd_output_rank=None, input_control_matrix=None)[source]

Bases: BaseRegressor

Initialize a DMDc class object.

Parameters:
  • svd_rank (int, optional) – Rank of SVD for the input space. This determines the dimensionality of the projected state and control matrices. Defaults to None.

  • svd_output_rank (int, optional) – Rank of SVD for the output space. Defaults to None.

  • input_control_matrix (numpy.ndarray, optional) – The known input control matrix B. Defaults to None.

Raises:
  • ValueError – If svd_rank is not an integer.

  • ValueError – If svd_output_rank is not an integer.

  • ValueError – If input_control_matrix is not a numpy array.

fit(x, y=None, u=None, dt=None)[source]

Fit the DMDc model to the provided data.

Parameters:
  • x (numpy.ndarray, shape (n_samples, n_features)) – Measurement data to be fit.

  • y (numpy.ndarray, shape (n_samples, n_features), default=None) – Measurement data output to be fitted.

  • u (numpy.ndarray, shape (n_samples, n_control_features), optional, default=None) – Time series of external actuation/control.

  • dt (float, optional) – Time interval between X and Y

Returns:

self

Return type:

returns a fitted DMDc instance

predict(x, u)[source]

Predicts the future state of the system based on the current state and the current value of control input, using the fitted DMDc model.

Parameters:
  • x (numpy.ndarray) – The current state of the system.

  • u (numpy.ndarray) – The current value of the input.

Returns:

The predicted future state of the system.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the model is not fitted, raise this error to prevent misuse of the model.

property coef_

The weight vectors of the regression problem.

This method checks if the regressor is fitted before returning the coefficient.

Returns:

The coefficient matrix.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property state_matrix_

The DMD state transition matrix.

This method checks if the regressor is fitted before returning the state matrix.

Returns:

The state transition matrix.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property control_matrix_
property eigenvectors_

The identified Koopman eigenvectors.

This method checks if the regressor is fitted before returning the eigenvectors.

Returns:

The Koopman eigenvectors.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property eigenvalues_

The identified Koopman eigenvalues.

This method checks if the regressor is fitted before returning the eigenvalues.

Returns:

The Koopman eigenvalues.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property unnormalized_modes

The raw DMD V with each column as one DMD mode.

This method checks if the regressor is fitted before returning the unnormalized

modes.

Returns:

The unnormalized modes.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property ur

The left singular vectors ‘U’.

This method checks if the regressor is fitted before returning ‘U’.

Returns:

The left singular vectors ‘U’.

Return type:

numpy.ndarray

Raises:

NotFittedError – If the regressor is not fitted yet.

property input_control_matrix
class pykoopman.regression.EDMDc[source]

Bases: BaseRegressor

Initialize the EDMDc regressor.

fit(x, y=None, u=None, dt=None)[source]

Fit the EDMDc regressor to the given data.

Parameters:
  • x (numpy.ndarray) – Measurement data to be fit.

  • y (numpy.ndarray, optional) – Time-shifted measurement data to be fit. Defaults to None.

  • u (numpy.ndarray, optional) – Time series of external actuation/control. Defaults to None.

  • dt (scalar, optional) – Discrete time-step. Defaults to None.

Returns:

Fitted EDMDc instance.

Return type:

self

predict(x, u)[source]

Predict the next timestep based on the given data.

Parameters:
  • x (numpy.ndarray) – Measurement data upon which to base prediction.

  • u (numpy.ndarray) – Time series of external actuation/control.

Returns:

Prediction of x one timestep in the future.

Return type:

y (numpy.ndarray)

property coef_

Weight vectors of the regression problem. Corresponds to either [A] or [A,B].

property state_matrix_

Identified state transition matrix A of the underlying system.

Returns:

State transition matrix A.

Return type:

state_matrix (numpy.ndarray)

property control_matrix_

Identified control matrix B of the underlying system.

Returns:

Control matrix B.

Return type:

control_matrix (numpy.ndarray)

property eigenvalues_

Identified Koopman lambda.

Returns:

Koopman eigenvalues.

Return type:

eigenvalues (numpy.ndarray)

property eigenvectors_

Identified Koopman eigenvectors.

Returns:

Koopman eigenvectors.

Return type:

eigenvectors (numpy.ndarray)

property unnormalized_modes

Identified Koopman eigenvectors.

Returns:

Koopman eigenvectors.

Return type:

unnormalized_modes (numpy.ndarray)

property ur

Matrix U that is part of the SVD.

Returns:

Matrix U.

Return type:

ur (numpy.ndarray)

class pykoopman.regression.EnsembleBaseRegressor(regressor, func, inverse_func)[source]

Bases: TransformedTargetRegressor

This class serves as a wrapper for PyKoopman regressors that utilize ensemble or non-consecutive training data.

EnsembleBaseRegressor inherits from TransformedTargetRegressor and checks whether the provided regressor object implements the fit and predict methods.

Attributes:
  • regressor (sklearn.base.BaseEstimator) – A regressor object that implements fit and predict methods.

  • func (function) – A function to apply to the target y before passing it to the fit method. The function must return a 2-dimensional array. If func is None, the identity function is used.

  • inverse_func (function) – A function to apply to the prediction of the regressor. This function is used to return predictions to the same space as the original training labels. It must return a 2-dimensional array.

Raises:
  • AttributeError – If the regressor does not have a callable fit or predict method.

  • ValueError – If both transformer and functions func/inverse_func are set, or if ‘func’ is provided while ‘inverse_func’ is not.

Note

This class does not implement the fit method on its own, instead, it checks the methods of the provided regressor object and raises an AttributeError if the required methods are not present or not callable. It also performs some pre-processing on the target values y before fitting the regressor, and provides additional checks and warnings for the transformer and inverse functions.

fit(X, y, **fit_params)[source]

Fits the model according to the given training data.

Parameters:
  • X (array-like or sparse matrix of shape (n_samples, n_features)) – Training vector, where n_samples is the number of samples and n_features is the number of features.

  • y (array-like of shape (n_samples,)) – Target values.

  • **fit_params (dict) – Additional parameters passed to the fit method of the underlying regressor.

Returns:

The fitted estimator.

Return type:

self

Raises:

ValueError – If ‘transformer’ and functions ‘func’/’inverse_func’ are both set, or if ‘func’ is provided while ‘inverse_func’ is not.

Note

This method transforms the target y before fitting the regressor and performs additional checks and warnings for the transformer and inverse functions.

class pykoopman.regression.HAVOK(svd_rank=None, differentiator=Derivative(k=1, kind='finite_difference'), plot_sv=False)[source]

Bases: BaseRegressor

Initialize the HAVOK regressor.

Parameters:
  • svd_rank (int, optional) – Rank of the SVD used for model reduction. Defaults to None.

  • differentiator (Derivative, optional) – Differentiation method to compute the time derivative. Defaults to Derivative(kind=”finite_difference”, k=1).

  • plot_sv (bool, optional) – Whether to plot the singular values. Defaults to False.

fit(x, y=None, dt=None)[source]

Fit the HAVOK regressor to the given data.

Parameters:
  • x (numpy.ndarray) – Measurement data to be fit.

  • y (not used) – Time-shifted measurement data to be fit. Ignored.

  • dt (scalar) – Discrete time-step.

Returns:

Fitted HAVOK instance.

Return type:

self

predict(x, u, t)[source]

Predict the output based on the input data.

Parameters:
  • x (numpy.ndarray) – Measurement data upon which to base prediction.

  • u (numpy.ndarray) – Time series of external actuation/control, which is sampled at time instances in t.

  • t (numpy.ndarray) – Time vector. Instances at which the solution vector shall be provided. Note: The time vector must start at 0.

Returns:

Prediction of x at the time instances provided in t.

Return type:

y (numpy.ndarray)

property coef_

Get the weight vectors of the regression problem.

Returns:

Weight vectors of the regression problem. Corresponds to either [A] or [A,B].

Return type:

coef (numpy.ndarray)

property state_matrix_

Get the identified state transition matrix A of the underlying system.

Returns:

Identified state transition matrix A.

Return type:

state_matrix (numpy.ndarray)

property control_matrix_

Get the identified control matrix B of the underlying system.

Returns:

Identified control matrix B.

Return type:

control_matrix (numpy.ndarray)

property eigenvectors_

Get the identified eigenvectors of the state matrix A.

Returns:

Identified eigenvectors of the state matrix A.

Return type:

eigenvectors (numpy.ndarray)

property eigenvalues_

Get the identified eigenvalues of the state matrix A.

Returns:

Identified eigenvalues of the state matrix A.

Return type:

eigenvalues (numpy.ndarray)

property unnormalized_modes

Get the identified unnormalized modes.

Returns:

Identified unnormalized modes.

Return type:

unnormalized_modes (numpy.ndarray)

property ur

Get the matrix UR.

Returns:

Matrix UR.

Return type:

ur (numpy.ndarray)

class pykoopman.regression.NNDMD(mode=None, dt=1.0, look_forward=1, config_encoder={'activations': 'tanh', 'hidden_sizes': [32, 32], 'input_size': 2, 'output_size': 6}, config_decoder={'activations': 'linear', 'hidden_sizes': [32, 32], 'input_size': 6, 'output_size': 2}, batch_size=16, lbfgs=False, normalize=True, normalize_mode='equal', normalize_std_factor=2.0, std_koopman=0.1, trainer_kwargs={})[source]

Bases: BaseRegressor

Initializes the NNDMD model.

fit(x, y=None, dt=None)[source]

fit the NNDMD model with data x,y

Parameters:
  • x (np.ndarray or list) – The training input data. If a 2D numpy array, then it represents a single time-series and each row represents a state, otherwise it should be a list of 2D numpy arrays.

  • y (np.ndarray or list, optional) – The target output data, corresponding to x. If None, x is assumed to contain the target data in its second half. Defaults to None.

  • dt (float, optional) – The time step used to generate x. Defaults to None.

Returns:

None. The fitted model is stored in the class attribute _regressor.

predict(x, n=1)[source]

Predict the system state after n steps away from x_0 = x.

Parameters:
  • x (numpy.ndarray or torch.Tensor) – Input data of shape (n_samples, n_features).

  • n (int) – Number of steps to predict the system state into the future.

Returns:

Predicted system state after n steps, of shape

(n_samples, n_features).

Return type:

numpy.ndarray

Note

By default, the model is stored on the CPU for inference.

simulate(x, n_steps)[source]

Simulate the system forward in time for n_steps steps starting from x.

Parameters:
  • x (np.ndarray or torch.Tensor) – The initial state of the system.

  • array/tensor. (Should be a 2D) –

  • n_steps (int) – The number of time steps to simulate the system forward.

Returns:

The simulated states of the system. Will be of shape (n_steps+1, n_features).

Return type:

np.ndarray

property A

Returns the state transition matrix A of the NNDMD model.

Returns:

A – The state transition matrix of shape (n_states, n_states), where n_states is the number of states in the model.

Return type:

numpy.ndarray

property B
property C

Returns the matrix C representing the effective linear transformation from the observables to the Koopman modes. The matrix C is computed during the fit process as the product of the decoder weights of the trained autoencoder network.

Returns:

numpy.ndarray of shape (n_koopman, n_features)

The matrix C.

property W

Returns the matrix W representing the Koopman modes. The matrix W is computed during the fit process as the eigenvectors of the Koopman operator.

Returns:

numpy.ndarray of shape (n_koopman, n_koopman)

The matrix V, where each column represents a Koopman mode.

phi(x_col)[source]
psi(x_col)[source]
property coef_
property state_matrix_
property eigenvalues_
property eigenvectors_
property unnormalized_modes
property ur