make_narx#

fastcan.narx.make_narx(X, y, n_terms_to_select, max_delay=1, poly_degree=1, *, fit_intercept=True, session_sizes=None, max_candidates=None, random_state=None, include_zero_delay=None, static_indices=None, lazy=False, refine_verbose=1, refine_drop=None, refine_max_iter=None, **params)#

Find time_shift_ids, poly_ids, output_ids for a NARX model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Target vector or matrix.

  • n_terms_to_select (int or array-like of shape (n_outputs,)) – The parameter is the absolute number of polynomial terms to select for each output. If n_terms_to_select is an integer, it is the same for all outputs.

  • max_delay (int, default=1) – The maximum delay of time shift features.

  • poly_degree (int, default=1) – The maximum degree of polynomial features.

  • fit_intercept (bool, default=True) – Whether to fit the intercept. If set to False, intercept will be zeros.

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

  • max_candidates (int, default=None) – Maximum number of candidate polynomial terms retained before selection. Randomly selected by reservoir sampling. If None, all candidates are considered.

  • random_state (int or RandomState instance, default=None) – Used when max_candidates is not None to subsample candidate terms. See Glossary for details.

  • include_zero_delay ({None, array-like} of shape (n_features,) default=None) – Whether to include the original (zero-delay) features.

  • static_indices ({None, array-like} of shape (n_static_features,) default=None) –

    The indices of static features without time delay.

    Note

    If the corresponding include_zero_delay of the static features is False, the static feature will be excluded from candidate features.

  • lazy (bool, default=False) –

    Whether to use LazyFastCan for selection. If False, FastCan will be used.

    Added in version 0.5.1.

  • refine_verbose (int, default=1) – The verbosity level of refine. Only available when lazy is False.

  • refine_drop (int or "all", default=None) – The number of the selected features dropped for the consequent reselection. If drop is None, no refining will be performed. Only available when lazy is False.

  • refine_max_iter (int, default=None) – The maximum number of valid iterations in the refining process. Only available when lazy is False.

  • **params (dict) – Keyword arguments passed to fastcan.FastCan. Only available when lazy is False.

Returns:

narx – NARX instance.

Return type:

NARX

Examples

>>> import numpy as np
>>> from sklearn.metrics import mean_squared_error
>>> from fastcan.narx import make_narx, print_narx
>>> rng = np.random.default_rng(12345)
>>> n_samples = 1000
>>> max_delay = 3
>>> e = rng.normal(0, 0.1, n_samples)
>>> u0 = rng.uniform(0, 1, n_samples+max_delay) # input
>>> u1 = rng.normal(0, 0.1, n_samples) # static input (i.e. no time shift)
>>> 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.3*u0[i]**2 + 2*u0[i-1]*u0[i-3] +
...             1.5*u0[i-2]*u1[i-max_delay] + 1)
>>> y = y[max_delay:]+e
>>> X = np.c_[u0[max_delay:], u1]
>>> narx = make_narx(X=X,
...     y=y,
...     n_terms_to_select=4,
...     max_delay=3,
...     poly_degree=2,
...     static_indices=[1],
...     eta=True,
...     verbose=0,
...     refine_verbose=0,
...     refine_drop=1)
>>> print(f"{mean_squared_error(y, narx.fit(X, y).predict(X)):.4f}")
0.0289
>>> print_narx(narx)
| yid |        Term        |   Coef   |
|-----|--------------------|----------|
|  0  |     Intercept      |  1.050   |
|  0  |    y_hat[k-1,0]    |  0.484   |
|  0  |   X[k,0]*X[k,0]    |  0.306   |
|  0  | X[k-1,0]*X[k-3,0]  |  2.000   |
|  0  |  X[k-2,0]*X[k,1]   |  1.528   |