问题
根据SciPy docs ,函数 scipy.optimize.basinhopping(...)
返回一个 OptimizeResult
实例,它基本上是一个字典的包装器。我可以打印
这个实例并看到success: True
,但我想以编程方式访问它。在通过 scipy.optimize.minimize(...) 进行局部最小化的情况下 - 根据 SciPy docs ,还返回 OptimizeResult
的实例 - success
值可以作为 OptimizeResult.success
访问。但是,这对于 basinhopping
例程不起作用。我觉得这很奇怪,因为 SciPy docs对于 OptimizeResult
显示 success
应该是允许的键。
我的问题
如何以编程方式访问 OptimizeResult
的 success
值?
示例代码
要重现此问题(并证明它在 minimize(...)
的情况下有效),请参阅下面的代码:
import numpy as np
from scipy.optimize import minimize, basinhopping, OptimizeResult
def pdf(prms, x):
""" normal distribution pdf """
return np.exp(- np.square((x - prms[0])/prms[1]) / 2) / (prms[1] * np.sqrt(2 * np.pi))
def nll(prms, x):
""" negative log-likelihood """
return np.log(pdf(prms, x))
def err_func(prms, x):
""" maximum likelihood estimation """
return -1 * np.sum(nll(prms, x))
prms = (50, 10) # true parameters: mean=50, std=10
data = np.random.normal(loc=prms[0], scale=prms[1], size=1000) # normal distribution
x0 = (30, 15) # initial parameter guess
local_result = minimize(err_func, x0, args=(data,), method='Nelder-Mead')
print("\n LOCAL EXTREMUM RESULT:\n{}\n".format(local_result))
print("\n .. LOCAL SUCCESS:\n{}\n".format(local_result.success))
minimizer_kwargs = {'args' : (data,), 'method' : 'Nelder-Mead'}
global_result = basinhopping(err_func, x0, minimizer_kwargs=minimizer_kwargs)
print("\n GLOBAL EXTREMUM RESULT:\n{}\n".format(global_result))
try:
print("\n .. GLOBAL SUCCESS:\n{}\n".format(global_result.success))
except:
suc = OptimizeResult(global_result)
print("\n .. GLOBAL SUCCESS:\n{}\n".format(suc.success))
示例输出
上面的 print
语句输出以下内容:
LOCAL EXTREMUM RESULT:
final_simplex: (array([[49.81697641, 10.07216849],
[49.81705723, 10.07218614],
[49.81706317, 10.07208372]]), array([3728.71186314, 3728.71186315, 3728.71186316]))
fun: 3728.711863138763
message: 'Optimization terminated successfully.'
nfev: 86
nit: 44
status: 0
success: True
x: array([49.81697641, 10.07216849])
.. LOCAL SUCCESS:
True
GLOBAL EXTREMUM RESULT:
fun: 3728.711863121894
lowest_optimization_result: final_simplex: (array([[49.81701209, 10.07213731],
[49.81709255, 10.07216525],
[49.81703196, 10.07220361]]), array([3728.71186312, 3728.71186315, 3728.71186316]))
fun: 3728.711863121894
message: 'Optimization terminated successfully.'
nfev: 63
nit: 31
status: 0
success: True
x: array([49.81701209, 10.07213731])
message: ['requested number of basinhopping iterations completed successfully']
minimization_failures: 0
nfev: 6455
nit: 100
x: array([49.81701209, 10.07213731])
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 114, in __getattr__
return self[name]
KeyError: 'success'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "stacko.py", line 28, in <module>
print("\n .. GLOBAL SUCCESS:\n{}\n".format(global_result.success))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 116, in __getattr__
raise AttributeError(name)
AttributeError: success
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 114, in __getattr__
return self[name]
KeyError: 'success'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "stacko.py", line 31, in <module>
print("\n .. GLOBAL SUCCESS:\n{}\n".format(suc.success))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 116, in __getattr__
raise AttributeError(name)
AttributeError: success
最佳答案
global_result
结果具有属性 lowest_optimization_result
,它本身就是 OptimizeResult
的实例,因此您应该调用该属性的 success
一:
global_result.lowest_optimization_result.success
关于python - 如何访问从 scipy.optimize.basinhopping (不是最小化)返回的 OptimizeResult 实例的 `success` 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598917/