Jam*_*es 6 python time-series smoothing
我正在尝试使用 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()
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,出现以下错误:
ValueError: view limit minimum -7.641460199922635e+16 小于 1 并且是无效的 Matplotlib 日期值。如果您将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况
我的数据中的日期格式为 07/05/2018 00:07:00。我认为问题在于 LOWESS 正在努力处理日期时间数据,但不确定?
你能帮我么?
Lowess 不尊重 DateTimeIndex 类型,而是仅将日期作为自纪元以来的纳秒返回。幸运的是,很容易转换回来:
smoothedx, smoothedy = lowess(y1, x, is_sorted=True, frac=0.025, it=0)
smoothedx = smoothedx.astype('datetime64[s]')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3865 次 |
| 最近记录: |