import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose import datetime

将Timestamp列转换为时间序列索引

遍历数据集中的每一行

for index, row in df.iterrows(): # 获取当前行的时间戳 time_str = row[' Timestamp'] # 将时间戳转换为时间格式 time_obj = datetime.datetime.strptime(time_str, '%M:%S.%f') # 将时间格式转化为秒数,并替换时间戳那一列的数据 seconds = (time_obj - time_obj.replace(minute=0, second=0, microsecond=0)).total_seconds() df.loc[index, ' Timestamp'] = seconds

将时间戳列转换为DatetimeIndex类型

df[' Timestamp'] = df[' Timestamp'].astype(int) df[' Timestamp'] = pd.to_datetime(df[' Timestamp'], unit='s') df[' Timestamp'] = df[' Timestamp'].apply(lambda x: pd.Timestamp.fromtimestamp(x.timestamp())) df.set_index(' Timestamp', inplace=True)

按每分钟对数据进行重采样

resampled_df = df.resample('1min').mean()

进行时间序列分解

decomposition = seasonal_decompose(resampled_df['Flow Bytes/s'], model='additive')

绘制分解后的趋势、季节性和残差图

plt.figure(figsize=(12, 8)) plt.subplot(411) plt.plot(resampled_df['Flow Bytes/s'], label='Original') plt.legend(loc='best') plt.subplot(412) plt.plot(decomposition.trend, label='Trend') plt.legend(loc='best') plt.subplot(413) plt.plot(decomposition.seasonal, label='Seasonality') plt.legend(loc='best') plt.subplot(414) plt.plot(decomposition.resid, label='Residuals') plt.legend(loc='best') plt.tight_layout() plt.show()

Python时间序列分解:使用statsmodels和matplotlib可视化趋势、季节性和残差

原文地址: https://www.cveoy.top/t/topic/nztH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录