使用gekko的已安装模型的predict()方法。

huangapple go评论53阅读模式
英文:

Using the predict() methods of fitted models with gekko

问题

许多模型拟合的Python包都有一个predict()方法,该方法会根据预测变量的观察值输出拟合模型的预测结果。

问题: 当观察值是gekko模型中的一个变量时,我该如何使用这些predict()方法来预测单个值?

以下是一个非常简单的可重现示例:

注意: 我正在拟合的实际模型是使用statsmodels.gam.smooth_basis.BSplinesstatsmodels.gam.generalized_additive_model.GLMGam的B样条模型。但是,我希望这个使用sklearn.linear_model.LinearRegression的简单示例可以适用于其他包中的更复杂模型类别。

from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

# 创建示例数据
x = np.arange(100)[:, np.newaxis]
y = np.arange(100) * 2 + 10

plt.plot(x, y)  # 绘制x与y数据
# plt.show()

model = LinearRegression()  # 实例化线性模型
model.fit(x, y)  # 拟合模型

x_predict = np.arange(100, 200)[:, np.newaxis]  # 创建预测观察值的数组
y_predict = model.predict(x_predict)  # 使用模型进行预测

plt.plot(x_predict, y_predict)  # 绘制预测结果
# plt.show()

m = GEKKO()  # 实例化gekko模型

x2 = m.FV()  # 实例化自由变量
x2.STATUS = 1  # 使变量可供求解器使用

y2 = 50  # 真实值

# 将x2变量放入numpy数组以符合predict()的参数要求
x2_arr = np.array(x2).reshape(1, -1)

# 最小化真实值与模型预测之间的平方误差
m.Minimize((y2 - model.predict(x2_arr)) ** 2)

m.options.IMODE = 3
# 求解x2
m.solve(disp=True)

print(f"x2 = {x2.value[0]:.3f}")

我得到以下连续错误:

TypeError: float() argument must be a string or a real number, not 'GK_FV'

ValueError: setting an array element with a sequence.

我的第一个想法是,我可能需要创建一个包装类来包装gekko.gk_parameter.GK_FV类,以修改float()方法,但这就是我的知识和技能的极限。

英文:

Many model-fitting Python packages have a predict() method, which outputs a prediction of the fitted model given observations of the predictor(s).

Question: How would I use these predict() methods to predict a single value when the observation is a variable in a gekko model?

Below is a very simple reproducible example:

Note: The actual model that I am fitting is a B-spline using statsmodels.gam.smooth_basis.BSplines and statsmodels.gam.generalized_additive_model.GLMGam. However, I am hoping that this simple example with sklearn.linear_model.LinearRegression will translate to more complex model classes from other packages.

from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

# create example data
x = np.arange(100)[:, np.newaxis]
y = np.arange(100) * 2 + 10

plt.plot(x, y)  # plot x vs y data
# plt.show()

model = LinearRegression()  # instantiate linear model
model.fit(x, y)  # fit model

x_predict = np.arange(100, 200)[:, np.newaxis]  # create array of predictor observations
y_predict = model.predict(x_predict)  # use model to make prediction

plt.plot(x_predict, y_predict)  # plot prediction
# plt.show()

m = GEKKO()  # instantiate gekko model

x2 = m.FV()  # instantiate free variable
x2.STATUS = 1  # make variable available for solver

y2 = 50  # true value

# place x2 variable in numpy array to adhere to predict()'s argument requirements
x2_arr = np.array(x2).reshape(1, -1)

# minimize squared error between the true value and the model's prediction
m.Minimize((y2 - model.predict(x2_arr)) ** 2)

m.options.IMODE = 3
# solve for x2
m.solve(disp=True)

print(f"x2 = {x2.value[0]:3f}")

I get the following sequential errors:

TypeError: float() argument must be a string or a real number, not 'GK_FV'

ValueError: setting an array element with a sequence.

My first thought is that I would have to create a wrapper class around the gekko.gk_parameter.GK_FV class to modify the float() method, but that's where my knowledge and skills end.

答案1

得分: 1

Gekko ML函数 可以导入 sklearngpflowtensorflow 模型。它要求先拟合模型,然后将参数和方程式导入到 gekko 模型中。

Gekko_Model = ML.Gekko_LinearRegression(model, Xtrain, RMSE, Gekko_Model)
  • 从sklearn导入一个经过训练的线性回归模型。此模型通过回归方法的Delta方法来计算不确定性。

  • model:从sklearn导入的经过训练的模型,可以是岭回归或线性回归模型。

  • Xtrain:输入训练集,用于计算预测区间。

  • RMSE:在整个数据集训练期间计算的均方根误差。用于计算预测区间。

  • Gekko_Model:Gekko模型(由GEKKO()创建),附加了新的线性回归模型。

英文:

The Gekko ML functions can import sklearn, gpflow, and tensorflow models. It requires the models to be fit and then the parameters and equations are imported into the gekko model.

Gekko_Model = ML.Gekko_LinearRegression(model,Xtrain,RMSE,Gekko_Model)
  • Import a trained linear regression model from sklearn. This model calculates uncertainty through the delta method for regression methods.

  • model: trained model from sklearn as a Ridge Regression or Linear Regression model.

  • Xtrain: input training set, needed to calculate the prediction interval.

  • RMSE: root mean squared error calculated during training for the entire data set. This is used to calculate the prediction interval.

  • Gekko_Model: Gekko model (created by GEKKO()) that is appended with the new Linear Regression model.

huangapple
  • 本文由 发表于 2023年6月6日 01:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定