【问题标题】:Interpolation of missing temperature data in PythonPython中缺失温度数据的插值
【发布时间】:2020-08-31 11:48:37
【问题描述】:

我有东西伯利亚几个站点的月度温度数据。但是,我的工作需要的一个站丢失了很多数据,而附近的其他站覆盖率很好。有没有办法根据另一个数据集的行为来插入缺失的数据?无法提供任何代码,因为我不知道从哪里开始,而且数据集看起来像这样:

红点是来自缺失值的站点数据,而绿色图表来自具有良好覆盖率的站点

如果有人能指出正确的方向,我将不胜感激

【问题讨论】:

  • 我认为您的意思是插值或外推而不是插值。你能发布一个数据集的样本吗?此外,其他电台与感兴趣的电台有何关联?你有这个站过去的数据我们可以借鉴吗?
  • 我投票结束这个问题,因为它是数学问题,而不是编程问题。

标签: python interpolation


【解决方案1】:

有一些方法可以做到这一点,例如,对覆盖率高的数据集应用 FFT,看看它与覆盖率低的数据集的匹配度如何,同时去除高频项。

但是,我非常怀疑这是否有用:覆盖率高的数据集几乎完全适合覆盖率低的数据集。无论您要应用什么方法,与覆盖率高的数据集相似而覆盖率低的数据集拟合的最佳函数就是覆盖率高的数据集本身。

【讨论】:

    【解决方案2】:

    让我们创建一个试验数据集来解决您的问题:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    t = np.linspace(0, 30*2*np.pi, 30*24*2)
    td = pd.date_range("2020-01-01", freq='30T', periods=t.size)
    
    T0 = np.sin(t)*8 - 15 + np.random.randn(t.size)*0.2
    T1 = np.sin(t)*7 - 13 + np.random.randn(t.size)*0.1
    T2 = np.sin(t)*9 - 10 + np.random.randn(t.size)*0.3
    T3 = np.sin(t)*8.5 - 11 + np.random.randn(t.size)*0.5
    T = np.vstack([T0, T1, T2, T3]).T
    
    features = pd.DataFrame(T, columns=["s1", "s2", "s3", "s4"], index=td)
    

    看起来像:

    axe = features[:"2020-01-04"].plot()
    axe.legend()
    axe.grid()
    

    然后,如果您的时间序列线性相关良好,您可以简单地通过普通最小二乘回归的平均值预测缺失值。 SciKit-Learn 提供了一个方便的接口来执行这种计算:

    from sklearn import linear_model
    from sklearn.model_selection import train_test_split
    
    # Remove target site from features:
    target = features.pop("s4")
    
    # Split dataset into train (actual data) and test (missing temperatures):
    x_train, x_test, y_train, y_test = train_test_split(features, target, train_size=0.25, random_state=123)
    
    # Create a Linear Regressor and train it:
    reg = linear_model.LinearRegression()
    reg.fit(x_train, y_train)
    
    # Assess regression score with test data:
    reg.score(x_test, y_test) # 0.9926150729585087
    
    # Predict missing values:
    ypred = reg.predict(x_test)
    ypred = pd.DataFrame(ypred, index=x_test.index, columns=["s4p"])
    

    结果如下:

    axe = features[:"2020-01-04"].plot()
    target[:"2020-01-04"].plot(ax=axe)
    ypred[:"2020-01-04"].plot(ax=axe, linestyle='None', marker='.')
    axe.legend()
    axe.grid()
    

    error = (y_test - ypred.squeeze())
    axe = error.plot()
    axe.legend(["Prediction Error"])
    axe.grid()
    

    【讨论】:

      猜你喜欢
      • 2020-09-07
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多