Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

scipy.minimize- missing 1 required positional argument

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’"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]
         . . .
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading