[英]Python - slice indices must be integers or None or have an __index__ method
I have a problem with using scipy's fmin solver.我在使用 scipy 的 fmin 求解器时遇到问题。 I wanted it to optimize parametres for my ARIMA model.我希望它为我的 ARIMA 模型优化参数。 When running code straight on (without fmin) everything works fine but when putting into optimizer there is an error:当直接运行代码(没有 fmin)时,一切正常,但在放入优化器时出现错误:
Traceback (most recent call last):
File "D:/Work/repo_python/bandwidth_estimation/estima.py", line 169, in <module>
optimum = fmin(criterion, x0)
File "C:\Users\Pigeon\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\optimize.py", line 442, in fmin
res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
File "C:\Users\Pigeon\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\optimize.py", line 585, in _minimize_neldermead
fsim[k] = func(sim[k])
File "C:\Users\Pigeon\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\optimize.py", line 326, in function_wrapper
return function(*(wrapper_args + args))
File "D:/Work/repo_python/bandwidth_estimation/estima.py", line 18, in criterion
model_arima = ARIMA(train, order=parametres)
File "C:\Users\Pigeon\AppData\Local\Programs\Python\Python36\lib\site-packages\statsmodels\tsa\arima_model.py", line 988, in __new__
mod.__init__(endog, order, exog, dates, freq, missing)
File "C:\Users\Pigeon\AppData\Local\Programs\Python\Python36\lib\site-packages\statsmodels\tsa\arima_model.py", line 1009, in __init__
self._first_unintegrate = unintegrate_levels(self.endog[:d], d)
TypeError: slice indices must be integers or None or have an __index__ method
Here's the code:这是代码:
def criterion(parametres):
parametres = tuple(parametres)
control_sum = 0
train = [5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349]
model_arima = ARIMA(train, order=parametres)
model_arima_fit = model_arima.fit()
predictions = model_arima_fit.forecast(steps=24)[0]
for i in range(0,len(predictions)):
control_sum += (predictions[i]-values[i])**2
print(control_sum)
return control_sum
x0 = [1,1,1]
optimum = fmin(criterion, x0)
As I said before - if I run code outside the function - everything works fine.正如我之前所说 - 如果我在函数之外运行代码 - 一切正常。 The problem is when put it into function that lands in fmin solver.问题是当它进入 fmin 求解器的函数时。
It's because fmin
is passing in floats to the parameters and they need to be int
you can fix it by casting parametres
to int like this这是因为fmin
正在将浮点数传递给参数,并且它们需要是int
您可以通过将parametres
为 int 来修复它,如下所示
parametres = parametres.astype(int)
and all together:和所有在一起:
def criterion(parametres):
parametres = parametres.astype(int)
control_sum = 0
train = [5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.545763155811349, 5.361409086288781, 5.899251779517743, 5.766256093003701, 5.795872889382786, 5.637485909370455, 5.7453759249241045, 5.066030606246879, 5.0944229577563425, 5.0944229577563425, 5.0944229577563425, 5.545763155811349]
print(parametres)
model_arima = ARIMA(train, order=parametres)
model_arima_fit = model_arima.fit()
predictions = model_arima_fit.forecast(steps=24)[0]
for i in range(0,len(predictions)):
control_sum += (predictions[i]-values[i])**2
print(control_sum)
return control_sum
x0 = [1,1,1]
optimum = fmin(criterion, x0)
Also, you are using the variable values
in your function while it is not defined in your function, that might cause problems as well.此外,您在函数中使用变量values
,而它未在函数中定义,这也可能会导致问题。
NOTE: That in order to use this outside of fmin
you will need to pass in a numpy array and not convert it to a tuple in your function.注意:为了在fmin
之外使用它,您需要传入一个 numpy 数组,而不是将其转换为函数中的元组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.