NARX#
- class fastcan.narx.NARX(*, feat_ids=None, delay_ids=None, output_ids=None, fit_intercept=True)#
The Nonlinear Autoregressive eXogenous (NARX) model class. For example, a (polynomial) NARX model is like y(k) = y(k-1)*u(k-1) + u(k-1)^2 + u(k-2) + 1.5 where y(k) is the system output at the k-th time step, u(k) is the system input at the k-th time step, u and y is called features, u(k-1) is called a (time shift) variable, u(k-1)^2 is called a (polynomial) term, and 1.5 is called an intercept.
- Parameters:
feat_ids (array-like of shape (n_terms, degree), default=None) – The unique id numbers of features to form polynomial terms. The id -1 stands for the constant 1. The id 0 to n_features_in_-1 are the input features. The id n_features_in_ to n_features_in_ + n_outputs_-1 are the output features. E.g., for a 2-input 1-output system, the feat_ids [[-1, 0], [-1, 1], [0, 2]] may represent the polynomial terms 1*u(k-1, 0), 1*u(k, 1), and u(k-1, 0)*y(k-2, 0).
delay_ids (array-like of shape (n_terms, degree), default=None) – The delays of each feature in polynomial terms. The id -1 stands for empty. The id 0 stands for 0 delay. The positive integer id k stands for k-th delay. E.g., for the polynomial terms 1*u(k-1, 0), 1*u(k, 1), and u(k-1, 0)*y(k-2, 0), the delay_ids [[-1, 1], [-1, 0], [1, 2]].
output_ids (array-like of shape (n_terms,), default=None) – The id numbers indicate which output the polynomial term belongs to. It is useful in multi-output case.
fit_intercept (bool, default=True) – Whether to fit the intercept. If set to False, intercept will be zeros.
- coef_#
Estimated coefficients for the linear regression problem.
- Type:
array of shape (n_terms,)
- intercept_#
Independent term in the linear model.
- Type:
array of shape (n_outputs_,)
- feature_names_in_#
Names of features seen during fit. Defined only when X has feature names that are all strings.
- Type:
ndarray of shape (n_features_in_,)
- max_delay_#
The maximum time delay of the time shift variables.
- Type:
int
- feat_ids_#
The unique id numbers of features to form polynomial terms. The id -1 stands for the constant 1.
- Type:
array-like of shape (n_terms, degree)
- delay_ids_#
The delays of each feature in polynomial terms. The id -1 stands for empty.
- Type:
array-like of shape (n_terms, degree)
References
- Billings, Stephen A. (2013).
Nonlinear System Identification: Narmax Methods in the Time, Frequency, and Spatio-Temporal Domains.
Examples
>>> import numpy as np >>> from fastcan.narx import NARX, print_narx >>> rng = np.random.default_rng(12345) >>> n_samples = 1000 >>> max_delay = 3 >>> e = rng.normal(0, 0.1, n_samples) >>> u = rng.uniform(0, 1, n_samples+max_delay) # input >>> y = np.zeros(n_samples+max_delay) # output >>> for i in range(max_delay, n_samples+max_delay): ... y[i] = 0.5*y[i-1] + 0.7*u[i-2] + 1.5*u[i-1]*u[i-3] + 1 >>> y = y[max_delay:]+e >>> X = u[max_delay:].reshape(-1, 1) >>> feat_ids = [[-1, 1], # 1*y ... [-1, 0], # 1*u ... [0, 0]] # u^2 >>> delay_ids = [[-1, 1], # 1*y(k-1) ... [-1, 2], # 1*u(k-2) ... [1, 3]] # u(k-1)*u(k-3) >>> narx = NARX(feat_ids=feat_ids, ... delay_ids=delay_ids).fit(X, y, coef_init="one_step_ahead") >>> print_narx(narx) | yid | Term | Coef | |-----|--------------------|----------| | 0 | Intercept | 1.008 | | 0 | y_hat[k-1,0] | 0.498 | | 0 | X[k-2,0] | 0.701 | | 0 | X[k-1,0]*X[k-3,0] | 1.496 |
- fit(X, y, sample_weight=None, coef_init=None, session_sizes=None, solver='least_squares', **params)#
Fit narx model.
- Parameters:
X (array-like of shape (n_samples, n_features_in_) or None) – Training data.
y (array-like of shape (n_samples,) or (n_samples, n_outputs_)) – Target values. Will be cast to X’s dtype if necessary.
sample_weight (array-like of shape (n_samples,), default=None) – Individual weights for each sample.
coef_init (array-like of shape (n_terms,), default=None) –
The initial values of coefficients and intercept for optimization. When coef_init is None, the model will be a One-Step-Ahead NARX. When coef_init is one_step_ahead, the model will be a Multi-Step-Ahead NARX whose coefficients and intercept are initialized by the a One-Step-Ahead NARX. When coef_init is an array, the model will be a Multi-Step-Ahead NARX whose coefficients and intercept are initialized by the array.
Note
When coef_init is one_step_ahead, the model will be trained as a Multi-Step-Ahead NARX, rather than a One-Step-Ahead NARX.
session_sizes (array-like of shape (n_sessions,), default=None) –
The sizes of measurement sessions for time-series. The sum of session_sizes should be equal to n_samples. If None, the whole data is treated as one session.
Added in version 0.5.0.
solver ({'least_squares', 'minimize'}, default='least_squares') –
The SciPy solver for optimization.
Added in version 0.5.1.
**params (dict) – Keyword arguments passed to
scipy.optimize.least_squares()orscipy.optimize.minimize().
- Returns:
self – Fitted Estimator.
- Return type:
object
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequestencapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
params – Parameter names mapped to their values.
- Return type:
dict
- predict(X, y_init=None)#
Predict using the linear model.
- Parameters:
X (array-like of shape (n_samples, n_features_in_) or int) – When X is an array, it is input data. When (nonlinear) AR model is adopted, where n_features_in_ is 0, X can be an integer, which indicates the total steps to predict.
y_init (array-like of shape (n_init,) or (n_init, n_outputs_), default=None) – The initial values for the prediction of y. It should at least have one sample.
- Returns:
y_hat – Returns predicted values. The number of dimensions is the same as that of y in fit.
- Return type:
array-like of shape (n_samples,) or (n_samples, n_outputs_)
- score(X, y, sample_weight=None)#
Return coefficient of determination on test data.
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_fittedis 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:
float
Notes
The \(R^2\) score used when calling
scoreon a regressor usesmultioutput='uniform_average'from version 0.23 to keep consistent with default value ofr2_score(). This influences thescoremethod of all the multioutput regressors (except forMultiOutputRegressor).
- set_fit_request(*, coef_init: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$', session_sizes: bool | None | str = '$UNCHANGED$', solver: bool | None | str = '$UNCHANGED$') NARX#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
coef_init (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
coef_initparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.session_sizes (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
session_sizesparameter infit.solver (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
solverparameter infit.
- Returns:
self – The updated object.
- Return type:
object
- 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_predict_request(*, y_init: bool | None | str = '$UNCHANGED$') NARX#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
y_init (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
y_initparameter inpredict.- Returns:
self – The updated object.
- Return type:
object
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') NARX#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif 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.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object