来自scipy的curve_fit进行曲线拟合

问题描述

我正在尝试使用指数函数拟合数据

import numpy as np

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

我从scipy.optimize使用了curve_fit

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,'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()

结果:

enter image description here

它看起来不太合适。

哪里错了?

解决方法

最后,我没有使用curve_fit。我使用了https://mathworld.wolfram.com/LeastSquaresFittingExponential.html

的指数拟合的定义

同样,我也需要根据幂律来拟合其他数据。

    #%%
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,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)

enter image description here

enter image description here

第一个图形用于指数拟合,第二个图形用于幂律。 我认为结果令人信服。