0

I checked the EES I feel it should be right, but the figure out-put is so strange. I hope some one can help me. I am quite newbee to Python and statistics. I don't konw if it is normal, but the same data I use matlab the result seems quite ok.

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import scipy.stats as st
import seaborn as sns
import numpy as np

data = [ 40, 21, 14, 9, 14, 32, 28, 42, 33, 30, 40, 14, 11, 32,
  53, 163,  28,  13,  14,  15,  16,  15,  12,   9,  12,  21,  41,  24,
  12,  26,  14,  36,  22,   4,  36,  13,  71,  10,  30,  17,  89,  31,
  30,  13,  37,  22,  30,  32,  31,  20,  19,  53,  18,  30,  31,  11,
  39,  37,  12,  26,   9,   9,]

sns.set()
DISTRIBUTIONS = [st.norm,st.gamma,st.weibull_min,st.burr,st.beta]
bins_=20

def cal_sse(fitting,data,bins):
    y, x = np.histogram(data , bins=bins)
    bin_width = x[1]-x[0]
    x_mid = (x + np.roll(x, -1))[:-1] / 2.0
    N = len(data)
    params = fitting.fit(data)
    arg = params[:-2]
    loc = params[-2]
    scale = params[-1]
    pdf = fitting.pdf(x_mid, loc=loc, scale=scale, *arg)
    pdf_scaled = pdf * bin_width * N
    sse = np.sum((y - pdf_scaled)**2)
    if fitting == st.weibull_min:
        print(y,x)
        print('------------------')
        print(pdf_scaled )
    return sse

def draw_fitting(data,bins,fittings):
    for fit in range(len(fittings)):
        if fit == 0:
            sns.distplot(data,bins=bins, kde=False, fit=fittings[fit],fit_kws={"color": "g", "linewidth": 3,"dashes":[6, 2]})
            print(cal_sse(fittings[fit],data,bins))
        else:
            sns.distplot(data,bins=bins, kde=False ,fit=fittings[fit],hist_kws={"alpha":0})
            print(cal_sse(fittings[fit],data,bins))

draw_fitting(data,bins_,DISTRIBUTIONS)
plt.show()

the result

Rohan Nadagouda
  • 462
  • 7
  • 18
  • What do you mean by 'the figure output is strange'? What did you expect to get? – Andrew Fan Dec 27 '18 at 21:25
  • Thanks, did you click 'the result' ? It's the result picture, cause I am not permit to paste images, and also you can run the script your self. I mean the weibull fitting should be close to original data not like this. I found somebody say should use parameter 'loc = 0' for weibull_min, but I don't konw how. – Dachuan Wang Dec 27 '18 at 22:14
  • If your question is about the result for `weibull_min`, then it would be easier for readers to understand the problem if you simplified the code to include only the `weibull_min` fit results. – Warren Weckesser Dec 28 '18 at 01:01
  • *"I found somebody say should use parameter 'loc = 0' for weibull_min, but I don't konw how."* See, for example, https://stackoverflow.com/questions/33070724/determine-weibull-parameters-from-data/33079243#33079243 – Warren Weckesser Dec 28 '18 at 01:10

0 Answers0