【问题标题】:Smooth Data and Find Maximum平滑数据并找到最大值
【发布时间】:2014-03-10 03:26:04
【问题描述】:

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

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

但我不确定如何找到最大值。

我想用 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

【问题讨论】:

    标签: python r statistics max smoothing


    【解决方案1】:

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

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

    这是平滑的结果:

    最大值出现在 6.93。

    scipy.signal 中还有很多其他窗口功能和过滤选项。请参阅documentation 了解更多信息。

    【讨论】:

    【解决方案2】:

    您也许可以使用平滑样条函数:

    import numpy as np
    from scipy import interpolate
    x = range(-20,20)
    y = [0.006561733, -4.48e-08, -4.48e-08, -4.48e-08, 0.003281305, 0.00164063, 0.003280565, 0.003282537, -4.48e-08, 0.003281286, 0.004921239, 0.00491897, -1.52e-06, 0.004925867, -1.27e-06, 0.009839438, 0.001643726, -4.48e-08, 2.09e-06, -0.001640027, 0.006559627, 0.001636958, 2.36e-06, 0.003281469, 0.011481469, 0.004922279, 0.018044207, 0.011483134, 0.014765087, 0.008201379, 0.00492497, 0.006560482, 0.009844796, 0.011483199, 0.008202129, 0.001641621, 0.004921645, 0.006563377, 0.006561068, 0.008201004]
    
    tck = interpolate.splrep(x,y) # pass in s= some value to change smoothing: 
                                  # higher = smoother, s=0 for no smoothing
    
    xnew = np.arange(-20, 20, 0.1)
    ynew = interpolate.splev(xnew,tck,der=0)
    

    现在xnewynew 包含拟合的精细采样版本,您可以通过

    max_index = np.argmax(ynew)
    max_value = ynew[max_index]
    max_x = xnew[max_index]
    

    抱歉,我无法对此进行测试;我现在使用的计算机没有加载 scipy 等...不过应该给你一些想法。

    参考:http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

    【讨论】:

      【解决方案3】:

      我不完全确定要解决的主要问题是什么?更好的平滑,找到最小值还是全部在 Python 中完成?如果您在 R 方面取得了可喜的进展,为什么要改用 Python?我发现在 R 中内置的 supsmu 函数通常可以很好地进行非参数平滑。这就是我在 R 中的做法。

      smooth <- do.call(supsmu, data)
      min.idx <- which.min(smooth$y)
      min.point <- c(smooth$x[min.idx], smooth$y[min.idx])
      

      【讨论】:

        猜你喜欢
        • 2019-10-13
        • 2020-07-25
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 2013-03-02
        • 2022-06-14
        • 2017-07-24
        相关资源
        最近更新 更多