gpt4 book ai didi

python - 来自平滑数据的样本

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:38 26 4
gpt4 key购买 nike

我有一个非常嘈杂的功能,我可以使用 How to smooth a curve in the right way? 平滑(根据此处给出的答案 scipy.signal.savgol_filter ) .原始数据和平滑数据如下所示(分别为蓝色和红色):

enter image description here

问题是我需要从平滑数据中采样,即:我需要在任意 x 值处评估红色曲线。 savgol_filter 函数只返回一个值数组,而不是一个我可以计算的函数。

执行此操作的最快方法是什么(它将被采样数百万次)?


MVCE

import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt

# Noisy data
y = np.array([-5715.75, -5592.3 , -5548.33, -5638.97, -5586.43, -5703.21,
-5660.6 , -5714.96, -5637.59, -5599.72, -5631.14, -5684.31,
-5586.08, -5617.43, -5629.58, -5530.08, -5540.53, -5475.53,
-5505.21, -5500.96, -5500.58, -5474.65, -5462.45, -5443.82,
-5441.77, -5463.53, -5512.18, -5395.85, -5389.87, -5432.94,
-5366.31, -5284.45, -5176.52, -5221.89, -5182.52, -5084.92,
-5084.3 , -4972.78, -4968.32, -4818.19, -4789.56, -4872.02,
-4809.45, -4855.06, -4806.77, -4717.93, -4741.29, -4822.45,
-4760.51, -4698.31, -4744.1 , -4797.08, -4777.43, -4785.02,
-4687.61, -4820.73, -4753.5 , -4777.99, -4812.5 , -4856.53,
-4859.69, -4905.37, -4838.71, -5058.49, -5053.58, -5057. ,
-5159.58, -5155.03, -5079.21, -5228.57, -5257.26, -5409.64,
-5505.87, -5511.82, -5471.4 , -5478.47, -5530.9 , -5578.88,
-5705.87, -5633.66, -5740.72, -5760.05, -5801.39, -5808.52,
-5803.22, -5832.76, -5867.51, -5837.56, -5923.97, -5933.75,
-5945.04, -5932.16, -5909.68, -5951.29, -5958.6 , -5958.07,
-5970.75, -5931.93, -5947.53, -5956.36])
x = np.linspace(0., 6, 100)

# Smoothed data
yhat = savgol_filter(y, 51, 3)

plt.plot(x, y)
plt.plot(x, yhat, color='r')
plt.show()

最佳答案

使用 scipy 的 interp1d功能:

x and y are arrays of values used to approximate some function f: y = f(x). This class returns a function whose call method uses interpolation to find the value of new points.

from scipy.interpolate import interp1d

y_ = interp1d(x, yhat)

new_x_vals = np.array([0.0001, 1.011, 2.022, 3.033, 4.044])
>>> y_(new_x_vals)
array([-5590.20368685, -5576.9338028 , -5140.41553793, -4749.82520031,
-5153.81189525])

关于python - 来自平滑数据的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com