简体   繁体   中英

python scipy curve fit not working

I'm trying to fit a lognormal distribution:

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

This returns the following:

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]]))

These results don't really seem like a great fit. I know there are only five datapoints but when I use solver in excel I get the parameters of 0.1536 and 3.1915, which isn't perfect, but it is much closer.

edit: trying this with a cdf

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

curve_fit(cdf,x_axis,y_axis,)

this returns the same error as above

Have you visualized the data?

The given values for x_axis and y_axis look like this:

x和y数据

If you use the given x_axis values and your Excel solver values for mu=0.1536 and sigma=3.1915 and then visualize the pdf you get this:

适合

So I wonder what result you want to get?

Actually the data in the first picture does not really look like a Log-normal pdf, does it?

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