我如何找到正常dist的方程最适合yip scipy

时间:2018-06-15 11:09:04

标签: python scipy

我在python中使用scipy库来执行多重分发 并驾驶图表。 并转换为png文件。 但问题是我没有得到特定分布的最佳拟合方程。 你可以帮我解决方程并将其显示在图表中。

import io
    import numpy as np
    import pandas as pd
    import scipy.stats as stats
    import matplotlib
    import matplotlib.pyplot as plt

matplotlib.rcParams['figure.figsize'] = (16.0, 14.0)
matplotlib.style.use('ggplot')
mean = 0.47
std = 0.24
DISTRIBUTIONS = [
    stats.norm(loc=mean, scale=std), 
    stats.powerlaw(a=1.66, loc=mean, scale=std), 
    stats.t(df=2.74, loc=mean, scale=std),
]

bins = 32
size = 16384
plotData = []
for distribution in DISTRIBUTIONS:
    try:
        # Create random data
        rv = pd.Series(distribution.rvs(size=size))
        # Get sane start and end points of distribution
        start = distribution.ppf(0.01)
        end = distribution.ppf(0.99)

        # Build PDF and turn into pandas Series
        x = np.linspace(start, end, size)
        y = distribution.pdf(x)
        pdf = pd.Series(y, x)

        # Get histogram of random data
        b = np.linspace(start, end, bins+1)
        y, x = np.histogram(rv, bins=b, normed=True)
        x = [(a+x[i+1])/2.0 for i,a in enumerate(x[0:-1])]
        hist = pd.Series(y, x)

        # Create distribution name and parameter string
        title = '{}({})'.format(distribution.dist.name, ', '.join(['{}={:0.2f}'.format(k,v) for k,v in distribution.kwds.items()]))

        # Store data for later
        plotData.append({
            'pdf': pdf,
            'hist': hist,
            'title': title
        })

    except Exception:
        print('could not create data', distribution.dist.name)

plotMax = len(plotData)

for i, data in enumerate(plotData):
    w = abs(abs(data['hist'].index[0]) - abs(data['hist'].index[1]))

    # Display
    plt.figure(figsize=(10, 6))
    ax = data['pdf'].plot(kind='line', label='Model PDF', legend=True, lw=2)
    ax.bar(data['hist'].index, data['hist'].values, label='Random Sample', width=w, align='center', alpha=0.5)
    ax.set_title(data['title'])

    # Grab figure
    fig = matplotlib.pyplot.gcf()
    # Output 'file'
    fig.savefig(data['title']+'.png', format='png', bbox_inches='tight')
    # fig.savefig('~/Desktop/dist/' + data['title'] + '.png', format='png', bbox_inches='tight')
    matplotlib.pyplot.close()

0 个答案:

没有答案