当轴为对数Python时拟合指数分布

问题描述

我一直在尝试使用以下代码拟合指数分布:

import numpy as np
from scipy.optimize import curve_fit



###########  Just to create the example,not needed #######
import random
nums = []  
lambd = 3
    
for i in range(10000):  
    temp = random.expovariate(lambd) 
    nums.append(temp)  
def selection_sort(x):
    for i in range(len(x)):
        swap = i + np.argmin(x[i:])
        (x[i],x[swap]) = (x[swap],x[i])
    return x
aa=selection_sort(nums)
bb=1/np.array(aa)#.to_numpy()
######################################


####  The code to fit the distribution


def expo(x,a,b) :
    return a * np.exp(x*b)
def linexpo(x,b) :
    return a + np.log10(np.exp(x*b)) ### I think the mistake is here right?

def curve_fit_log(xdata,ydata) :
    """Fit data to a an exponential with weights according to a log scale"""
    # Weights according to a log scale
    # Apply fscalex
    xdata_log = np.log10(xdata)
    # Apply fscaley
    ydata_log = np.log10(ydata)
    # Fit linear
    popt_log,pcov_log = curve_fit(linexpo,xdata_log,ydata_log)
    #print(popt_log,pcov_log)
    # Apply fscaley^-1 to fitted data
    ydatafit_log = np.power(10,linexpo(xdata_log,*popt_log))
    # There is no need to apply fscalex^-1 as original data is already available
    return (popt_log,pcov_log,ydatafit_log)

#### The coefficients of the fit #######3

aq,ad,af=curve_fit_log(np.linspace(1000,10000,9000),bb[1000:10000])


########## Plot the data along with the fit #########

plt.scatter(np.linspace(1,10000),bb)
plt.plot(np.linspace(1000,af,color='red')
#plt.scatter(half2,relative2,s=40,facecolors='none',edgecolors='r')
plt.yscale("log") 
plt.xscale("log")

我得到的结果看起来像硫代,这显然是不正确的:

enter image description here

它需要在某个地方进行少量更改,但无法识别!有人可以提供帮助吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...