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.xml 中找到任何看起来相似的东西.我发现唯一相似的是 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屋!