python scipy曲线适合不工作

问题描述 投票:0回答:1

我正在尝试拟合对数正态分布:

import numpy as np
import scipy.stats as sp
from scipy.optimize import curve_fit


def pdf(x, mu, sigma):
    return (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))


x_axis = [5e5,1e6,2e6,5e6,6e6]
y_axis = [0,0.2,0.4,0.6,0.8]
curve_fit(pdf,x_axis,y_axis,maxfev=10000,)

这将返回以下内容:

C:\Anaconda3\Lib\site-packages\scipy\optimize\minpack.py:604: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)
Out[66]: 
(array([ 1.,  1.]), array([[ inf,  inf],
        [ inf,  inf]]))

这些结果看起来并不合适。我知道只有五个数据点但是当我在excel中使用求解器时,我得到的参数为0.1536和3.1915,这并不完美,但它更接近。

编辑:用cdf尝试这个

def cdf(x,mu,sigma):
    return sp.norm.cdf((np.log(x)-mu)/sigma)

curve_fit(cdf,x_axis,y_axis,)

这会返回与上面相同的错误

python curve-fitting
1个回答
2
投票

你有可视化数据吗?

x_axisy_axis的给定值如下所示:

x- and y-data

如果您使用给定的x_axis值和mu=0.1536sigma=3.1915的Excel求解器值,然后可视化pdf,您会得到:

fit

所以我想知道你想得到什么结果?

实际上,第一张图片中的数据看起来并不像Log-normal pdf,是吗?

© www.soinside.com 2019 - 2024. All rights reserved.