考虑以下示例:
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy import interpolate
xs = np.linspace(1,10,500)
ys = [0.92 * x ** 2.3 + 0.0132 * x ** 4 + 0.0743 * (x - 9) ** 3 - 4 * (x -3) ** 2 + 80 * math.sin(math.sin(x)) + 10 * math.sin(x*5) + 1.2* np.random.normal(-4,4,1) for x in xs]
ys[200] = ys[200] + 130
ys[201] = ys[201] + 135
ys[202] = ys[202] + 129
ys[203] = ys[203] + 128
ys[204] = ys[204] + 131
ys[205] = ys[205] + 130
ys[206] = ys[206] + 129
ys[207] = ys[207] + 129
ys[208] = ys[208] + 128
ys[209] = ys[209] + 130
现在我使用scipy.interpolate.splrep
将样条曲线拟合到此数据。我使用了两个不同的样条线来拟合数据的两个不同部分:
tck = interpolate.splrep(xs[0:199], ys[0:199], s = 1000)
ynew2 = interpolate.splev(xs[0:199], tck, der = 0)
和:
tck = interpolate.splrep(xs[210:500], ys[210:500], s = 9000)
ynew3 = interpolate.splev(xs[210:500], tck, der = 0)
现在我想以编程方式检测拟合的质量。拟合既不应该太直 - 即保留特征,也不应该“过度检测”作为特征的嘈杂变化。
我计划使用馈送到ANN的峰值计数器。
但是,此时,我的问题是:
splrep
的输出中输入它,它会计算最小值或最大值以及任何特定时间间隔的最大值/最小值的密度? 注意:
我知道R**2
值,我正在寻找另一种检测功能保存的措施。
答案 0 :(得分:1)
SciPy没有找到三次样条曲线临界点的方法。我们最接近sproot找到三次样条的根。为了使这在这里有用,我们必须拟合阶数为4的样条,以便导数是一个三次样条。这就是我在下面做的事情
from scipy.interpolate import splrep, splev, splder, sproot
tck1 = splrep(xs[0:199], ys[0:199], k=4, s=1000)
tck2 = splrep(xs[210:500], ys[210:500], k=4, s=9000)
roots1 = sproot(splder(tck1), 1000) # 1000 is an upper bound for the number of roots
roots2 = sproot(splder(tck2), 1000)
x1 = np.linspace(xs[0], xs[198], 1000) # plot both splines
plt.plot(x1, splev(x1, tck1))
x2 = np.linspace(xs[210], xs[499], 1000)
plt.plot(x2, splev(x2, tck2))
plt.plot(roots1, splev(roots1, tck1), 'ro') # plot their max/min points
plt.plot(roots2, splev(roots2, tck2), 'ro')
plt.show()
差异很明显。
您还可以在任何特定时间间隔内找到根数,例如[3,4]:
np.where((3 <= roots1) & (roots1 <= 4))[0].size # 29
或等效地,np.sum((3 <= roots1) & (roots1 <= 4))