小白学Pytorch使用(2-2):气温预测回归

本文介绍了如何使用Python的PyTorch库处理气温预测数据集,包括数据预处理(独热编码和标准化)、构建神经网络模型(包含隐藏层和线性输出层)、以及训练过程和结果的可视化。作者展示了如何使用历史气温数据和时间信息预测当天实际温度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

任务背景

利用年、月、日、周几、前天的最高温度值、昨天的最高温度值、历史这天平均最高温度值来预测当天的实际温度值,数据如下:
气温预测数据集

一、导入库

# 矩阵计算库
import numpy as np
# 数据基本处理库,可读取csv文件
import pandas as pd
# 画图展示库
import matplotlib.pyplot as plt
# pytorch框架
import torch
# 优化器
import torch.optim as optim
# 处理时间数据
import datetime
# 归一化数据
from sklearn import preprocessing
# 警告
import warnings
warnings.filterwarnings("ignore")

二、处理数据集

# 更改为自己的文件夹路径
features = pd.read_csv('D:/咕泡人工智能-配套资料/配套资料/4.第四章 深度学习核⼼框架PyTorch/第二,三章:神经网络实战分类与回归任务/神经网络实战分类与回归任务/temps.csv')
# print(features.head())
'''
   year  month  day  week  temp_2  temp_1  average  actual  friend
0  2016      1    1   Fri      45      45     45.6      45      29
1  2016      1    2   Sat      44      45     45.7      44      61
2  2016      1    3   Sun      45      44     45.8      41      56
3  2016      1    4   Mon      44      41     45.9      40      53
4  2016      1    5  Tues      41      40     46.0      44      41
year,moth,day,week分别表示的具体的时间
temp_2:前天的最高温度值
temp_1:昨天的最高温度值
average:在历史中,每年这一天的平均最高温度值
actual:这就是我们的标签值了,当天的真实最高温度
friend:这一列可能是凑热闹的,你的朋友猜测的可能值,咱们不管它就好了
'''
# print(features.shape)       #(348, 9)

# 获取年月日数据
years = features['year']
months = features['month']
days = features['day']

# 转换为datatime格式,年-月-日
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
dates = [datetime.datetime.strptime(date, '%Y-%m-%d')for date in dates]
# print(dates[:1])    #[datetime.datetime(2016, 1, 1, 0, 0)] 2016.1.1

三、数据展示

# 画图,设置图像风格
plt.style.use('fivethirtyeight')

# 设置布局
# 2行2列10*10图像布局,ax1,ax2, ax3, ax4四个子图
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值