【问题标题】:Python smoothing dataPython平滑数据
【发布时间】:2015-05-05 12:40:41
【问题描述】:

我有一个想要平滑的数据集。我有两个不均匀分布的变量 y 和 x。 y 是因变量。但是,我不知道什么公式将 x 与 y 联系起来。

我阅读了有关插值的所有内容,但插值要求我知道将 x 与 y 相关联的公式。我还研究了其他平滑函数,但这些会导致起点和终点出现问题。

有谁知道如何: - 获得将 x 与 y 相关联的公式 - 平滑数据点而不弄乱端点

我的数据如下:

import matplotlib.pyplot as plt

x = [0.0, 2.4343476531707129, 3.606959459205791, 3.9619355597454664, 4.3503348239356558, 4.6651002761894667, 4.9360228447915109, 5.1839565805565826, 5.5418099660513596, 5.7321342976055165,5.9841050994671106, 6.0478709402949216, 6.3525180590674513, 6.5181245134579893, 6.6627517592933767, 6.9217136972938444,7.103121623408132, 7.2477706136047413, 7.4502723880766748, 7.6174503055171137, 7.7451599936721376, 7.9813193157205191, 8.115292520850506,8.3312689109403202, 8.5648187916197998, 8.6728478860287623, 8.9629327234023926, 8.9974662723308612, 9.1532523634107257, 9.369326186780814, 9.5143785756455479, 9.5732694726297893, 9.8274813411538613, 10.088572892445802, 10.097305715988142, 10.229215999264703, 10.408589988296546, 10.525354763219688, 10.574678982757082, 10.885039893236041, 11.076574204171795, 11.091570626351352, 11.223859812944436, 11.391634940142225, 11.747328449715521, 11.799186895037078, 11.947711314893802, 12.240901223703657, 12.50151825769724, 12.811712563174883, 13.153496854155087, 13.978408296586579, 17.0, 25.0]
y = [0.0, 4.0, 6.0, 18.0, 30.0, 42.0, 54.0, 66.0, 78.0, 90.0, 102.0, 114.0, 126.0, 138.0, 150.0, 162.0, 174.0, 186.0, 198.0, 210.0, 222.0, 234.0, 246.0, 258.0, 270.0, 282.0, 294.0, 306.0, 318.0, 330.0, 342.0, 354.0, 366.0, 378.0, 390.0, 402.0, 414.0, 426.0, 438.0, 450.0, 462.0, 474.0, 486.0, 498.0, 510.0, 522.0, 534.0, 546.0, 558.0, 570.0, 582.0, 594.0, 600.0, 600.0]

#Smoothing here

fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(x, y, color='red', label= 'Unsmoothed curve')

【问题讨论】:

    标签: python smoothing


    【解决方案1】:

    我认为平滑(即过滤)、插值和曲线拟合之间存在混淆,

    • 过滤/平滑:我们在数据上应用一个算子,以消除高频振荡的方式修改原始y 点。这可以通过例如scipy.signal.convolvescipy.signal.medfiltscipy.signal.savgol_filter 或基于 FFT 的方法来实现。

    • 插值:我们从可用数据点创建数据的连续本地表示。插值定义函数在数据点之间的行为方式,但不修改数据点本身。参见例如scipy.interpolate.interp1d。不过,为了让事情变得更复杂,spline interpolation 实际上也做了一些平滑处理。

    • 曲线拟合:我们通过一些分析函数拟合数据点。这允许在我们的数据中确定xy 之间的全局关系,但需要对合适的拟合函数有一些先前的了解。见scipy.optimize.curve_fit

    在这种特殊情况下,我们可以使用的方法是首先在统一网格上进行插值(如@agomcas 的答案),然后应用 Savitzky-Golay 过滤器来平滑数据。或者,可以将数据拟合到一些分析表达式,例如基于 tanh 函数,但这需要进一步调整:

    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    from scipy.interpolate import interp1d
    from scipy.signal import savgol_filter
    import numpy as np
    
    x = np.array([0.0, 2.4343476531707129, 3.606959459205791, 3.9619355597454664, 4.3503348239356558, 4.6651002761894667, 4.9360228447915109, 5.1839565805565826, 5.5418099660513596, 5.7321342976055165,5.9841050994671106, 6.0478709402949216, 6.3525180590674513, 6.5181245134579893, 6.6627517592933767, 6.9217136972938444,7.103121623408132, 7.2477706136047413, 7.4502723880766748, 7.6174503055171137, 7.7451599936721376, 7.9813193157205191, 8.115292520850506,8.3312689109403202, 8.5648187916197998, 8.6728478860287623, 8.9629327234023926, 8.9974662723308612, 9.1532523634107257, 9.369326186780814, 9.5143785756455479, 9.5732694726297893, 9.8274813411538613, 10.088572892445802, 10.097305715988142, 10.229215999264703, 10.408589988296546, 10.525354763219688, 10.574678982757082, 10.885039893236041, 11.076574204171795, 11.091570626351352, 11.223859812944436, 11.391634940142225, 11.747328449715521, 11.799186895037078, 11.947711314893802, 12.240901223703657, 12.50151825769724, 12.811712563174883, 13.153496854155087, 13.978408296586579, 17.0, 25.0])
    y = np.array([0.0, 4.0, 6.0, 18.0, 30.0, 42.0, 54.0, 66.0, 78.0, 90.0, 102.0, 114.0, 126.0, 138.0, 150.0, 162.0, 174.0, 186.0, 198.0, 210.0, 222.0, 234.0, 246.0, 258.0, 270.0, 282.0, 294.0, 306.0, 318.0, 330.0, 342.0, 354.0, 366.0, 378.0, 390.0, 402.0, 414.0, 426.0, 438.0, 450.0, 462.0, 474.0, 486.0, 498.0, 510.0, 522.0, 534.0, 546.0, 558.0, 570.0, 582.0, 594.0, 600.0, 600.0])
    
    
    xx = np.linspace(x.min(),x.max(), 1000)
    
    # interpolate + smooth
    itp = interp1d(x,y, kind='linear')
    window_size, poly_order = 101, 3
    yy_sg = savgol_filter(itp(xx), window_size, poly_order)
    
    
    # or fit to a global function
    def func(x, A, B, x0, sigma):
        return A+B*np.tanh((x-x0)/sigma)
    
    fit, _ = curve_fit(func, x, y)
    yy_fit = func(xx, *fit)
    
    fig, ax = plt.subplots(figsize=(7, 4))
    ax.plot(x, y, 'r.', label= 'Unsmoothed curve')
    ax.plot(xx, yy_fit, 'b--', label=r"$f(x) = A + B \tanh\left(\frac{x-x_0}{\sigma}\right)$")
    ax.plot(xx, yy_sg, 'k', label= "Smoothed curve")
    plt.legend(loc='best')
    

    【讨论】:

    • 您好,感谢您的回复,但我不太明白您最后一个适合的公式是什么意思。我应该将此公式用作 scipy.optimize.curve.fit 函数的参数吗?
    • 对,我把上面的答案编辑得更明确一点,还修正了一些错误的陈述。
    • 哇,他们的 savgol 过滤器看起来很棒。这肯定解决了我的平滑问题。但是,我仍然想知道是否有办法找到描述图表的数学公式。
    【解决方案2】:

    插值要求您知道有关 x 和 y 的公式。

    import matplotlib.pyplot as plt
    from scipy import interpolate
    import numpy as np
    
    x = [0.0, 2.4343476531707129, 3.606959459205791, 3.9619355597454664, 4.3503348239356558, 4.6651002761894667, 4.9360228447915109, 5.1839565805565826, 5.5418099660513596, 5.7321342976055165,5.9841050994671106, 6.0478709402949216, 6.3525180590674513, 6.5181245134579893, 6.6627517592933767, 6.9217136972938444,7.103121623408132, 7.2477706136047413, 7.4502723880766748, 7.6174503055171137, 7.7451599936721376, 7.9813193157205191, 8.115292520850506,8.3312689109403202, 8.5648187916197998, 8.6728478860287623, 8.9629327234023926, 8.9974662723308612, 9.1532523634107257, 9.369326186780814, 9.5143785756455479, 9.5732694726297893, 9.8274813411538613, 10.088572892445802, 10.097305715988142, 10.229215999264703, 10.408589988296546, 10.525354763219688, 10.574678982757082, 10.885039893236041, 11.076574204171795, 11.091570626351352, 11.223859812944436, 11.391634940142225, 11.747328449715521, 11.799186895037078, 11.947711314893802, 12.240901223703657, 12.50151825769724, 12.811712563174883, 13.153496854155087, 13.978408296586579, 17.0, 25.0]
    y = [0.0, 4.0, 6.0, 18.0, 30.0, 42.0, 54.0, 66.0, 78.0, 90.0, 102.0, 114.0, 126.0, 138.0, 150.0, 162.0, 174.0, 186.0, 198.0, 210.0, 222.0, 234.0, 246.0, 258.0, 270.0, 282.0, 294.0, 306.0, 318.0, 330.0, 342.0, 354.0, 366.0, 378.0, 390.0, 402.0, 414.0, 426.0, 438.0, 450.0, 462.0, 474.0, 486.0, 498.0, 510.0, 522.0, 534.0, 546.0, 558.0, 570.0, 582.0, 594.0, 600.0, 600.0]
    
    
    f = interpolate.interp1d(x, y, kind="linear")
    x_int = np.linspace(x[0],x[-1], 20)
    y_int = f(x_int)
    
    #Smoothing here
    
    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot(x, y, color='red', label= 'Unsmoothed curve')
    ax.plot(x_int, y_int, color="blue", label= "Interpolated curve")
    

    【讨论】:

    • 忘了说,曲线在技术上还没有平滑。但是,因为我对插值数据使用了较少的点,所以它看起来已经不那么嘈杂了。
    猜你喜欢
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多