Identification of an electromechanical system using Entropic Regression¶
Example created by Wilson Rocha Lacerda Junior
More details about this data can be found in the following paper (in Portuguese): https://www.researchgate.net/publication/320418710_Identificacao_de_um_motorgerador_CC_por_meio_de_modelos_polinomiais_autorregressivos_e_redes_neurais_artificiais
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 ER
from sysidentpy.basis_function._basis_function import Polynomial
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.utils.generate_data import get_siso_data
from sysidentpy.utils.display_results import results
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import (
compute_residues_autocorrelation,
compute_cross_correlation,
)
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sysidentpy.model_structure_selection import ER from sysidentpy.basis_function._basis_function import Polynomial from sysidentpy.metrics import root_relative_squared_error from sysidentpy.utils.generate_data import get_siso_data from sysidentpy.utils.display_results import results from sysidentpy.utils.plotting import plot_residues_correlation, plot_results from sysidentpy.residues.residues_correlation import ( compute_residues_autocorrelation, compute_cross_correlation, )
In [2]:
Copied!
df1 = pd.read_csv("examples/datasets/x_cc.csv")
df2 = pd.read_csv("examples/datasets/y_cc.csv")
df1 = pd.read_csv("examples/datasets/x_cc.csv") df2 = pd.read_csv("examples/datasets/y_cc.csv")
In [4]:
Copied!
# we will decimate the data using d=500 in this example
x_train, x_valid = np.split(df1.iloc[::500].values, 2)
y_train, y_valid = np.split(df2.iloc[::500].values, 2)
# we will decimate the data using d=500 in this example x_train, x_valid = np.split(df1.iloc[::500].values, 2) y_train, y_valid = np.split(df2.iloc[::500].values, 2)
Building a Polynomial NARX model using Entropic Regression Algorithm¶
In [5]:
Copied!
basis_function = Polynomial(degree=2)
model = ER(
ylag=6,
xlag=6,
n_perm=2,
k=2,
skip_forward=True,
estimator="recursive_least_squares",
basis_function=basis_function,
)
basis_function = Polynomial(degree=2) model = ER( ylag=6, xlag=6, n_perm=2, k=2, skip_forward=True, estimator="recursive_least_squares", basis_function=basis_function, )
c:\Users\wilso\Desktop\projects\GitHub\sysidentpy\sysidentpy\utils\deprecation.py:37: FutureWarning: Passing a string to define the estimator will rise an error in v0.4.0. You'll have to use ER(estimator=LeastSquares()) instead. The only change is that you'll have to define the estimator first instead of passing a string like 'least_squares'. This change will make easier to implement new estimators and it'll improve code readability. warnings.warn(message, FutureWarning)
In [6]:
Copied!
model.fit(X=x_train, y=y_train)
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)
ee = compute_residues_autocorrelation(y_valid, yhat)
plot_residues_correlation(data=ee, title="Residues", ylabel="$e^2$")
x1e = compute_cross_correlation(y_valid, yhat, x_valid)
plot_residues_correlation(data=x1e, title="Residues", ylabel="$x_1e$")
model.fit(X=x_train, y=y_train) 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) ee = compute_residues_autocorrelation(y_valid, yhat) plot_residues_correlation(data=ee, title="Residues", ylabel="$e^2$") x1e = compute_cross_correlation(y_valid, yhat, x_valid) plot_residues_correlation(data=x1e, title="Residues", ylabel="$x_1e$")
C:\Users\wilso\AppData\Local\Temp/ipykernel_35768/1917592845.py:1: UserWarning: Given the higher number of possible regressors (91), the Entropic Regression algorithm may take long time to run. Consider reducing the number of regressors model.fit(X=x_train, y=y_train)
0.026821345664204566 Regressors Parameters ERR 0 1 -5.0893E+02 0.00000000E+00 1 y(k-1) 9.4303E-01 0.00000000E+00 2 y(k-5) -5.8675E-02 0.00000000E+00 3 x1(k-2) 5.2611E+02 0.00000000E+00 4 x1(k-5) -4.8028E+01 0.00000000E+00 5 x1(k-6) -8.2871E+00 0.00000000E+00 6 y(k-1)^2 2.2206E-04 0.00000000E+00 7 y(k-2)y(k-1) -3.8730E-04 0.00000000E+00 8 y(k-3)y(k-1) 7.0514E-05 0.00000000E+00 9 y(k-5)y(k-1) 1.3006E-05 0.00000000E+00 10 x1(k-1)y(k-1) -1.6708E-01 0.00000000E+00 11 x1(k-2)y(k-1) -1.5839E-01 0.00000000E+00 12 x1(k-4)y(k-1) 1.1017E-02 0.00000000E+00 13 y(k-2)^2 1.6803E-04 0.00000000E+00 14 y(k-3)y(k-2) -5.1021E-05 0.00000000E+00 15 y(k-4)y(k-2) -6.8171E-06 0.00000000E+00 16 x1(k-1)y(k-2) 1.1750E-01 0.00000000E+00 17 x1(k-2)y(k-2) 6.9008E-02 0.00000000E+00 18 x1(k-3)y(k-2) -7.1654E-03 0.00000000E+00 19 x1(k-4)y(k-2) -1.6124E-02 0.00000000E+00 20 x1(k-5)y(k-2) 8.1489E-03 0.00000000E+00 21 x1(k-6)y(k-2) 1.1681E-03 0.00000000E+00 22 x1(k-1)y(k-3) -4.7176E-02 0.00000000E+00 23 x1(k-1)y(k-4) 1.7867E-02 0.00000000E+00 24 x1(k-4)y(k-4) 2.4316E-03 0.00000000E+00 25 x1(k-1)y(k-5) -7.1066E-03 0.00000000E+00 26 x1(k-2)y(k-5) -3.2368E-03 0.00000000E+00 27 x1(k-1)y(k-6) 2.5080E-03 0.00000000E+00 28 x1(k-1)^2 1.1453E+02 0.00000000E+00 29 x1(k-2)x1(k-1) -1.1003E+00 0.00000000E+00 30 x1(k-4)x1(k-1) -9.2887E-01 0.00000000E+00 31 x1(k-3)x1(k-2) 4.7982E+00 0.00000000E+00 32 x1(k-4)x1(k-3) 2.5953E+00 0.00000000E+00