wilsonrljr / sysidentpy

A Python Package For System Identification Using NARMAX Models
https://sysidentpy.org
BSD 3-Clause "New" or "Revised" License
380 stars 77 forks source link

MIMO #132

Closed wangshanze closed 2 months ago

wangshanze commented 2 months ago

Can your package handle systems with multiple inputs and outputs? Do you have any examples. (Looking forward to receiving your answer)

wilsonrljr commented 2 months ago

Hey @wangshanze! No, it can't. Only MISO systems are supported for now. The only way to handle MIMO systems is to split it into multiples MISO systems.

wangshanze commented 2 months ago

Dear Willsonrljr, I didn't expect you to reply to me. I am very excited and I will use your package to split the MIMO system into multiple MISO systems.

I have just been admitted to graduate school and I will continue to research in this field. If you come to Shenyang, Liaoning, China, I will treat you to many delicious Chinese cuisine. Thank you very much for your bag. Wishing you a happy and fulfilling life.

---- Replied Message ---- | From | Wilson @.> | | Date | 6/25/2024 07:53 | | To | @.> | | Cc | @.>, @.> | | Subject | Re: [wilsonrljr/sysidentpy] MIMO (Issue #132) |

Hey @wangshanze! No, it can't. Only MISO systems are supported for now. The only way to handle MIMO systems is to split it into multiples MISO systems.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

wilsonrljr commented 2 months ago

Thank you for your message, @wangshanze! Congratulations on your admission to graduate school! I wish you all the best in your continued studies. Besides, if you have any questions about how to use SysIdentPy in the future or any assistance, don't hesitate to contact me.

I hope to visit China someday, and I will certainly remember your invitation!

I'll close this issue for know, but again, feel free to contact me if you need anything else.

wangshanze commented 2 months ago

Dear Willsonrljr,Could you please explain to me how to retrieve this xlag and ylag? My data is not accurate, and I have been adjusting parameters recently but have not been able to do so. Could you please provide me with an answer? Thank you, I can show you my code. Please.

---- Replied Message ---- | From | Wilson @.> | | Date | 6/27/2024 05:46 | | To | @.> | | Cc | @.>, @.> | | Subject | Re: [wilsonrljr/sysidentpy] MIMO (Issue #132) |

Thank you for your message, @wangshanze! Congratulations on your admission to graduate school! I wish you all the best in your continued studies. Besides, if you have any questions about how to use SysIdentPy in the future or any assistance, don't hesitate to contact me.

I hope to visit China someday, and I will certainly remember your invitation!

I'll close this issue for know, but again, feel free to contact me if you need anything else.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

wilsonrljr commented 2 months ago

Hi @wangshanze ! Could you send me a code to reproduce what you are trying to do?

wangshanze commented 2 months ago

ten.xlsx import numpy as np import pandas as pd from sklearn.linear_model import BayesianRidge, ARDRegression from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split from sysidentpy.basis_function._basis_function import Polynomial, Fourier from sysidentpy.general_estimators import NARX from sysidentpy.metrics import root_relative_squared_error from sysidentpy.model_structure_selection import FROLS from sysidentpy.neural_network import NARXNN from sysidentpy.utils.display_results import results from sysidentpy.utils.narmax_tools import regressor_code

from sysidentpy.utils.plotting import plot_residues_correlation, plot_results from sysidentpy.residues.residues_correlation import ( compute_residues_autocorrelation, compute_cross_correlation, ) from torch import nn import torch

excel_path = 'ten.xlsx' xls = pd.ExcelFile(excel_path) out_puts_01 = pd.read_excel(xls, sheet_name='Sheet1', usecols='A', skiprows=1, nrows=600)

in_puts = pd.read_excel(xls, sheet_name='Sheet1', usecols='K:M', skiprows=1, nrows=600) out_puts_01 = np.array(out_puts_01) in_puts = np.array(in_puts) basis_function = Polynomial(degree=1) x_train, x_valid, y_train, y_valid = train_test_split(in_puts, out_puts_01, test_size=0.2)

x1lag = list(range(1, 10)) x2lag = list(range(1, 10)) x3lag = list(range(1, 10)) torch.cuda.is_available() device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using {device} device")

narx_net = NARXNN( ylag=100, xlag=[x1lag,x2lag,x3lag], basis_function=basis_function, model_type="NARMAX", loss_func="mse_loss", optimizer="Adam", epochs=2000, verbose=False, device="cuda", optim_params={ "betas": (0.9, 0.999), "eps": 1e-05, }, # optional parameters of the optimizer )

regressors = regressor_code( X=x_train, xlag=[x1lag,x2lag,x3lag], ylag=100, model_type="NARMAX", model_representation="neural_network", basis_function=basis_function, ) n_features = regressors.shape[0] # the number of features of the NARX net print(n_features) class NARX(nn.Module): def init(self): super().init() self.lin = nn.Linear(n_features, 30) self.lin2 = nn.Linear(30, 30) self.lin3 = nn.Linear(30, 1) self.tanh = nn.Tanh()

def forward(self, xb):
    z = self.lin(xb)
    z = self.tanh(z)
    z = self.lin2(z)
    z = self.tanh(z)
    z = self.lin3(z)
    return z

narx_net.net = NARX() if device == "cuda": narx_net.net.to(torch.device("cuda")) narx_net.fit(X=x_train, y=y_train, X_test=x_valid, y_test=y_valid) yhat = narx_net.predict(X=x_valid, y=y_valid) print("MSE: ", mean_squared_error(y_valid, yhat)) 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$")

Hi!Dear wilsonrljr!Here is my code and data.Please help me take a look, please

wilsonrljr commented 1 month ago

Hey @wangshanze ! Sorry for the late answer (I'm working hard on the next update, so I take more time than expected to answer you. I'm uploading a notebook for you here. I took your code and made some adaptations. I made 3 important changes:

  1. I change the lag definitions. You were defining too many lags (ylag=100, for example). In a first approach, that's not a good idea.
  2. You input and output data have very different range (the output is 0.0004 while the input is 2000). So I insert a scaler for you. I did not tested which scaler works the best, but you can test it using my code as an example. I've used the MaxAbsScaler from scikit learn.
  3. You were using the train_test_split function from scikit learn to split your data. In a time series scenario, that's wrong because that function change the sequence in the data by taking random values, so I've changed that.

There are other minor changes in the code, as you'll see. The results I got are like the image attached, but I think you can improve it by yourself by testing the configurations of the NARX network. As I said, I did not try to get a good model, just to give you some ideas.

image

Let me know if you have any other question! test_wang.zip

wangshanze commented 1 month ago

Thank you very much, wilsonrljr sir.I am already very happy that you can reply to me.I still have many shortcomings in my programming ability, and I will continue to learn. Thank you for the code you have modified and the questions you have solved for me. I will carefully read it to make up for my shortcomings. Thank you again and wish you a happy day It's quite dreamy to say, two people I've never seen before have crossed countless kilometers using email. Haha!

---- Replied Message ---- | From | Wilson @.> | | Date | 7/17/2024 08:46 | | To | @.> | | Cc | @.>, @.> | | Subject | Re: [wilsonrljr/sysidentpy] MIMO (Issue #132) |

Hey @wangshanze ! Sorry for the late answer (I'm working hard on the next update, so I take more time than expected to answer you. I'm uploading a notebook for you here. I took your code and made some adaptations in your code. I made 3 important changes:

I change the lag definitions. You were defining too many lags (ylag=100, for example). In a first approach, that's not a good idea. You input and output data have very different range (the output is 0.0004 while the input is 2000). So I insert a scaler for you. I did not tested which scaler works the best, but you can test it using my code as an example. I've used the MaxAbsScaler from scikit learn. You were using the train_test_split function from scikit learn to split your data. In a timeseries scenario, thats wrong because that function change the sequence in the data by taking random values, so I've changed that.

There are other minor changes in the code, as you'll see. The results I got are like the image attached, but I think you can improve it by yourself by testing the configurations of the NARX network. As I said, I did not try to get a good model, just to give you some ideas.

image.png (view on web)

Let me know if you have any other question! test_wang.zip

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>