Multiple Inputs usage¶
Example created by Wilson Rocha Lacerda Junior
Generating 2 input 1 output sample data¶
The data is generated by simulating the following model:
$y_k = 0.4y_{k-1}^2 + 0.1y_{k-1}x1_{k-1} + 0.6x2_{k-1} -0.3x1_{k-1}x2_{k-2} + e_{k}$
If colored_noise is set to True:
$e_{k} = 0.8\nu_{k-1} + \nu_{k}$
where $x$ is a uniformly distributed random variable and $\nu$ is a gaussian distributed variable with $\mu=0$ and $\sigma=0.001$
In [ ]:
Copied!
pip install sysidentpy
pip install sysidentpy
In [1]:
Copied!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sysidentpy.model_structure_selection import FROLS
from sysidentpy.basis_function._basis_function import Polynomial
from sysidentpy.parameter_estimation import LeastSquares
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.utils.display_results import results
from sysidentpy.utils.plotting import plot_results
from sysidentpy.utils.generate_data import get_miso_data
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sysidentpy.model_structure_selection import FROLS from sysidentpy.basis_function._basis_function import Polynomial from sysidentpy.parameter_estimation import LeastSquares from sysidentpy.metrics import root_relative_squared_error from sysidentpy.utils.display_results import results from sysidentpy.utils.plotting import plot_results from sysidentpy.utils.generate_data import get_miso_data
In [2]:
Copied!
x_train, x_valid, y_train, y_valid = get_miso_data(
n=1000, colored_noise=False, sigma=0.001, train_percentage=90
)
x_train, x_valid, y_train, y_valid = get_miso_data( n=1000, colored_noise=False, sigma=0.001, train_percentage=90 )
There is a specific difference for multiple input data.
- You have to pass the lags for each input in a nested list (e.g., [[1, 2], [1, 2]])
The remainder settings remains the same.
Build the model¶
In [7]:
Copied!
basis_function = Polynomial(degree=2)
estimator = LeastSquares()
model = FROLS(
order_selection=True,
n_terms=4,
ylag=2,
xlag=[[1, 2], [1, 2]],
info_criteria="aic",
estimator=estimator,
basis_function=basis_function,
err_tol=None,
)
basis_function = Polynomial(degree=2) estimator = LeastSquares() model = FROLS( order_selection=True, n_terms=4, ylag=2, xlag=[[1, 2], [1, 2]], info_criteria="aic", estimator=estimator, basis_function=basis_function, err_tol=None, )
In [8]:
Copied!
model.fit(X=x_train, y=y_train)
model.fit(X=x_train, y=y_train)
Out[8]:
<sysidentpy.model_structure_selection.forward_regression_orthogonal_least_squares.FROLS at 0x1a88cc17350>
Model evaluation¶
In [11]:
Copied!
yhat = model.predict(X=x_valid, y=y_valid)
rrse = root_relative_squared_error(y_valid, yhat)
print(rrse)
r = pd.DataFrame(
results(
model.final_model,
model.theta,
model.err,
model.n_terms,
err_precision=8,
dtype="sci",
),
columns=["Regressors", "Parameters", "ERR"],
)
print(r)
plot_results(y=y_valid, yhat=yhat, n=1000)
yhat = model.predict(X=x_valid, y=y_valid) rrse = root_relative_squared_error(y_valid, yhat) print(rrse) r = pd.DataFrame( results( model.final_model, model.theta, model.err, model.n_terms, err_precision=8, dtype="sci", ), columns=["Regressors", "Parameters", "ERR"], ) print(r) plot_results(y=y_valid, yhat=yhat, n=1000)
In [12]:
Copied!
xaxis = np.arange(1, model.n_info_values + 1)
plt.plot(xaxis, model.info_values)
plt.xlabel("n_terms")
plt.ylabel("Information Criteria")
xaxis = np.arange(1, model.n_info_values + 1) plt.plot(xaxis, model.info_values) plt.xlabel("n_terms") plt.ylabel("Information Criteria")