I have been trying to pass a piecewise function through the scipy optimizer. The example I've constructed below shows the problem:
我一直试图通过scipy优化器传递分段函数。我在下面构建的示例显示了问题:
args = (6,6,7,1,2,4,6,6)
def _alpha(params, *args):
knot = params[0]
rate = np.asarray(args)
where_knot = np.where(rate>knot, 1, 0)
return np.sum(where_knot)
seed_vals = (5,)
bounds = ((1,7), )
res1 = optimize.minimize(_alpha, seed_vals, args=args, method='L-BFGS-B', bounds=bounds)
res1.x
>>> array([ 5.])
However, this is obviously not the solution:
但是,这显然不是解决方案:
print _alpha((5,), args)
>>> 5
print _alpha((7,), args)
>>> 0
Is there a way to do this that works?
有没有办法做到这一点?
EDIT: I've also tried the numpy piecewise function and get the same results.
编辑:我也尝试了numpy分段函数并获得相同的结果。
1 个解决方案
#1
2
you'll need to adjust your approximation stepsize using this: http://docs.scipy.org/doc/scipy/reference/optimize.minimize-lbfgsb.html#optimize-minimize-lbfgsb
您需要使用以下方法调整近似步长:http://docs.scipy.org/doc/scipy/reference/optimize.minimize-lbfgsb.html#optimize-minimize-lbfgsb
the default is something like .0000001 so it will estimate a 0 gradient for knot
默认值类似于.0000001,因此它将估计结的0梯度
#1
2
you'll need to adjust your approximation stepsize using this: http://docs.scipy.org/doc/scipy/reference/optimize.minimize-lbfgsb.html#optimize-minimize-lbfgsb
您需要使用以下方法调整近似步长:http://docs.scipy.org/doc/scipy/reference/optimize.minimize-lbfgsb.html#optimize-minimize-lbfgsb
the default is something like .0000001 so it will estimate a 0 gradient for knot
默认值类似于.0000001,因此它将估计结的0梯度