gpt4 book ai didi

python - 滚动平均数据绘图未正确平滑

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:58 25 4
gpt4 key购买 nike

当尝试使用以下代码在 pandas 中绘制滚动平均值以平滑我的数据时,我得到一个奇怪的图形

data['mean_Kincaid'] = pd.rolling_mean(data.Kincaid,30, min_periods=1)
data['Year']= data['Date'].dt.year
data.plot(x='Date', y='mean_Kincaid')

这会产生以下图表:enter image description here

我希望图表“更平滑”(我的目标是首先使用rolling_mean函数)。

任何帮助将不胜感激:)

更新:带有建议代码的图像 enter image description here

更新 2:使用以下代码,我能够生成以下图像 - 关于如何将 x 轴固定为年份的任何想法?

data['mean_Kincaid'] = data.Kincaid.rolling(75, min_periods=1).mean()
data.plot(x='Date', y='mean_Kincaid')

enter image description here

当我使用以下代码运行它时,出现错误“AttributeError:只能使用具有日期时间值的 .dt 访问器”谢谢!

更新3:

data['mean_Kincaid'] = data.Kincaid.rolling(10000, 
min_periods=1).mean()
data.Date = pd.to_datetime(data.Date)
data.plot(x='Date', y='mean_Kincaid', legend=False, title="Kincaid
scores over time")

enter image description here

最佳答案

这不够平滑。

n = 8001
df = pd.DataFrame(dict(
Kincaid=np.sin(np.linspace(-4, 4, n)) + np.random.rand(n) * 2,
Date=pd.date_range('2010-03-31', periods=n)
))

df['mean_Kincaid'] = df.Kincaid.rolling(30, min_periods=1).mean()

df.plot(x='Date', y=['Kincaid', 'mean_Kincaid'])

enter image description here

这样更好

df['mean_Kincaid'] = df.Kincaid.rolling(360, min_periods=1).mean()

df.plot(x='Date', y=['Kincaid', 'mean_Kincaid'])

enter image description here

注意更大的窗口参数。

关于python - 滚动平均数据绘图未正确平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572926/

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