简体   繁体   中英

curve fitting by curve_fit from scipy in Python

I am trying to fit my data with an exponential function

import numpy as np

def exponentional(k, alpha, k0, c):
    return k0 * np.exp(k *-alpha) + c 

I used curve_fit from scipy.optimize,

from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
uniq_deg = [2,...,103,..,203,...,307,...,506] 
normalized_deg_dist = [0.99,...,0.43,..0.12,..,0.04,..., 0.01]
           
            
popt, pcov = curve_fit(exponentional, uniq_deg, normalized_deg_dist, 
                       p0 = [1,0.00001,1,1], maxfev = 6000)
           
fig = plt.figure() 
ax = fig.add_subplot(111)
             
ax.semilogy(uniq_deg, normalized_deg_dist, 'bo', label = 'Real data') 
ax.semilogy(uniq_deg,[exponentional(d,*popt) for d in uniq_deg], 'r-', label = 'Fit') 
ax.set_xlabel('Degree' ) 
ax.set_ylabel('1-CDF degree') 
ax.legend(loc='best')
ax.set_title(f'Degree distribution in {city}')
plt.show()

Resulting in:

在此处输入图片说明

It does not look like a good fit.

Where I am wrong?

Finally, I did not use curve_fit . I used the definition of the exponential fit from https://mathworld.wolfram.com/LeastSquaresFittingExponential.html

and also I need to fit some other data by power-law I did the same.

    #%%
def fit_powerlaw(xs, ys):
    S_lnx_lny = 0.0
    S_lnx_S_lny = 0.0
    S_lny = 0.0
    S_lnx = 0.0
    S_lnx2 = 0.0
    S_ln_x_2 = 0.0
    n = len(xs)
    for (x,y) in zip(xs, ys):
        S_lnx += np.log(x)
        S_lny += np.log(y)
        S_lnx_lny += np.log(x) * np.log(y)
        S_lnx_S_lny = S_lnx * S_lny
        S_lnx2 += np.power(np.log(x),2)
        S_ln_x_2 = np.power(S_lnx,2)
    #end
    b = (n * S_lnx_lny - S_lnx_S_lny ) / (n * S_lnx2 - S_ln_x_2)
    a = (S_lny - b * S_lnx)  / (n)
    return (np.exp(a), b)
#%%
def fit_exp(xs, ys):
    S_x2_y = 0.0
    S_y_lny = 0.0
    S_x_y = 0.0
    S_x_y_lny = 0.0
    S_y = 0.0
    for (x,y) in zip(xs, ys):
        S_x2_y += x * x * y
        S_y_lny += y * np.log(y)
        S_x_y += x * y
        S_x_y_lny += x * y * np.log(y)
        S_y += y
    #end
    a = (S_x2_y * S_y_lny - S_x_y * S_x_y_lny) / (S_y * S_x2_y - S_x_y * S_x_y)
    b = (S_y * S_x_y_lny - S_x_y * S_y_lny) / (S_y * S_x2_y - S_x_y * S_x_y)
    return (np.exp(a), b)

在此处输入图片说明

在此处输入图片说明

First graph for exponential fitting and second for power law. I think the result is convincing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM