【问题标题】:Lowess Smoothing of Time Series data python时间序列数据python的Lowess平滑
【发布时间】:2019-05-12 09:32:03
【问题描述】:

我正在尝试使用 LOWESS 来平滑以下数据:

我想获得一条可以过滤掉数据中尖峰的平滑线。我的代码如下:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import HourLocator, DayLocator, DateFormatter
from statsmodels.nonparametric.smoothers_lowess import lowess

file = r'C:...'
df = pd.read_csv(file) # reads data file   

df['Date'] = pd.to_datetime(df['Time Local'], format='%d/%m/%Y  %H:%M')     

x = df['Date']  
y1 = df['CTk2 Level'] 

filtered = lowess(y1, x, is_sorted=True, frac=0.025, it=0)

plt.plot(x, y1, 'r')
plt.plot(filtered[:,0], filtered[:,1], 'b')

plt.show()

当我运行此代码时,我收到以下错误:

ValueError: view limit minimum -7.641460199922635e+16 is less than 1 and is a invalid Matplotlib date value.如果您将非日期时间值传递给具有日期时间单位的轴,这通常会发生

我数据中的日期格式为 07/05/2018 00:07:00。我认为问题在于 LOWESS 正在努力处理日期时间数据,但不确定?

你能帮帮我吗?

【问题讨论】:

    标签: python time-series smoothing


    【解决方案1】:

    Lowess 不尊重 DateTimeIndex 类型,而是仅将日期返回为自纪元以来的纳秒。幸运的是,很容易转换回来:

    smoothedx, smoothedy = lowess(y1, x, is_sorted=True, frac=0.025, it=0)
    smoothedx = smoothedx.astype('datetime64[s]')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2020-07-12
      • 2015-08-09
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多