使用循环神经网络(RNN, LSTM或GRU)实现气象数据预测pytorch


前言

使用循环神经网络(RNN, LSTM或GRU)实现气象数据预测:

数据集:
tq.csv记录了某地每隔1小时的气象数据:
Date Time:时间(日期是 日月年 的格式)
p (mbar):大气压
T (degC):气温
rh (%):湿度

使用循环神经网络(RNN/LSTM/GRU之一)完成下列功能:输入最近n个小时的气象数据,预测之后24小时的气象数据
生成训练集和测试集:
以2014年及以前的数据为训练集
2015年及以后的数据为测试集

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
from torch.utils import data as data

from sklearn.preprocessing import StandardScaler
from config import *

2.读入数据

代码如下(示例):

# 数据读取&dataloader

data_path = cwd + 'data/tq.csv'


train_data = np.array(pd.read_csv(data_path, header=None).iloc[1:52622,1:]).astype('float32')
test_data = np.array(pd.read_csv(data_path, header=None).iloc[52622:,1:]).astype('float32')

scaler = StandardScaler()
train_data = scaler.fit_transform(train_data)
test_data = scaler.transform(test_data)

def split_Xy(data,n,steps=24):
    data_X = []
    data_y = []
    num_Xy = (len(data) - (n + steps)) + 1
    for i in range(num_Xy):
        X = data[i:i+n, :]
        data_X.append(X)
        y = data[i+n:i+n+steps, :]
        data_y.append(y)
    return torch.from_numpy(np.array(data_X)).type(dtype=torch.float32), torch.from_numpy(np.array(data_y)).type(dtype=torch.float32)



train_X,train_y = split_Xy(train_data,24)
test_X,test_y = split_Xy(test_data,24)

train_dataset = data.TensorDataset(train_X,train_y)
test_dataset = data.TensorDataset(test_X,test_y)

trainloader = data.DataLoader(train_dataset,
                              batch_size=128,
                              shuffle=True,drop_last=True, num_workers=0)
testloader = data.DataLoader(test_dataset,
                             batch_size=1024,drop_last=True, num_workers=0)


#完整代码:
https://download.csdn.net/download/weixin_47707533/85269806?spm=1001.2014.3001.5503

你可能感兴趣的:(pytorch,lstm,rnn)