使用曲线拟合拟合数据然后找出数据是否高于/低于曲线拟合

Fitting Data w/ Curve Fit Then Find Out if Data is above / below curve fit

我生成了适合以下代理数据的指数曲线。将模型拟合到数据后,我需要逐行确定另一个样本 second sample 是在拟合线上方还是下方。我苦苦挣扎的地方是如何确定 second sample 数据是高于还是低于拟合线。

#Sample Data for generating the exponential curve fit model
ys = np.array([40951088., 35375058., 22160211., 21306439., 20980581., 15379697.,
       10308974., 16793804.,  6867746.,  5952455.,  4505347.,  4768728.,
        5254116.,  2183644.,  3350415.,  1992107.,  1449918.,   985307.,
        2804293.,  2515258.,   884647.,   251409.,   901582.])

xs = np.array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ,
        5.5,  6. ,  6.5,  7. ,  7.5,  8. ,  8.5,  9. ,  9.5, 10. , 10.5,
       11.])

def monoExp(x, m, t, b):
    return m * np.exp(-t * x) + b

# perform the fit
p0 = (2000, .1, 50) #approximate values
params, cv = scipy.optimize.curve_fit(monoExp, xs, ys, p0, maxfev=5000)
m, t, b = params
sampleRate = 20_000 # Hz
tauSec = (1 / t) / sampleRate

# determine quality of the fit
squaredDiffs = np.square(ys - monoExp(xs, m, t, b))
squaredDiffsFromMean = np.square(ys - np.mean(ys))
rSquared = 1 - np.sum(squaredDiffs) / np.sum(squaredDiffsFromMean)
print(f"R² = {rSquared}")

# plot the results
plt.plot(xs, ys, '.', label="data")
plt.plot(xs, monoExp(xs, m, t, b), '--', label="fitted")
plt.title("Fitted Exponential Curve")

# inspect the parameters
print(f"Y = {m} * e^(-{t} * x) + {b}")
print(f"Tau = {tauSec * 1e6} µs")

这是上面代码片段的结果。

现在我想确定 second sample 中的数据是在第一个样本使用的曲线上方还是下方。 yssecond sample 都使用 xs 作为一种索引(范围从 0 到 11,步长为 0.5)。

second_ys = np.array([17623610, 32724312,   918384,   749818,   910372])
second_xs = np.array([0, 0, 0.5, 0, 11.5])

我无法在论坛中输入 second_ys[0:1],因为答案是错误的术语:

x1 = 17623610 #second_ys[0:1]
39960941.909692936 * np.exp(-0.4000309715410416 * x1) + 421846.9906738758
>>> 421846.9906738758

我不确定如何确定沿 second_ys 的值是高于还是低于线。

当我将参数重新插入方程式以查看值是高于还是低于视觉表示的曲线时,它 returns 所有相同的值:

monoExp(ys, params[0], params[1], params[2])
>>> array([421846.99067388, 421846.99067388, 421846.99067388, 421846.99067388,
       421846.99067388, 421846.99067388, 421846.99067388, 421846.99067388,
       421846.99067388, 421846.99067388, 421846.99067388, 421846.99067388,
       421846.99067388, 421846.99067388, 421846.99067388, 421846.99067388,
       421846.99067388, 421846.99067388, 421846.99067388, 421846.99067388,
       421846.99067388, 421846.99067388, 421846.99067388])

因为你有拟合曲线,你可以替换使用 xs 来确定以 ys

结尾的误差向量的来源

根据你的绘图代码,曲线是monoExp(xs, m, t, b),所以你可以得到如下错误。

err = ys - monoExp(xs, m, t, b)
plt.stem(xs, err)