# 基于CNN_A_LSTM的小时天气预测 **Repository Path**: wangp06/CNN_A_LSTM_Weather_Predict ## Basic Information - **Project Name**: 基于CNN_A_LSTM的小时天气预测 - **Description**: 基于CNN_A_LSTM的小时天气预测 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2024-07-22 - **Last Updated**: 2024-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 基于CNN_A_LSTM的小时天气预测 #### 介绍 基于CNN_A_LSTM的小时天气预测(有问题请联系qq:869114539,vx:15234405680) | 文件 | 用途 | |---|---| | code目录 | 代码| | Houston.csv | 数据集 | | util.py | 工具类 | #### 基于深度学习的小时天气预测(温度单步预测) 基于torch框架完成 | 模型 | 参数 | |---|---| | RNN | 神经元个数=32 步长=24| | LSTM | 神经元个数=32 步长=24| | GRU | 神经元个数=64 步长=24| | CNN_LSTM | 神经元个数=64 步长=24| | CNN_A_LSTM | 神经元个数=64 步长=24| #### 缺失值处理 插值填充 df = df.interpolate() #### weather数值化 label_encoder = LabelEncoder() df['weather'] = label_encoder.fit_transform(df['weather']) category_mapping = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_))) ![输入图片说明](1703468248373.png) #### 数据格式处理 因需要预测未来一小时天气(时间序列单步预测),基于滑动窗口的方式对数据格式进行处理: ![输入图片说明](image.png) def create_sliding_window(data, window_size): window_data = [] for i in range(len(data) - window_size): window = data[i:i + window_size] target = data[i + window_size][2] window_data.append((window, target)) return window_data #### RNN实现 class RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(LSTMModel, self).__init__() self.hidden_size = hidden_size self.lstm = nn.RNN(input_size, hidden_size) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x): lstm_out, _ = self.lstm(x) output = self.fc(lstm_out[:, -1, :]) return output ![RNN训练Loss](rnn_loss.png)![RNN测试集预测结果展示](rnn_true_vs_predict.jpg) #### LSTM实现 class LSTMModel(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(LSTMModel, self).__init__() self.hidden_size = hidden_size self.lstm = nn.LSTM(input_size, hidden_size) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x): lstm_out, _ = self.lstm(x) output = self.fc(lstm_out[:, -1, :]) return output ![LSTM训练Loss](lstm_loss.jpg)![LSTM测试集预测结果展示](lstm_true_vs_predict.jpg) #### GRU实现 ![输入图片说明](gru_loss.jpg)![输入图片说明](gru_true_vs_predict.jpg) #### CNN_LSTM实现 class CNNLSTMModel(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(CNNLSTMModel, self).__init__() self.hidden_size = hidden_size self.conv1 = nn.Conv1d(input_size, hidden_size, kernel_size=3, padding=1) self.lstm = nn.LSTM(hidden_size, hidden_size) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x): x = x.permute(0, 2, 1) # 转换输入的维度,使其适合卷积层的输入 x = F.relu(self.conv1(x)) x = x.permute(2, 0, 1) # 将维度转换回原始维度 lstm_out, _ = self.lstm(x) output = self.fc(lstm_out[-1, :, :]) # 只使用最后一个时间步的输出 return output ![输入图片说明](cnn_lstm.jpg) ![输入图片说明](cnn_lstm_true_.jpg) #### CNN_A_LSTM实现 ![输入图片说明](cnn_a_lstm_loss.jpg)![输入图片说明](cnn_a_lstm_true_vs_predict.jpg)