Home > Blockchain >  scipy.minimize- missing 1 required positional argument
scipy.minimize- missing 1 required positional argument

Time:11-16

Have been stuck on this error for a while, also tried all the online resources but can not find a working solution anywhere.

I would like to use scipy.minimize function to find a minimum of my two variable function f(alpha, z). I get the following error:

"missing 1 required positional argument: 'z'"

I tried everything I knew, I am new to python so it is not much but tried my best. Can anyone please help me?

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

CodePudding user response:

The variables are passed on as a single array. You can unpack this inside your function:

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