GEKKO ARX 模型预测:数据数组必须具有相同的长度
GEKKO ARX Model Forecasting: data arrays must have the same length
我正在尝试使用 sysid
定期回归 ARX 模型,然后通过模拟未来的输入并将输出与实验数据进行比较来评估该模型的预测能力。当我尝试使用 m.solve()
解决问题时,出现以下错误:Exception: Data arrays must have the same length, and match time discretization in dynamic problems
以下是 MRE:
X = [[ 0.9, 0.], [ 0.9, 0.],[ 0.9,0.],[ 0.9,0.],[ 0.9, 0.],[ 0.5,0.],[0.5,0.],[0.5,0.],[0.5,0.], [ 0.5, 0.]] # 2 values for inputs at each time step
Y = [20.3, 20.3, 20.2, 20.2, 20.1, 20.1, 20.1, 20., 19.9, 19.8,] # 1 output at each time step
t = np.linspace(0, 9*300, 10) # 10 points 5 minutes apart each
na = 1 # output coefficients
nb = 2 # input coefficients
res, p, K = m.sysid(t, X, Y, na, nb, pred='meas')
m.time = t - t[0]
y_, u_ = m.arx(p)
u_[0].value = X[0]
u_[1].value = X[1]
m.options.imode = 4
m.options.nodes = 2
# simulate
m.solve()
我不想控制,而是将实验值应用于未来的时间步长,看看 ARX 模型如何推断。
感谢您的帮助
问题出在这部分以及如何将数据加载到 value
:
u_[0].value = X[:,0]
u_[1].value = X[:,1]
y_[0].value = Y
尝试打印 X[0]
和 X[1]
以查看它们只是原始列表的第一个和第二个元素。转换为 numpy 数组有助于切片。
import numpy as np
from gekko import GEKKO
m = GEKKO()
X = np.array([[ 0.9, 0.], [ 0.9, 0.],\
[ 0.9,0.],[ 0.9,0.],\
[ 0.9, 0.],[ 0.5,0.],\
[0.5,0.],[0.5,0.],\
[0.5,0.], [ 0.5, 0.]]) # 2 values for inputs at each time step
Y = np.array([20.3, 20.3, 20.2, 20.2,\
20.1, 20.1, 20.1, 20., \
19.9, 19.8]) # 1 output at each time step
t = np.linspace(0, 9*300, 10) # 10 points 5 minutes apart each
na = 1 # output coefficients
nb = 2 # input coefficients
res, p, K = m.sysid(t, X, Y, na, nb, pred='meas')
m.time = t - t[0]
y_, u_ = m.arx(p)
u_[0].value = X[:,0]
u_[1].value = X[:,1]
y_[0].value = Y
print(X[0])
print(X[1])
m.options.imode = 4
m.options.nodes = 2
# simulate
m.solve()
这是另一个使用 Pandas DataFrames 的例子:
from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data[['H1','H2']]
y = data['T1']
# generate time-series model
m = GEKKO(remote=False) # remote=True for MacOS
# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,diaglevel=1)
plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u)
plt.legend([r'$u_0$',r'$u_1$'])
plt.ylabel('MVs')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.legend([r'$y_0$',r'$z_0$'])
plt.ylabel('CVs')
plt.xlabel('Time')
plt.savefig('sysid.png')
plt.show()
我们还在开发一个带有 Seeq add-on for system identification 的软件包,它可以在 Python 和 Jupyter notebooks 中运行。
我正在尝试使用 sysid
定期回归 ARX 模型,然后通过模拟未来的输入并将输出与实验数据进行比较来评估该模型的预测能力。当我尝试使用 m.solve()
解决问题时,出现以下错误:Exception: Data arrays must have the same length, and match time discretization in dynamic problems
以下是 MRE:
X = [[ 0.9, 0.], [ 0.9, 0.],[ 0.9,0.],[ 0.9,0.],[ 0.9, 0.],[ 0.5,0.],[0.5,0.],[0.5,0.],[0.5,0.], [ 0.5, 0.]] # 2 values for inputs at each time step
Y = [20.3, 20.3, 20.2, 20.2, 20.1, 20.1, 20.1, 20., 19.9, 19.8,] # 1 output at each time step
t = np.linspace(0, 9*300, 10) # 10 points 5 minutes apart each
na = 1 # output coefficients
nb = 2 # input coefficients
res, p, K = m.sysid(t, X, Y, na, nb, pred='meas')
m.time = t - t[0]
y_, u_ = m.arx(p)
u_[0].value = X[0]
u_[1].value = X[1]
m.options.imode = 4
m.options.nodes = 2
# simulate
m.solve()
我不想控制,而是将实验值应用于未来的时间步长,看看 ARX 模型如何推断。
感谢您的帮助
问题出在这部分以及如何将数据加载到 value
:
u_[0].value = X[:,0]
u_[1].value = X[:,1]
y_[0].value = Y
尝试打印 X[0]
和 X[1]
以查看它们只是原始列表的第一个和第二个元素。转换为 numpy 数组有助于切片。
import numpy as np
from gekko import GEKKO
m = GEKKO()
X = np.array([[ 0.9, 0.], [ 0.9, 0.],\
[ 0.9,0.],[ 0.9,0.],\
[ 0.9, 0.],[ 0.5,0.],\
[0.5,0.],[0.5,0.],\
[0.5,0.], [ 0.5, 0.]]) # 2 values for inputs at each time step
Y = np.array([20.3, 20.3, 20.2, 20.2,\
20.1, 20.1, 20.1, 20., \
19.9, 19.8]) # 1 output at each time step
t = np.linspace(0, 9*300, 10) # 10 points 5 minutes apart each
na = 1 # output coefficients
nb = 2 # input coefficients
res, p, K = m.sysid(t, X, Y, na, nb, pred='meas')
m.time = t - t[0]
y_, u_ = m.arx(p)
u_[0].value = X[:,0]
u_[1].value = X[:,1]
y_[0].value = Y
print(X[0])
print(X[1])
m.options.imode = 4
m.options.nodes = 2
# simulate
m.solve()
这是另一个使用 Pandas DataFrames 的例子:
from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data[['H1','H2']]
y = data['T1']
# generate time-series model
m = GEKKO(remote=False) # remote=True for MacOS
# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,diaglevel=1)
plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u)
plt.legend([r'$u_0$',r'$u_1$'])
plt.ylabel('MVs')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.legend([r'$y_0$',r'$z_0$'])
plt.ylabel('CVs')
plt.xlabel('Time')
plt.savefig('sysid.png')
plt.show()
我们还在开发一个带有 Seeq add-on for system identification 的软件包,它可以在 Python 和 Jupyter notebooks 中运行。