python - 平滑数据并找到最大值

标签 python r statistics max smoothing

我有一个包含两个变量 x 和 y 的数据集(见下文)。我想找出 x 的哪个值在 y 中出现最大值。我目前的方法是简单地查找 x ,从而得到最大 y 。这并不理想,因为我的数据非常嘈杂,所以我想首先执行某种平滑,然后找到最大值。

到目前为止,我已尝试使用 R 通过 np 包中的 npreg(内核回归)来平滑数据,以获得此曲线:

enter image description here

但我不知道如何找到最大值。

我想要用 Python 解决以下问题:

1)平滑数据(不是核回归)

2) 使用平滑数据找到 y 中最大值出现处的 x 值

x   y
-20 0.006561733
-19 -4.48E-08
-18 -4.48E-08
-17 -4.48E-08
-16 0.003281305
-15 0.00164063
-14 0.003280565
-13 0.003282537
-12 -4.48E-08
-11 0.003281286
-10 0.004921239
-9  0.00491897
-8  -1.52E-06
-7  0.004925867
-6  -1.27E-06
-5  0.009839438
-4  0.001643726
-3  -4.48E-08
-2  2.09E-06
-1  -0.001640027
0   0.006559627
1   0.001636958
2   2.36E-06
3   0.003281469
4   0.011481469
5   0.004922279
6   0.018044207
7   0.011483134
8   0.014765087
9   0.008201379
10  0.00492497
11  0.006560482
12  0.009844796
13  0.011483199
14  0.008202129
15  0.001641621
16  0.004921645
17  0.006563377
18  0.006561068
19  0.008201004

最佳答案

我会对数据运行高斯滤波器以平滑:

# first, make a function to linearly interpolate the data
f = scipy.interpolate.interp1d(x,y)

# resample with 1000 samples
xx = np.linspace(-20,19, 1000)

# compute the function on this finer interval
yy = f(xx)

# make a gaussian window
window = scipy.signal.gaussian(200, 60)

# convolve the arrays
smoothed = scipy.signal.convolve(yy, window/window.sum(), mode='same')

# get the maximum
xx[np.argmax(smoothed)]

这是平滑后的结果:

The smoothed result.

最大值出现在 6.93。

scipy.signal 中还有大量其他窗口函数和过滤选项。请参阅documentation了解更多。

关于python - 平滑数据并找到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291567/

相关文章:

r - 创建连续值的组名

statistics - 在应用分类算法之前预处理分类数据的方法有哪些?

python - 使用 python 中的对数轴缩放和拟合对数正态分布

python - Django Rest Framework 不序列化 SerializerMethodField

r - 维恩图 : How to hide % overlap labels?

python - 模拟 requests.post 和 requests.json 解码器 python

r - 如何将标量值乘以 R data.frame 中的多个列?

python - 为什么 R 和 Python Mann Whitney 不同?

python - class_weights 如何应用于 sklearn 逻辑回归?

python - 曲线拟合数据问题