Search code examples
pythonnumpymatplotlibcurve-fittinglogarithm

Python using curve_fit to fit a logarithmic function


I'm trying to fit a log curve using curve_fit, assuming it follows Y=a*ln(X)+b, but the fitted data still looks off.

Right now I'm using the following code:

from scipy.optimize import curve_fit
X=[3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4,
   4.5, 4.6, 4.7]
Y=[-5.890486683, -3.87063815, -2.733484754, -2.104972457, -1.728190699, 
   -1.477976987, -1.285589215, -1.120224363, -0.968576581, -0.82492453, 
   -0.688457731, -0.559780327, -0.440437932, -0.331886009, -0.235162505, 
   -0.150572236, -0.078157925, -0.01718885]

#plot Y against X
fig = plt.figure(num=None, figsize=(9, 7),facecolor='w', edgecolor='k')
ax2=fig.add_subplot(111)
ax2.scatter(X,Y)

#fit using curve_fit
popt, pcov = curve_fit(Hyp_func, X, Y,maxfev=10000)
print(' fit coefficients:\n', popt)
#fit coefficients:
#[9.51543579 -14.10114674]

#plot Y_estimated against X
Y_estimated=[popt[0]*np.log(i)+popt[1] for i in X]
ax2.scatter(X,Y_estimated, c='r')
def Hyp_func(x, a,b):
    return a*np.log(x)+b

enter image description here

the fitted curve (red) still looks not as 'curvy' like the read curve (blue). Any help would be appreciated.


Solution

  • The X data values sometimes need to be shifted a bit for this equation, and when I tried this it worked rather well. Here is a graphical Python fitter using your data and an X-shifted equation "y = a * ln(x + b)+c".

    enter image description here

    import numpy, scipy, matplotlib
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    
    # ignore any "invalid value in log" warnings internal to curve_fit() routine
    import warnings
    warnings.filterwarnings("ignore")
    
    X=[3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7]
    Y=[-5.890486683, -3.87063815, -2.733484754, -2.104972457, -1.728190699, -1.477976987, -1.285589215, -1.120224363, -0.968576581, -0.82492453, -0.688457731, -0.559780327, -0.440437932, -0.331886009, -0.235162505, -0.150572236, -0.078157925, -0.01718885]
    
    # alias data to match previous example
    xData = numpy.array(X, dtype=float)
    yData = numpy.array(Y, dtype=float)
    
    def func(x, a, b, c): # x-shifted log
        return a*numpy.log(x + b)+c
    
    # these are the same as the scipy defaults
    initialParameters = numpy.array([1.0, 1.0, 1.0])
    
    # curve fit the test data
    fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
    
    modelPredictions = func(xData, *fittedParameters) 
    
    absError = modelPredictions - yData
    
    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
    
    print('Parameters:', fittedParameters)
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)
    
    print()
    
    
    ##########################################################
    # graphics output section
    def ModelAndScatterPlot(graphWidth, graphHeight):
        f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
        axes = f.add_subplot(111)
    
        # first the raw data as a scatter plot
        axes.plot(xData, yData,  'D')
    
        # create data for the fitted equation plot
        xModel = numpy.linspace(min(xData), max(xData))
        yModel = func(xModel, *fittedParameters)
    
        # now the model as a line plot
        axes.plot(xModel, yModel)
    
        axes.set_xlabel('X Data') # X axis data label
        axes.set_ylabel('Y Data') # Y axis data label
    
        plt.show()
        plt.close('all') # clean up after using pyplot
    
    graphWidth = 800
    graphHeight = 600
    ModelAndScatterPlot(graphWidth, graphHeight)