小编Bru*_*wso的帖子

Python捕获所有打印输出

我期待在python中编写基于控制台的程序,它可以执行函数来执行通用任务,非常通用.是否有可能通过函数中的打印调用捕获写入控制台的所有内容,而无需返回字符串,类似于bash和windows shell如何允许将程序的输出传递给文本文件,即

IPCONFIG> ipconfig.txt

但是在调用函数的python程序中执行此操作时,打印到该函数内部控制台的所有内容都会收集为字符串列表,然后可以保存到用户选择的txt文件中?

python piping

11
推荐指数
1
解决办法
1865
查看次数

函数传递给scipy.optimize.curve_fit需要满足哪些特定要求才能运行?

我正在使用matplotlib hist函数将统计模型拟合到分布.例如,我的代码使用以下代码拟合指数分布:

    try:

        def expDist(x, a, x0):
            return a*(exp(-(x/x0))/x0)

        self.n, self.bins, patches = plt.hist(self.getDataSet(), self.getDatasetSize()/10, normed=1, facecolor='blue', alpha = 0.55)
        popt,pcov = curve_fit(expDist,self.bins[:-1], self.n, p0=[1,mean])
        print "Fitted gaussian curve to data with params a %f, x0 %f" % (popt[0], popt[1])
        self.a = popt[0]
        self.x0 = popt[1]

        self.fitted = True
    except RuntimeError:
        print "Unable to fit data to exponential curve"
Run Code Online (Sandbox Code Playgroud)

它运行很好,但是当我修改它做同样的事情之间的均匀分布ab,

    def uniDist(x, a, b):
        if((x >= a)and(x <= b)):
            return float(1.0/float(b-a))
        else:
            return 0.000

    try: …
Run Code Online (Sandbox Code Playgroud)

python numpy mathematical-optimization curve-fitting scipy

5
推荐指数
1
解决办法
194
查看次数

在python中为一对发行版生成MLE

好的,所以我当前的曲线拟合代码有一个步骤,使用scipy.stats根据数据确定正确的分布,

distributions = [st.laplace, st.norm, st.expon, st.dweibull, st.invweibull, st.lognorm, st.uniform]
mles = []

for distribution in distributions:
    pars = distribution.fit(data)
    mle = distribution.nnlf(pars, data)
    mles.append(mle)

results = [(distribution.name, mle) for distribution, mle in zip(distributions, mles)]

for dist in sorted(zip(distributions, mles), key=lambda d: d[1]):
    print dist
best_fit = sorted(zip(distributions, mles), key=lambda d: d[1])[0]
print 'Best fit reached using {}, MLE value: {}'.format(best_fit[0].name, best_fit[1])          


print [mod[0].name for mod in sorted(zip(distributions, mles), key=lambda d: d[1])]
Run Code Online (Sandbox Code Playgroud)

数据是数值列表.到目前为止,这对于拟合单峰分布非常有效,在脚本中确认,该脚本随机生成随机分布的值并使用curve_fit重新确定参数.

拟合的正态分布

现在我想使代码能够处理双峰分布,如下例所示:

正态和指数分布相结合

是否有可能从scipy.stats获取一对模型的MLE,以确定特定的一对分布是否适合数据?,类似于

distributions = [st.laplace, st.norm, st.expon, …
Run Code Online (Sandbox Code Playgroud)

python statistics distribution scipy model-fitting

5
推荐指数
1
解决办法
663
查看次数