scipy.optimize.curve_fit 与 matplotlib.pyplot.hist 直方图和密度图

    matplotlib.pyplot.hist(  
    x, bins=10, range=None, normed=False,   
    weights=None, cumulative=False, bottom=None,   
    histtype=u'bar', align=u'mid', orientation=u'vertical',   
    rwidth=None, log=False, color=None, label=None, stacked=False,   
    hold=None, **kwargs)  
  • x : 要画直方图的数据。
  • bins :指定bin(箱子)的个数。也可以是list序列,表示bins的左边界和右边界。
  • density(也就是normed参数): 设置为True,返回值的第一个是概率密度, 直方图下的面积(或积分)之和为1.

(density : bool, optionalIf True, the first element of the return tuple will be the counts normalized to form a probability density, i.e., the area (or integral) under the histogram will sum to 1. This is achieved by dividing the count by the number of observations times the bin width and not dividing by the total number of observations. If stacked is also True, the sum of the histograms is normalized to 1.Default is None for both normed and density. If either is set, then that value will be used. If neither are set, then the args will be treated as False.If both density and normed are set an error is raised.)

返回值:

  • n : 直方图bins的值,对应在y轴上。
  • bins : bins+1个值,对应画bins的位置,在x轴上。
  • patches : Silent list of individual patches used to create the histogram or list of such list if multiple input datasets.(没看过)

例子2:


随机生成一些数字:daily_maxes

daily_maxes.shape (10000,)

# probs:y值
# hungers:x值
probs, hungers, _ = hist(daily_maxes, normed=True, bins=100)
  • curve_fit
scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, 
check_finite=True, bounds=(-inf, inf), method=None, **kwargs)

用途:Use non-linear least squares to fit a function, f, to data.Assumes ydata = f(xdata, *params) + eps
参数函数f拟合一个给定的数据,返回拟合数据时的参数值。
返回:

  • popt : 最优的拟合参数值;
  • pcov:The estimated covariance of popt.(参数的估计协方差)
# 定义一个函数,含有两个超参数,loc和scale,求这两个参数。
def gumbel_pdf(prob, loc, scale):
    z = (prob - loc) / scale
    return exp(-z - exp(-z)) / scale
# 传入函数,x值,y值
(loc, scale), _ = curve_fit(gumbel_pdf, hungers[:-1], probs)

curve_fit 官网解释:http://lagrange.univ-lyon1.fr/docs/scipy/0.17.1/generated/scipy.optimize.curve_fit.html

  • 直方图:

    例子:https://www.cnblogs.com/python-life/articles/6084059.html
    官网解释:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html

    直方图的缺点,密度图的优点:https://towardsdatascience.com/histograms-and-density-plots-in-python-f6bda88f5ac0

另外:
关于可视化的python手册:https://serialmentor.com/dataviz/histograms-density-plots.html

你可能感兴趣的:(matplotlib)