Matlab的fminsearch的numpy/scipy类似物 [英] numpy/scipy analog of matlab's fminsearch
问题描述
我正在使用 numpy 将一些Matlab代码转换为python.一切工作都非常顺利,但是最近我遇到了 fminsearch 函数.
I am converting some Matlab code into python using numpy. Everything worked pretty smoothly but recently I encountered fminsearch function.
因此,简而言之:是否有一种简单的方法可以在python中制作像这样的东西:
So, to cut it short: is there an easy way to make in python something like this:
banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;
[x,fval] = fminsearch(banana,[-1.2, 1])
将返回
x = 1.0000 1.0000
fval = 8.1777e-010
到目前为止,我还没有发现类似numpy的任何东西.我发现类似的唯一东西是 scipy.optimize.fmin .根据定义
Up till now I have not found anything that looks similar in numpy. The only thing that I found similar is scipy.optimize.fmin. Based on the definition it
使用下坡单纯形算法最小化功能.
Minimize a function using the downhill simplex algorithm.
但是现在我找不到使用此功能编写上述Matlab代码
But right now I can not find to write the above-mentioned Matlab code using this function
推荐答案
这只是从Matlab语法到python语法的直接转换:
It's just a straight-forward conversion from Matlab syntax to python syntax:
import scipy.optimize
banana = lambda x: 100*(x[1]-x[0]**2)**2+(1-x[0])**2
xopt = scipy.optimize.fmin(func=banana, x0=[-1.2,1])
输出:
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 85
Function evaluations: 159
array([ 1.00002202, 1.00004222])
这篇关于Matlab的fminsearch的numpy/scipy类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!