我想使用fmin查找功能的最小值,但是出现以下错误:

TypeError: <lambda>() takes 1 positional argument but 2 were given

有问题的代码如下:
import numpy as np
from scipy.optimize import fmin

g = lambda alpha: np.sum(np.square(np.subtract(D, (avec[0]-alpha*grad)*f((avec[1]-alpha*grad),y))))

b = fmin(g,0.0)

你能告诉我如何解决这个问题吗?

整个代码在这里:
from scipy.optimize import fmin
from scipy import interpolate
import numpy as np



Emax = 10;
bins = 200;

x = np.linspace(1, Emax, num = Emax, dtype=np.int)   #create grid of indexes
y = np.linspace(1, bins, num = bins, dtype=np.int)
z = np.random.rand(bins, Emax)                       # response matrix
f = interpolate.interp2d(x,y,z, kind='cubic')        # make the matrix continious

D= np.zeros(bins)
D = 1*f(1.5, y) + 3*f(2.5, y)   # signal

iterations = 1000
step = 1e-5

avec = np.array([1.0,2.0])   # chosen starting parameters
grad = np.array([0.0,0.0])
chix_current = np.arange(iterations, dtype=float)

#gradient unfolding

for i in range(0, iterations):
    fx = avec[0]*f(avec[1], y)     # evaluation in every layer
    chi = np.square(np.subtract(D,fx))    #chi function
    chi_a = np.square(np.subtract(D,  (avec[0]+step)*f(avec[1],y)))
    chi_b = np.square(np.subtract(D,  avec[0]*f((avec[1]+step),y)))

    chisquared = np.sum(chi)
    chisquared_a = np.sum(chi_a)
    chisquared_b = np.sum(chi_b)

    grad[0] = np. divide(np.subtract(chisquared_a, chisquared), step)
    grad[1] = np.divide(np.subtract(chisquared_b, chisquared), step)



    g= lambda alpha: np.sum(np.square(np.subtract(D,  (avec[0]-alpha*grad)*f((avec[1]-alpha*grad),y)) ))
    b= fmin(g,(0.0))


    avec= np.subtract(avec, 1e5*grad )

最终,当函数g最小时,我只需要知道alpha的值,并在最后一行中使用它代替1e5。

最佳答案

您的代码不太清楚,某些方法和变量不明确。

从您给出的结果来看,lambda函数f()似乎应该接受1个参数,但是在f()内部您又递归地调用f(),但是内部f()需要2个参数,我认为这是一个错字。

尝试将lambda方法重命名为没有冲突的其他名称,例如g:

g = lambda alpha: np.sum(np.square(np.subtract(D, (avec[0]-alpha*grad)*f((avec[1]-alpha*grad),y))))
b = fmin(g,0.0)

关于python - “takes 1 positional argument but 2 were given”,同时找到最少的功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53746366/

10-09 01:15
查看更多