从scipy optimize中提取输出

时间:2017-06-08 09:09:32

标签: python-2.7 optimization scipy minimize

使用Powell算法在Python中最小化双参数函数后,我留下以下输出:

li,
div {
  transition: all 1s linear;
  -webkit-transition: all 1s linear;
  border: 1px solid;
  overflow: hidden;
}

.work {
  border-color: green;
  max-height: 500px;
}

.no-work {
  border-color: red;
  max-height: 500px;
}

li.ng-hide,
div.ng-hide {
  max-height: 0;
}

我希望在x中提取两个优化参数,然后将其传递到脚本的另一部分。有没有办法自动提取这些数字而无需在脚本中手动输入?我查看了fun: -3.5839582049322310 maxcv: 0.0 message: 'Optimization terminated successfully.' nfev: 98 status: 1 success: True x: array([ 1.9116445888705806, 3.7094985019795996]) 文档,但没有找到有关如何访问输出的任何内容。

1 个答案:

答案 0 :(得分:1)

查看用于计算结果的代码会很有帮助,现在我只能猜测你做了什么。

我假设您的代码如下:

res = scipy.optimize.minimize(fun, f0, method='Powell')

function会返回optimizeResult个对象。要获得优化的实际结果,您需要致电

print(res.x)

res.x的类型是numpy.ndarray。如果您只想在计算成功时继续,那么您可以执行类似

的操作
if res.success:
    print('success, the results are')
    print(res.x)