[Python] how to set up the initial value for curve_fit to find the best optimizing, not just...

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to fit a power-law function, and in order to find the best fit parameter. However, I find that if the initial guess of parameter is different, the "best fit" output is different. Unless I find the right initial guess, I can get the best optimizing, instead of local optimizing. Is there any way to find the **appropriate initial guess ** ????. My code is listed below. Please feel free make any input. Thanks!

    import numpy as np
    import pandas as pd
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt
    %matplotlib inline

    # power law function
    def func_powerlaw(x,a,b,c):
    return a*(x**b)+c

    test_X = [1.0,2,3,4,5,6,7,8,9,10]
    test_Y =[3.0,1.5,1.2222222222222223,1.125,1.08,1.0555555555555556,1.0408163265306123,1.03125, 1.0246913580246915,1.02]

    predict_Y = []
    for x in test_X:
    predict_Y.append(2*x**-2+1)

    If I align with default initial guess, which p0 = [1,1,1]


    popt, pcov = curve_fit(func_powerlaw, test_X[1:], test_Y[1:], maxfev=2000)


    plt.figure(figsize=(10, 5))
    plt.plot(test_X, func_powerlaw(test_X, *popt),'r',linewidth=4, label='fit: a=%.4f, b=%.4f, c=%.4f' % tuple(popt))
    plt.plot(test_X[1:], test_Y[1:], '--bo')
    plt.plot(test_X[1:], predict_Y[1:], '-b')
    plt.legend()
    plt.show()


    The fit is like below, which is not the best fit. [​IMG]

    If I change the initial guess to p0 = [0.5,0.5,0.5]


    popt, pcov = curve_fit(func_powerlaw, test_X[1:], test_Y[1:], p0=np.asarray([0.5,0.5,0.5]), maxfev=2000)


    I can get the best fit [​IMG]

    ---------------------Updated in 10.7.2018-------------------------------------------------------------------------------------------------------------------------

    As I need to run thousands to even millions of Power Law function, using @James Phillips's method is too expensive. So what method is appropriate besides curve_fit? such as sklearn, np.linalg.lstsq etc.

    Continue reading...

Compartilhe esta Página