如何删除线性拟合的1 sigma std以外的数据

时间:2018-12-30 08:42:27

标签: python python-3.x python-2.7

首先,非常感谢您的帮助。我有一个非常大的数据,并且我在python中使用线性拟合来拟合此数据。现在,我想删除拟合1 sigma区域(红色区域)之外的所有数据点。我已经在考虑解决方案已有一段时间了,但是我什么也没想出来。有任何想法吗?

欢呼 Justyna

这是我的代码

from numpy import *
from lmfit.models import *
import matplotlib.pyplot as plt
data = loadtxt('data.txt')
x = data[:, 0] 
y = data[:, 1]
yerr = np.absolute(0.1 * y)


def line(x, slope, intercept):
    """a line"""
    return slope*x + intercept

mod = Model(line)
pars  = mod.make_params(slope=1, intercept=0.5)

result = mod.fit(y, pars, x=x)
print(result.fit_report())

# extract components of Model
comps = result.eval_components(result.params, x=x)


final_min = result.best_fit - (std(result.best_fit))
final_max = result.best_fit + (std(result.best_fit))

plt.plot(x, y, 'ks')

plt.plot(x, result.best_fit, 'r-')
plt.plot(x, final_min, 'r--')
plt.plot(x, final_max, 'r--')
plt.xlim(11.75,23.5)
plt.ylim(0,2)
plt.show()

这是图片https://i.stack.imgur.com/PxyDq.png

0 个答案:

没有答案