scipy.minimize-缺少1个必需的位置参数



这个错误已经困扰了一段时间,也尝试了所有的在线资源,但在任何地方都找不到有效的解决方案。

我想使用scipy.minimize函数来找到我的双变量函数f(alpha,z(的最小值。我得到以下错误:

"缺少1个必需的位置参数:"z";

我尝试了我所知道的一切,我是python的新手,所以它不多,但我已经尽力了。有人能帮我吗?

def calculate_loss(alpha, z):
scaled_data = []
for ind, L in enumerate(LLs):
scaled_data.append(data[ind][:,1:45])
scaled_data[ind][0] = np.log10(scaled_data[ind][0]/(L**z))
scaled_data[ind][1] = np.log10(scaled_data[ind][1]/(L**alpha))
a = []
b = []
for ind, L in enumerate(LLs):
popt, pcov = curve_fit(f, scaled_data[ind][0], scaled_data[ind][1])
a.append(popt[0])
b.append(popt[1])
N = len(LLs)
loss = 0
for i in range(0,N):
for j in range(i+1,N):
#tu musim doplnit loss function
loss += np.sum((scaled_data[ind][1] - (a[j]*(scaled_data[ind][0])+b[j]))**2)

return loss
initial = [1, 2]
res = minimize(calculate_loss, initial)
print(res.x)
fitted = res.x

变量作为单个数组传递。你可以在你的功能中打开这个:

def calculate_loss(x):
alpha = x[0]
z = x[1]
. . .

最新更新