1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] scipy.optimize.curve_fit stopped working after running several iterations

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 18:02.

  1. Stack

    Stack Membro Participativo

    ----- updates 2:

    when i was stuck here, I also revised my codes to using optuna -- and looks like optuna works better than curve_fit.

    ----- updates 1:

    the issue was due to "maxfev", and it was solved after I increased the value of "maxfev". for some reason that I don't know, on my machine, scipy did not raise any exception to me -- i'll reinstall scipy.

    Thank you all who helped me diagonose this problem.

    ----- original post:

    I wanted to use scipy.optimize.curve_fit to fit the data, and I have more than 10000 data sets to fit, so I used a for loop to run curve_fit for 10000 times. However, curve_fit only worked successfully for the first 49 iterations, and after that curve_fit stopped working--to be more specific, the curve_fit function did not return anything, and it did not raise any exceptions or warnings either. What could be the reason and how to fix it?

    I've included my code below. When I run it, it prints i up to 48, and then exits the for loop. There are no errors, warnings, return values, or anything.

    def irt(x, alpha, beta):
    return 1 / (1 + np.exp(0 - alpha * (x - beta)))

    for i in range(10000):
    """ I did something here to process the data, and these lines are skipped for simplicity """
    popt, pcov = scipy.optimize.curve_fit(irt, xdata, ydata, p0=[1.0, 0.5], full_output=False)
    print(i)



    Please see the following example, I run the following lines in the console after running curve_fit for 49 iterations. "ress" is the return value, but nothing was returnned. (no matter what values are inputed, i got nothing from the curve_fit function)

    theta = np.array([0.80748663, 0.86585366, 0.8245614 , 0.79310345, 0.63513514, 0.82926829, 0.73684211, 0.91358025, 0.79310345, 0.81818182])
    correct = np.array([0., 1., 1., 1., 1., 1., 1., 0., 1., 1.])
    ress = scipy.optimize.curve_fit(irt, theta, correct, p0=[1.0, 0.8181818181818182])
    ress

    Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
    ^^^^^^
    File "<input>", line 1, in <module>
    NameError: name 'ress' is not defined.


    If i just run the curve_fit function in the console[​IMG], it didn't return anything:

    Continue reading...

Compartilhe esta Página