更新2: 更好的标题(现在我明白了问题)应该是: scipy optimize.fmin输入的正确语法是什么?
更新:请求提供可运行代码,因此函数定义已被替换为可运行代码。样本输入数据已硬编码为numpy数组"data"。
我正在尝试使用scipy优化一个函数,但卡住了,并且必须请求帮助。一个长度为零的数组被传递给优化器中的方法,我无法理解为什么会这样,也不知道如何克服这个问题。
这段代码试图做什么:
- 给定由单个观测值"r"组成的数据集"data"
- 估计产生"data"的参数"m"的最可能值
- 对于给定的m,计算观察到"data"中每个"r"的概率p(r|m)
- 对于给定的m,计算产生数据集的概率P(m|data)。
- 定义用于optimize.fmin的辅助函数。
- 使用SciPy optimize.fmin确定helper(m|data)最大值的m。
当我运行这段代码时,我得到的错误是: ValueError: zero-size array to reduction operation maximum which has no identity
以下是在我的机器上生成错误的可运行代码片段。
#!/usr/bin/env python2.7
import numpy as np
from scipy import optimize
def p_of_r(m, r): ## this calculates p(r|m) for each datum r
r_range = np.arange(0, r+1, 1, dtype='int')
p_r = []
p_r = np.array([0.0 for a in r_range])
for x in r_range:
if x == 0:
p_r[x] = np.exp(-1 * m)
else:
total = 0.0
for y in np.arange(0, x, 1, dtype='int'):
current = ( p_r[y] ) / (x - y + 1)
total = current + total
p_r[x] = ( m / x ) * total
return p_r
def likelihood_function(m, *data): # calculates P(m|data) using entire data set
p_r = p_of_r(m, np.ma.max(data))
p_r_m = np.array([p_r[y] for y in data])
bigP = np.prod(p_r_m)
return bigP
def main():
data = np.array( [10, 10, 7, 19, 9, 23, 26, 7, 164, 16 ] )
median_r = np.median(data)
def Drake(m):
return median_r / m - np.log(m)
m_initial = optimize.broyden1(Drake, 1)
def helper(x, *args):
helper_value = -1 * likelihood_function(x, *args)
return helper_value
# here is the actual optimize.fmin
fmin_result = optimize.fmin(helper, x0=[m_initial], args=data)
print fmin_result
# for i in np.arange(0.0, 25.0, 0.1):
# print i, helper(i, data)
if __name__ == "__main__" : main()
错误本身: ValueError:零大小数组到没有身份的缩减操作最大值
下面提供跟踪信息。
ValueError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
176 else:
177 filename = fname
--> 178 __builtin__.execfile(filename, *where)
/Users/deyler/bin/MSS-likelihood-minimal.py in <module>()
43 print fmin_result
44
---> 45 if __name__ == "__main__" : main()
/Users/deyler/bin/MSS-likelihood-minimal.py in main()
40
41
---> 42 fmin_result = optimize.fmin(helper, x0=[m_initial], args=data)
43 print fmin_result
44
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in fmin(func, x0, args, xtol, ftol, maxiter, maxfun, full_output, disp, retall, callback)
371 'return_all': retall}
372
--> 373 res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
374 if full_output:
375 retlist = res['x'], res['fun'], res['nit'], res['nfev'], res['status']
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in _minimize_neldermead(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, return_all, **unknown_options)
436 if retall:
437 allvecs = [sim[0]]
--> 438 fsim[0] = func(x0)
439 nonzdelt = 0.05
440 zdelt = 0.00025
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in function_wrapper(*wrapper_args)
279 def function_wrapper(*wrapper_args):
280 ncalls[0] += 1
--> 281 return function(*(wrapper_args + args))
282
283 return ncalls, function_wrapper
/Users/deyler/bin/MSS-likelihood-minimal.py in helper(x, *args)
33 m_initial = optimize.broyden1(Drake, 1)
34 def helper(x, *args):
---> 35 helper_value = -1 * likelihood_function(x, *args)
36 return helper_value
37
/Users/deyler/bin/MSS-likelihood-minimal.py in likelihood_function(m, *data)
21
22 def likelihood_function(m, *data):
---> 23 p_r = p_of_r(m, np.ma.max(data))
24 p_r_m = np.array([p_r[y] for y in data])
25 bigP = np.prod(p_r_m)
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/ma/core.pyc in max(obj, axis, out, fill_value)
5899 # If obj doesn't have a max method,
5900 # ...or if the method doesn't accept a fill_value argument
-> 5901 return asanyarray(obj).max(axis=axis, fill_value=fill_value, out=out)
5902 max.__doc__ = MaskedArray.max.__doc__
5903
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/ma/core.pyc in max(self, axis, out, fill_value)
5159 # No explicit output
5160 if out is None:
-> 5161 result = self.filled(fill_value).max(axis=axis, out=out).view(type(self))
5162 if result.ndim:
5163 # Set the mask
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_methods.pyc in _amax(a, axis, out, keepdims)
8 def _amax(a, axis=None, out=None, keepdims=False):
9 return um.maximum.reduce(a, axis=axis,
---> 10 out=out, keepdims=keepdims)
11
12 def _amin(a, axis=None, out=None, keepdims=False):
ValueError: zero-size array to reduction operation maximum which has no identity
data
是空的。不幸的是,我们无法确定data
来自哪里。此外,你的错误信息与你的代码不匹配。在简化代码时,请尽力构建一个最小的可运行示例,以演示你发布的错误。如果你做不到这一点,请至少使其与错误消息保持一致。 - user2357112data
已明确定义。如果data
看起来为空,则我在优化器的输入方面出了问题。 - dangenet