使用 scipy.optimize.brute
Using scipy.optimize.brute
基于 this post 我正在尝试使用 brute 在 ARIMA 模型上进行网格搜索,但我做不到 运行。我正在做这个原则证明,但我在论证上做错了什么?
y = pd.DataFrame([0,1,4,9,16]) + 3
def objfunc(coeffs, endog):
exp = coeffs[0]
const = coeffs[1]
print(exp, const, endog)
out = 0
for i in range(4):
out += i**exp + const
return out
from scipy.optimize import brute
grid = (slice(0, 2, 1), slice(3, 4, 1))
brute(objfunc, ranges=grid, args=y)
(0, 3, 0)
(0, 3, 0)
(1, 3, 0)
...
TypeError: objfunc() takes exactly 2 arguments (1 given)
一旦我解决了这个问题,我的目标实际上是按顺序优化这个函数,seasonal_order 分别是这样的元组(_、_、_)和这个(_、_、_、12)。
def objfunc(coeffs, endog):
order = coeffs[0]
seasonal = coeffs[1]
fit = sm.tsa.statespace.SARIMAX(endog, trend='n', order=order, seasonal_order=seasonal).fit()
return fit.aic()
编辑:此代码有效(感谢@sasha),变量名更清晰,更有意义(我将错误的函数最小化)。
import pandas as pd
y = np.array([0,1,4,9,16]) + 3 #polynomial x^2+3 with x=0:4
def objfunc(coeffs, *args):
arr = args[0] # access first element of tuple: y
exp = coeffs[0] # assuming y should become endog
const = coeffs[1]
pol = [i**exp + const for i in range(len(y))]
print coeffs
return abs(sum(pol) - sum(arr))
from scipy.optimize import brute
grid = (slice(1, 3, 1), slice(2, 5, 1))
resbrute = brute(objfunc, ranges=grid, args=(y,), full_output=True, finish=None)
print("Best coeffs: {}".format(resbrute[0]))
print("Score with best coeffs: {}".format(resbrute[1]))
print("Grid: {}".format(resbrute[2].tolist()))
print("Scores for grid: {}".format(resbrute[3].tolist()))
包含所有这些变量名的代码看起来有点奇怪。内狗,y; y 变成 endog?
不过下面的方法大概就是这样了,完全按照documentation
args : tuple, optional
Any additional fixed parameters needed to completely specify the function.
代码:
import pandas as pd
y = pd.DataFrame([0,1,4,9,16]) + 3
def objfunc(coeffs, *args):
endog = args[0] # access first element of tuple: y
exp = coeffs[0] # assuming y should become endog
const = coeffs[1]
print(exp, const, endog)
out = 0
for i in range(4):
out += i**exp + const
return out
from scipy.optimize import brute
grid = (slice(0, 2, 1), slice(3, 4, 1))
brute(objfunc, ranges=grid, args=(y,)) # in general a tuple; first pos: y
基于 this post 我正在尝试使用 brute 在 ARIMA 模型上进行网格搜索,但我做不到 运行。我正在做这个原则证明,但我在论证上做错了什么?
y = pd.DataFrame([0,1,4,9,16]) + 3
def objfunc(coeffs, endog):
exp = coeffs[0]
const = coeffs[1]
print(exp, const, endog)
out = 0
for i in range(4):
out += i**exp + const
return out
from scipy.optimize import brute
grid = (slice(0, 2, 1), slice(3, 4, 1))
brute(objfunc, ranges=grid, args=y)
(0, 3, 0)
(0, 3, 0)
(1, 3, 0)
...
TypeError: objfunc() takes exactly 2 arguments (1 given)
一旦我解决了这个问题,我的目标实际上是按顺序优化这个函数,seasonal_order 分别是这样的元组(_、_、_)和这个(_、_、_、12)。
def objfunc(coeffs, endog):
order = coeffs[0]
seasonal = coeffs[1]
fit = sm.tsa.statespace.SARIMAX(endog, trend='n', order=order, seasonal_order=seasonal).fit()
return fit.aic()
编辑:此代码有效(感谢@sasha),变量名更清晰,更有意义(我将错误的函数最小化)。
import pandas as pd
y = np.array([0,1,4,9,16]) + 3 #polynomial x^2+3 with x=0:4
def objfunc(coeffs, *args):
arr = args[0] # access first element of tuple: y
exp = coeffs[0] # assuming y should become endog
const = coeffs[1]
pol = [i**exp + const for i in range(len(y))]
print coeffs
return abs(sum(pol) - sum(arr))
from scipy.optimize import brute
grid = (slice(1, 3, 1), slice(2, 5, 1))
resbrute = brute(objfunc, ranges=grid, args=(y,), full_output=True, finish=None)
print("Best coeffs: {}".format(resbrute[0]))
print("Score with best coeffs: {}".format(resbrute[1]))
print("Grid: {}".format(resbrute[2].tolist()))
print("Scores for grid: {}".format(resbrute[3].tolist()))
包含所有这些变量名的代码看起来有点奇怪。内狗,y; y 变成 endog?
不过下面的方法大概就是这样了,完全按照documentation
args : tuple, optional
Any additional fixed parameters needed to completely specify the function.
代码:
import pandas as pd
y = pd.DataFrame([0,1,4,9,16]) + 3
def objfunc(coeffs, *args):
endog = args[0] # access first element of tuple: y
exp = coeffs[0] # assuming y should become endog
const = coeffs[1]
print(exp, const, endog)
out = 0
for i in range(4):
out += i**exp + const
return out
from scipy.optimize import brute
grid = (slice(0, 2, 1), slice(3, 4, 1))
brute(objfunc, ranges=grid, args=(y,)) # in general a tuple; first pos: y