【问题标题】:Fitting Data w/ Curve Fit Then Find Out if Data is above / below curve fit用曲线拟合拟合数据然后找出数据是否高于/低于曲线拟合
【发布时间】:2022-01-06 20:11:41
【问题描述】:

我为下面的代理数据生成了一个指数曲线拟合。在将模型拟合到数据后,我需要逐行确定另一个样本 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")

这是上面代码 sn-p 的结果。

现在我想确定来自second sample 的数据是否适合第一个样本所用曲线的上方或下方。 ys 和 second 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 的值是高于还是低于该线。

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

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])

【问题讨论】:

  • 插入带有拟合参数的 x 值应该给出拟合线。然后你可以只(ys-fitted)并且结果数组> 0然后在线上,结果数组
  • @NathanFurnal 你能演示一下吗?

标签: python numpy scipy curve-fitting exponential


【解决方案1】:

因为你有拟合曲线,你可以替换使用xs来确定以ys结尾的误差向量的原点

根据您的绘图代码,曲线为monoExp(xs, m, t, b),因此您可以得到如下错误。

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

【讨论】:

  • 嗨@Bob,这不起作用,因为second_ys 没有与xs 相同的大小索引
  • 你没有secodn_xs吗?如果你有你使用second_ys - monoExp(second_xs, m, t,b)。如果未定义 xs,则会给出错误 max(b - second_ys, 0),因为对于任何 y > b(并读取 t),您都可以找到一个 x,它将给出 monoExp(x, m, t, b),给出错误零。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 2010-10-09
  • 2021-10-14
  • 2015-09-03
  • 2021-01-19
  • 2019-03-15
相关资源
最近更新 更多