【时间序列预测_python_jupyter】使用neuralforecast包在jupyter-lab上预测并绘图

neuralforecast包有很多引入好的时间序列预测算法模型,可以直接通过接口调用。

支持的算法模型有:

__all__ = ['RNN', 'GRU', 'LSTM', 'TCN', 'DeepAR', 'DilatedRNN',
           'MLP', 'NHITS', 'NBEATS', 'NBEATSx', 'DLinear', 'NLinear',
           'TFT', 'VanillaTransformer', 'Informer', 'Autoformer', 'PatchTST', 'FEDformer',
           'StemGNN', 'HINT', 'TimesNet', 'TimeLLM', 'TSMixer', 'TSMixerx', 'MLPMultivariate',
           'iTransformer', 'BiTCN', 'TiDE', 'DeepNPTS', 'SOFTS', 'TimeMixer', 'KAN'
           ]

预测效果:

在这里插入图片描述

实现代码:

#%%

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

from neuralforecast import NeuralForecast
from neuralforecast.models import NBEATS, NHITS, DLinear, TFT, NLinear, TimesNet, TimeMixer, TSMixerx, iTransformer, DeepNPTS, PatchTST
from neuralforecast.utils import AirPassengersDF

from neuralprophet import NeuralProphet
%matplotlib widget
pd.set_option('display.max_columns', None) # 显示完整的列
pd.set_option('display.max_rows', None) # 显示完整的行

#%%

#处理excel数据,如果时间有缺失的话,插入一条数据,y值取前后两个点的插值
df=pd.read_csv('test1.csv')

df['ds']=pd.to_datetime(df['ds'])
'''df.set_index('ds', inplace=True)  # 设置时间戳为索引
resampled_df = df['2024-5-15':'2024-6-5'].resample('1T').interpolate(method='linear').ffill()
#把date移回列
resampled_df.reset_index(drop=False, inplace=True) '''

#%%

#add N-BEATs
#freq,M月,D天,H小时,T分钟,S秒
nf = NeuralForecast(
    models = [PatchTST(input_size=24, h=240, max_steps=100)],
    freq = 'T',
)

nf.fit(df=df)
result_df=nf.predict()
print(result_df)

#绘图
temp_df=result_df['PatchTST']

_,ax=plt.subplots(figsize=(12,6))
ax.scatter(x=df['ds'],y=df['y'],s=1,c='b')
ax.scatter(x=result_df['ds'], y=temp_df,s=5,c='r')
#把最低和最高绘制成两条曲线,作为fillbetween的输入
ax.fill_between(result_df['ds'],temp_df.min(),temp_df.max(),alpha=0.5,linewidth=0,color='r')

#%%



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值