pykoop.lmi_regressors.LmiHinfZpkMeta
- class LmiHinfZpkMeta(hinf_regressor=None, type='post', zeros=None, poles=None, gain=1, discretization='bilinear', t_step=1, units='rad/s')
Bases:
BaseEstimator
,RegressorMixin
Meta-estimator where H-infinity weight is specified in ZPK format.
H-infinity regularization weights must normally be specified in discrete-time state space format. This can make cross-validating pole or zero positions annoying. This meta-estimator wraps
LmiEdmdHinfReg
orLmiDmdcHinfReg
and allows the weighting filter to be specified using zeros, poles, and a gain (i.e., ZPK format) in continuous-time.All attributes with a trailing underscore are set by
fit()
.- Parameters:
- hinf_regressor_
Fit internal regressor.
- ss_ct_
Continuous-times state space weight.
- Type:
- ss_dt_
Discrete-times state space weight.
- Type:
- feature_names_in_
Array of input feature name strings.
- Type:
np.ndarray
- coef_
Fit coefficient matrix.
- Type:
np.ndarray
- __init__(hinf_regressor=None, type='post', zeros=None, poles=None, gain=1, discretization='bilinear', t_step=1, units='rad/s')
Instantiate
LmiHinfZpkMeta
.- Parameters:
hinf_regressor (Optional[koopman_pipeline.KoopmanRegressor]) – Instance of
LmiEdmdHinfReg
orLmiDmdcHinfReg
.type (str) – Type of weight (
'pre'
or'post'
).zeros (Union[float, np.ndarray, None]) – Filter zeros. If
None
, no zeros are used. Accepts scalar input if only one zero is required.poles (Union[float, np.ndarray, None]) – Filter poles. If
None
, no poles are used. Accepts scalar input if only one pole is required.gain (float) – Filter gain.
discretization (str) –
Discretization method supported by
scipy.signal.cont2discrete()
(except'gbt'
). Specifically, possible valued are'bilinear'
– Tustin’s approximation (recommended),'euler'
– Euler (or forward differencing) method,'backward_diff'
– backwards differencing method,'zoh'
– zero-order hold method,'foh'
– first-order hold method, or'impulse'
– equivalent impulse response method.
t_step (float) – Timestep beween samples. Used for discretization.
units (str) –
Units of poles and zeros. Possible values are
'rad/s'
– radians per second,'hz'
– Hertz, or'normalized'
– normalized, where 1 is the Nyquist frequency.
- Return type:
None
Notes
The zeros and poles in the weight should usually have negative real parts! If you want a pole at
10 rad/s
, thenpoles
must be-10
.Examples
>>> est = pykoop.lmi_regressors.LmiHinfZpkMeta( ... hinf_regressor=pykoop.lmi_regressors.LmiEdmdHinfReg(), ... type='post', ... zeros=-0, ... poles=-5, ... gain=1, ... discretization='bilinear', ... t_step=0.1, ... ) >>> est.fit(X_msd, n_inputs=1, episode_feature=True) LmiHinfZpkMeta(hinf_regressor=LmiEdmdHinfReg(), poles=-5, t_step=0.1, zeros=0)
Methods
__init__
([hinf_regressor, type, zeros, ...])Instantiate
LmiHinfZpkMeta
.fit
(X[, y, n_inputs, episode_feature])Fit the regressor.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Perform a single-step prediction for each state in each episode.
score
(X, y[, sample_weight])Return the coefficient of determination of the prediction.
set_fit_request
(*[, episode_feature, n_inputs])Request metadata passed to the
fit
method.set_params
(**params)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.- fit(X, y=None, n_inputs=0, episode_feature=False)
Fit the regressor.
If only
X
is specified, the regressor will compute its unshifted and shifted versions. IfX
andy
are specified,X
is treated as the unshifted data matrix, whiley
is treated as the shifted data matrix.- Parameters:
X (np.ndarray) – Full data matrix if
y=None
. Unshifted data matrix ify
is specified.y (Optional[np.ndarray]) – Optional shifted data matrix. If
None
, shifted data matrix is computed usingX
.n_inputs (int) – Number of input features at the end of
X
.episode_feature (bool) – True if first feature indicates which episode a timestep is from.
- Returns:
Instance of itself.
- Return type:
- Raises:
ValueError – If constructor or fit parameters are incorrect.
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequest
encapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)
Get parameters for this estimator.
- predict(X)
Perform a single-step prediction for each state in each episode.
- Parameters:
X (np.ndarray) – Data matrix.
- Returns:
Predicted data matrix.
- Return type:
np.ndarray
- score(X, y, sample_weight=None)
Return the coefficient of determination of the prediction.
The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()
and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum()
. The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted)
, wheren_samples_fitted
is the number of samples used in the fitting for the estimator.y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score – \(R^2\) of
self.predict(X)
w.r.t. y.- Return type:
Notes
The \(R^2\) score used when calling
score
on a regressor usesmultioutput='uniform_average'
from version 0.23 to keep consistent with default value ofr2_score()
. This influences thescore
method of all the multioutput regressors (except forMultiOutputRegressor
).
- set_fit_request(*, episode_feature='$UNCHANGED$', n_inputs='$UNCHANGED$')
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
episode_feature (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
episode_feature
parameter infit
.n_inputs (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
n_inputs
parameter infit
.self (LmiHinfZpkMeta)
- Returns:
self – The updated object.
- Return type:
- set_params(**params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_score_request(*, sample_weight='$UNCHANGED$')
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weight
parameter inscore
.self (LmiHinfZpkMeta)
- Returns:
self – The updated object.
- Return type: