modelNormal.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:scrap 作者: BruceJohnJennerLawso 项目源码 文件源码
def __init__(self, data, mleValue, fitParameters=True, mean=None, sigma=None):
        super(normalModel, self).__init__(data)




        self.MLE = mleValue

        if(None in [mean, sigma]):
            fitParameters=True
        if(fitParameters):
            mean = Mean(self.getDataSet())      
            sigma = standardDeviation(self.getDataSet())
            try:

                def normDist(x, x0, sigma):
                    output = 1.0/sqrt(2*np.pi*(sigma**2))
                    output *= exp(-0.5*((x - x0)/sigma)**2)
                    return output

                self.n, self.bins, patches = plt.hist(self.getDataSet(), self.getDatasetSize()/10, normed=1, facecolor='blue', alpha = 0.55)
                popt,pcov = curve_fit(normDist,self.bins[:-1], self.n, p0=[mean, sigma])
                ##plt.plot(bins[:-1], gaus(bins[:-1],*popt),'c-',label="Gaussian Curve with params\na=%f\nx0=%f\nsigma=%f" % (popt[0], popt[1], popt[2]), alpha=0.5)
                print "Fitted gaussian curve to data with params x0 %f, sigma %f" % (popt[0], popt[1])
                self.x0 = popt[0]
                self.sigma = popt[1]

                self.fitted = True
            except RuntimeError:
                print "Unable to fit data to normal curve, runtime error"
                raise
            except Warning:
                raise RuntimeError
        else:
            self.x0 = mean
            self.sigma = sigma
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号