问题
根据SciPy docs,函数scipy.optimize.basinhopping(...)
返回OptimizeResult
的实例,该实例基本上是字典的包装器。我可以print
该实例并查看success: True
,但是我想以编程方式访问它。在通过scipy.optimize.minimize(...)
进行局部最小化的情况下-根据SciPy docs,它还会返回OptimizeResult
的实例-success
值可以作为OptimizeResult.success
进行访问。但是,这不适用于basinhopping
例程。我觉得这很奇怪,因为SciPy docs的OptimizeResult
显示success
应该是允许的键。
我的问题
如何以编程方式访问success
的OptimizeResult
值?
示例代码
要重新创建此问题(并显示它在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
问题根据SciPy文档,函数scipy.optimize.basinhopping(...)返回OptimizeResult的实例,该实例基本上是字典的包装。我可以打印此...
[global_result
结果具有属性lowest_optimization_result
,它本身是OptimizeResult
的实例,因此您应该调用该属性的success
: