scipy polyfit x,y,weights =误差条

时间:2014-09-26 05:02:09

标签: python scipy curve-fitting least-squares

我想拟合一条使用误差反转的线 酒吧作为重量。

我将我的数据x和y分成10个箱子(每个约26个点)并取其平均值。 这不是一个价值矩阵,所以polyfit对此并不满意。

# note I didn't test this pseudo code....
import numpy as np
import scipy

x = np.random.randn(100)
y = np.random.rand(100)

x_bins = np.linspace(x.min(), x.max(), 10) # reduction of data into 10 bins 
y_bins = np.linspace(y.min(), y.max(), 10) 

x_bin = np.digitize(x, x_bins)
y_bin = np.digitize(y, y_bins)

x_mu = np.zeros(10)
y_mu = x_mu.copy()
err = x_mu.copy()   

for i in range(10):
    x_mu = np.mean(x[x_bin==i]) 
    y_mu = np.mean(y[y_bin==i])
    err = np.std([y[y_bin==i])


x_mu[np.isnan(x_mu)] = 0
y_mu[np.isnan(y_mu)] = 0
errror[np.isnan(error)] = 0

plt.errorbar(x_mu, y_mu, err, fmt='o')

编辑:scipy.polyfit停止抱怨病态的输入......

out = scipy.polyfit(x_mu, y_mu, deg=1, w=error)

1 个答案:

答案 0 :(得分:1)

numpy.polyfit不允许您明确指定不确定性。相反,您可以使用scipy.optimize.curve_fit,例如

import numpy as np
import scipy
import scipy.optimize

x = np.linspace(0,1, 100)
y = np.random.rand(100)

# bin the data
n, bins = np.histogram(y, 10, [0, 1])
xb = bins[:-1] + 0.05  # at bin center; has overflow bin
yb = n                 # just the per-bin counts
err = sqrt(n)          # from Poisson statistics
plt.errorbar(xb, yb, err, fmt='ro')

# fit a polynomial of degree 1, no explicit uncertainty
a1, b1 = np.polyfit(xb, yb, 1)
plt.plot(xb, a1*xb + b1, 'b') 

# fit explicitly taking uncertainty into account
f = lambda x, a, b: a*x + b  # function to fit
# fit with initial guess for parameters [1, 1]
pars, corr = scipy.optimize.curve_fit(f, xb, yb, [1, 1], err)
a2, b2 = pars
plt.plot(xb, a2*xb + b2, 'r')

enter image description here

要正确解释所需的拟合,以检查拟合参数的相关矩阵,但这超出了这个技术问题。