【问题标题】:Smoothing Data in Python在 Python 中平滑数据
【发布时间】:2015-11-09 15:12:55
【问题描述】:

我有这些数据:

8.7530482   1.34E-01
8.7584016   2.68E-01
8.7637563   6.70E-01
8.769111    1.47E+00
8.7744644   2.15E+00
8.7798191   3.08E+00
...
11.5693578  6.36E+01
11.5747125  6.21E+01
11.5800659  6.17E+01
11.5854193  6.14E+01
11.590774   6.14E+01
11.5961287  6.15E+01
11.6014821  6.45E+01

问题是我需要数据的域看起来像这样:

8.75
8.76
8.77
8.78
8.79
...
11.57
11.58
11.59
11.60
11.61

同时保持范围值与原始数据一致。所以我需要对数据进行智能平均,得到一个有规律的区间域。

我正在尝试使用 python 来做到这一点。

任何帮助将不胜感激。

【问题讨论】:

  • 你的数据是什么格式的?是熊猫DataFrame吗?
  • 只是一个 .txt 文件,制表符分隔

标签: python dataset smoothing moving-average


【解决方案1】:

听起来您想对数据进行线性插值。为此,请使用 scipy 的 interp1d

这是一个演示:

import numpy as np
from scipy.interpolate import interp1d

data = """
11.5693578  6.36E+01
11.5747125  6.21E+01
11.5800659  6.17E+01
11.5854193  6.14E+01
11.590774   6.14E+01
11.5961287  6.15E+01
11.6014821  6.45E+01
"""
data = np.fromstring(data, sep=' ')
data = data.reshape((-1, 2))

x, y = data.T
f = interp1d(x, y)

现在我们可以使用f 来获取正则域上的插值:

regular_x = np.arange(11.57, np.max(x), .01)

plt.plot(x, y, marker='o')
plt.plot(regular_x, f(regular_x), marker='o')

plt.legend(['Original', 'Interpolated'], loc='best')
plt.ylim([61, 65])

【讨论】:

    猜你喜欢
    • 2015-05-05
    • 2015-08-07
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2017-11-28
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多