【问题标题】:Sklearn or Pandas, impute missing values with simple linear regressionSklearn 或 Pandas,用简单的线性回归估算缺失值
【发布时间】:2019-03-26 19:05:04
【问题描述】:

我有数据、时间序列数据,我想估算缺失的数据。我不能使用列的平均值,因为我认为它不适合时间序列数据。 所以我想要简单的线性回归来估算它

Day, Price
 1 , NaN
 2, NaN
 3, 1800
 4, 1900
 5, NaN
 6, NaN
 7, 2000
 8, 2200

如何做到这一点?

我更喜欢使用 Pandas 来做这件事, 但如果没有其他方法,我可以使用 sklearn 来做到这一点:)

【问题讨论】:

标签: python pandas scikit-learn data-mining


【解决方案1】:

您可以使用interpolate

df['Price'].interpolate(method='linear', inplace=True)

结果:

    Price   Date
0   NaN     1
1   NaN     2
2   1800.000000     3
3   1900.000000     4
4   1933.333333     5
5   1966.666667     6
6   2000.000000     7
7   2200.000000     8

如您所见,这只会向前填充缺失值。如果还想填充前两个值,请使用参数limit_direction="both"

df['Price'].interpolate(method='linear', inplace=True, limit_direction="both")

有不同的插值方法,例如二次或样条,有关更多信息,请参阅文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.interpolate.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2022-11-11
    • 2020-08-28
    • 2019-08-31
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多