gpt4 book ai didi

python - 时间序列数据python的Lowess平滑

转载 作者:行者123 更新时间:2023-12-03 14:38:59 25 4
gpt4 key购买 nike

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

Time series data

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

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 an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units



我的数据中的日期格式为 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]')

关于python - 时间序列数据python的Lowess平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com