Skip to content

Commit 8306e04

Browse files
DOC add example for make_narx (#33)
* DOC add example for basic of NARX
1 parent bcd6208 commit 8306e04

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

examples/plot_narx.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,23 @@
4444
# %%
4545
# Build term libriary
4646
# -------------------
47-
#
4847
# To build a polynomial NARX model, it is normally have two steps:
4948
#
50-
# 1. Search the structure of the model, i.e., the terms in the model, e.g.,
51-
# :math:`u_0(k-1)u_0(k-3)`, :math:`u_0(k-2)u_1(k-3)`, etc.
52-
# 2. Learn the coefficients before the terms.
49+
# #. Search the structure of the model, i.e., the terms in the model, e.g.,
50+
# :math:`u_0(k-1)u_0(k-3)`, :math:`u_0(k-2)u_1(k-3)`, etc.
51+
#
52+
# #. Learn the coefficients of the terms.
5353
#
5454
# To search the structure of the model, the candidate term libriary should be
55-
# constructed.
55+
# constructed by the following two steps.
56+
#
57+
# #. Time-shifted variables: the raw input-output data, i.e., :math:`u_0(k)`,
58+
# :math:`u_1(k)`, and :math:`y(k)`, are converted into :math:`u_0(k-1)`,
59+
# :math:`u_1(k-2)`, etc.
5660
#
57-
# 1. Time-shifted variables: the raw input-output data, i.e., :math:`u0(k)`,
58-
# :math:`u1(k)`, and :math:`y(k)`, are converted into :math:`u0(k-1)`, :math:`u1(k-2)`,
59-
# etc.
60-
# 2. Nonlinear terms: the time-shifted variables are onverted to nonlinear terms
61-
# via polynomial basis functions, e.g., :math:`u_0(k-1)^2`, :math:`u_0(k-1)u_0(k-3)`,
62-
# etc.
61+
# #. Nonlinear terms: the time-shifted variables are onverted to nonlinear terms
62+
# via polynomial basis functions, e.g., :math:`u_0(k-1)^2`,
63+
# :math:`u_0(k-1)u_0(k-3)`, etc.
6364
#
6465
# .. rubric:: References
6566
#
@@ -116,6 +117,8 @@
116117
# ----------------
117118
# As the polynomical NARX is a linear function of the nonlinear tems,
118119
# the coefficient of each term can be easily estimated by oridnary least squares.
120+
# In the printed NARX model, it is found that :class:`FastCan` selects the correct
121+
# terms and the coefficients are close to the true values.
119122

120123
from fastcan.narx import NARX, print_narx
121124

@@ -126,9 +129,25 @@
126129

127130
narx_model.fit(X, y)
128131

129-
# In the printed NARX model, it is found that :class:`FastCan` selects the correct
130-
# terms and the coefficients are close to the true values.
131132
print_narx(narx_model)
133+
# %%
134+
# Automaticated NARX modelling workflow
135+
# -------------------------------------
136+
# We provide :meth:`narx.make_narx` to automaticate the workflow above.
137+
138+
from fastcan.narx import make_narx
139+
140+
auto_narx_model = make_narx(
141+
X=X,
142+
y=y,
143+
n_features_to_select=4,
144+
max_delay=3,
145+
poly_degree=2,
146+
verbose=0,
147+
).fit(X, y)
148+
149+
print_narx(auto_narx_model)
150+
132151

133152
# %%
134153
# Plot NARX prediction performance

0 commit comments

Comments
 (0)