scipy.minimize- 缺少 1 个必需的位置参数
scipy.minimize- missing 1 required positional argument
一直卡在这个错误上,也尝试了所有在线资源,但找不到任何有效的解决方案。
我想使用 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]
. . .
一直卡在这个错误上,也尝试了所有在线资源,但找不到任何有效的解决方案。
我想使用 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]
. . .