我希望最小化均方误差函数,为我的模型寻找最佳的alpha
值(衰减率)。
下面是我的模型的描述:
time: 1st_month 2nd_month 3rd_month 4th_month 5th_month
Product_shipped 500 600 453 200 789
If there is delay in products installed after shipping , we multiply by alpha
Suppose alpha=-0.01
We create a lower traingular matrix
month p1 p2 p3 p4 p5
1 495.0249169
2 490.0993367 588.119204
3 485.2227668 582.2673201 439.6118267
4 480.3947196 576.4736635 435.2376159 192.1578878
5 475.6147123 570.7376547 430.9069293 190.2458849 750.5200159
M(1,1)计算为500*(e^-α*月(=1))
M(2,1)计算为500*(e^-α*月(=2))
M(2,2)计算为600*(e^-α*月(=2))
诸如此类。
然后,预测的产品装运量为跨行之和:
Predicted_Installation
495.0249169
1078.218541
1507.101914
1684.263887
2418.025197
我们有原件安装:
Original_Installation
565
1200
1677
1876
2500
我想将F(sum(Original_Installation-Predicted_Installation)^2)
最小化,以找到最小化这一点的alpha
。我们如何在Python中构建这个框架或者解决这个问题。
发布于 2020-03-04 17:16:40
对于这种问题,我肯定会从scipy.otpimize
方法开始。
我在这里复制了一个如何在您的上下文中使用它的示例:
import numpy as np
from scipy.optimize import minimize
ALPHA_TRUE = 0.5 # used only to generate some test data
def model(params, X):
# here you need to implement your real model
# for Predicted_Installation
alpha = params[0]
y_pred = np.exp(-alpha * X)
return y_pred
def sum_of_squares(params, X, Y):
y_pred = model(params, X)
obj = np.sqrt(((y_pred - Y) ** 2).sum())
return obj
# generate some test data
X = np.random.random(10) # this is "month" if I understood your problem correctly
Y = model([ALPHA_TRUE], X) # Original_Installation
# perform fit to find optimal parameters
# initial value for alpha (guess)
alpha_0 = 0.1
res = minimize(sum_of_squares, [alpha_0, ], args=(X, Y), tol=1e-3, method="Powell")
print(res)
结果如下:
direc: array([[-1.12550246e-12]])
fun: 0.0
message: 'Optimization terminated successfully.'
nfev: 225
nit: 9
status: 0
success: True
x: array(0.5)
您必须深入研究文档,以找到最佳的拟合方法,这取决于alpha是否有界或参数是否有约束。
我最近也使用了tensorflow梯度下降优化(例如:https://stellasia.github.io/blog/2020-02-29-custom-model-fitting-using-tensorflow/)来处理类似的东西。
https://datascience.stackexchange.com/questions/69092
复制相似问题