gpt4 book ai didi

python - 使用 scipy.optimize 优化 3 个系数

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:41 25 4
gpt4 key购买 nike

我一直在尝试优化三个系数。但我无法解决我遇到的错误。以下是代码。 'y' 是预测函数,它接受时间序列数据列表和系数列表,然后在计算后返回预测列表。 x是历史数据。 coeffList 是一个包含 3 个系数作为值的列表。

“mape”函数计算实际数据和预测数据之间的差异。在优化中,我试图最小化“mape”函数的输出。作为约束,三个系数均大于 0 且小于 1。

from __future__ import division
import numpy as np
from scipy.optimize import minimize



#coeffList[0] = alpha
#coeffList[1] = beta
#coeffList[2] =gamma

def mape(x, coeffList):
diff = abs(y(x,coeffList)-x)
print("np.mean(diff/x) : ", np.mean(diff/x))
return np.mean(diff/x)


#Holt Winters-Multiplicative



def y(x, coeffList , debug=True):

c =4
#Compute initial b and intercept using the first two complete c periods.
xlen =len(x)
print("xlen : ", xlen)
#if xlen % c !=0:
# return None
fc =float(c)
xbar2 =sum([x[i] for i in range(c, 2 * c)])/ fc
print("xbar2 : ",xbar2)

xbar1 =sum([x[i] for i in range(c)]) / fc


print("xbar1 : ", xbar1)
b0 =(xbar2 - xbar1) / fc
if debug: print ("b0 = ", b0)

#Compute for the level estimate a0 using b0 above.
tbar =sum(i for i in range(1, c+1)) / fc
print("tbar : ",tbar)
a0 =xbar1 - b0 * tbar
if debug: print ("a0 = ", a0)

#Compute for initial indices - seasonality
I =[x[i] / (a0 + (i+1) * b0) for i in range(0, xlen)]
if debug: print ("Initial indices = ", I)

S=[0] * (xlen+ c)

for i in range(c):
S[i] =(I[i] + I[i+c]) / 2.0
print ("S[",i,"]=", S[i])

#Normalize so S[i] for i in [0, c) will add to c.
tS =c / sum([S[i] for i in range(c)])
print("tS : ", tS)
for i in range(c):
S[i] *=tS
if debug: print ("Normalized S[",i,"]=", S[i])

# Holt - winters proper ...
if debug: print( "Use Holt Winters formulae")


At =a0
Bt =b0
#y =[0] * (xlen)
y = np.empty(len(x),float)
for i in range(xlen):
Atm1 =At # a[0] = a0
Btm1 =Bt # b[0] = b0

At =coeffList[0] * x[i] / S[i] + (1.0-coeffList[0]) * (Atm1 + Btm1)


Bt =coeffList[1] * (At - Atm1) + (1- coeffList[1]) * Btm1

S[i+c] =coeffList[2] * x[i] / At + (1.0 - coeffList[2]) * S[i]




y[i]=(a0 + b0 * (i+1)) * S[i]

return y

# the time-series data.
coeff = [0.2, 0.3, 0.4]

x =[146, 96, 59, 133, 192, 127, 79, 186, 272, 155, 98, 219]
test = y(x,coeff)
print("x : ", x)
print("y : ",test)


#optimization


result = minimize(mape, coeff, (x,), bounds =[(0,1),(0,1), (0,1)], method='SLSQP')
opt = result.x
print("opt : ", result.x)

这是我收到的错误消息:

Traceback (most recent call last):
File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 135, in <module>
result = minimize(mape, coeff, (x,), bounds =[(0,1),(0,1), (0,1)], method='SLSQP')
File "C:\Python27\lib\site-packages\scipy\optimize\_minimize.py", line 364, in minimize
constraints, **options)
File "C:\Python27\lib\site-packages\scipy\optimize\slsqp.py", line 354, in _minimize_slsqp
fx = func(x)
File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 261, in function_wrapper
return function(x, *args)
File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 12, in mape
diff = abs(y(x,coeffList)-x)
File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 30, in y
xbar2 =sum([x[i] for i in range(c, 2 * c)])/ fc
IndexError: index 4 is out of bounds for axis 0 with size 3

我遇到了什么问题?我非常感谢您的任何评论

最佳答案

生成 IndexError: index 4 is out ofbounds for axis 0 with size 3 的调用是最小化调用。这是因为您正在最小化的目标函数会尝试最小化第一个参数。

您似乎想最小化coeff(三维的东西),但目前您正在尝试最小化mape的第一个参数,即x。因此,您的函数的输入参数顺序错误。您可以通过 say 快速修复此问题(或重新定义 mape(将第一行更改为 def mape(x, coeffList):)。

def mape_reversed(coeffList, x):                                                         
return mape(x, coeffList)

result = minimize(mape_reversed, coeff, (x,), bounds =[(0,1),(0,1), (0,1)], method='SLSQP')

关于python - 使用 scipy.optimize 优化 3 个系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com