当使用scipy.optimize.fmin_bfgs时,我得到TypeError:f()缺少1个必需的位置参数:(When using scipy.optimize.fmin_bfgs I got TypeError: f() missing 1 required positional argument:)

我正在尝试使用scipy.optimize.fmin_bfgs()函数计算六驼峰驼峰函数的最小值。 这是我的代码:

import numpy as np import matplotlib.pyplot as plt from scipy import optimize def f(x,y): return (4 - 2.1*x**2 + x**4/3)*x**2 + x*y + (4*y**2 - 4)*y**2 x0 = [0,0] optimize.fmin_bfgs(f, x0)

输出:

TypeError: f() missing 1 required positional argument: 'y'

我猜我传递x0的方式有问题吗?

I'm trying to calculate minima of the six-hump camelback function using scipy.optimize.fmin_bfgs() function. Here is my code:

import numpy as np import matplotlib.pyplot as plt from scipy import optimize def f(x,y): return (4 - 2.1*x**2 + x**4/3)*x**2 + x*y + (4*y**2 - 4)*y**2 x0 = [0,0] optimize.fmin_bfgs(f, x0)

Output:

TypeError: f() missing 1 required positional argument: 'y'

I guess there is something wrong with the way I pass x0?

最满意答案

根据这个页面, f应该有一个数组参数: http : //docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

改为:

def f(x): return (4 - 2.1*x[0]**2 + x[0]**4/3)*x[0]**2 + x[0]*x[1] + (4*x[1]**2 - 4)*x[1]**2 x0 = [0,0] optimize.fmin_bfgs(f,x0)

Per this page there should be one array argument to f: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

Do instead:

def f(x): return (4 - 2.1*x[0]**2 + x[0]**4/3)*x[0]**2 + x[0]*x[1] + (4*x[1]**2 - 4)*x[1]**2 x0 = [0,0] optimize.fmin_bfgs(f,x0)

更多推荐