简体   繁体   English

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

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

I generated an exponential curve fit for the proxy data below.我为下面的代理数据生成了一个指数曲线拟合。 After fitting the model to the data, I need to determine if another sample second sample , row by row, is above or below the fit line.在将 model 拟合到数据后,我需要逐行确定另一个样本second sample是否高于或低于拟合线。 Where I am struggling is how to determine if the second sample data is above or below the fit line.我苦苦挣扎的地方是如何确定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")

Here are the results from the code snippet above.这是上面代码片段的结果。

在此处输入图像描述

Now I want to determine whether or not the data from the second sample fits above or below the curve used for the first sample.现在我想确定second sample的数据是否适合第一个样本使用的曲线上方或下方。 Both ys and the second sample use xs as a sort of index (range from 0 to 11 with 0.5 steps). 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])

I cannot enter second_ys[0:1] into the forumla, because the answer is the error term:我无法在论坛中输入second_ys[0:1] ,因为答案是错误术语:

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

I am not sure how to determine if values along second_ys fall above or below the line.我不确定如何确定沿second_ys的值是高于还是低于该线。

Is there a way to generate data of where the curve lies on for xs below?有没有办法为下面的xs生成曲线所在位置的数据?

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

since you have the fitted curve you can replace use the xs to determine the origin of the error vector ending in ys因为你有拟合曲线,你可以替换使用xs来确定以 ys 结尾的误差向量的原点

The curve is monoExp(xs, m, t, b) accordingly to your plotting code, so you can get the error as follows.根据您的绘图代码,曲线是monoExp(xs, m, t, b) ,因此您可以得到如下错误。

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM