Pytorch: 全连接神经网络-解决 Boston 房价回归问题
Copyright: Jingmin Wei, Pattern Recognition and Intelligent System, School of Artificial and Intelligence, Huazhong University of Science and Technology
MLP 回归模型
使用sklearn库的fetch_california_housing()函数。数据集共包含20640个样本,有8个自变量。
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.datasets import fetch_california_housing
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD
import torch.utils.data as Data
import matplotlib.pyplot as plt
import seaborn as sns
房价数据准备
# 导入数据
housedata = fetch_california_housing()
# 切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(housedata.data, housedata.target,
test_size = 0.3, random_state = 42)
70% 训练集,30%测试集。
X_train, X_test, y_train, y_test
(array([[ 4.1312 , 35. , 5.88235294, ..., 2.98529412,
33.93 , -118.02 ],
[ 2.8631 , 20. , 4.40120968, ..., 2.0141129 ,
32.79 , -117.09 ],
[ 4.2026 , 24. , 5.61754386, ..., 2.56491228,
34.59 , -120.14 ],
...,
[ 2.9344 , 36. , 3.98671727, ..., 3.33206831,
34.03 , -118.38 ],
[ 5.7192 , 15. , 6.39534884, ..., 3.17889088,
37.58 , -121.96 ],
[ 2.5755 , 52. , 3.40257649, ..., 2.10869565,
37.77 , -122.42 ]]),
array([[ 1.6812 , 25. , 4.19220056, ..., 3.87743733,
36.06 , -119.01 ],
[ 2.5313 , 30. , 5.03938356, ..., 2.67979452,
35.14 , -119.46 ],
[ 3.4801 , 52. , 3.97715472, ..., 1.36033229,
37.8 , -122.44 ],
...,
[ 3.512 , 16. , 3.76228733, ..., 2.36956522,
33.67 , -117.91 ],
[ 3.65 , 10. , 5.50209205, ..., 3.54751943,
37.82 , -121.28 ],
[ 3.052 , 17. , 3.35578145, ..., 2.61499365,
34.15 , -118.24 ]]),
array([1.938, 1.697, 2.598, ..., 2.221, 2.835, 3.25 ]),
array([0.477 , 0.458 , 5.00001, ..., 2.184 , 1.194 , 2.098 ]))
# 数据标准化处理
scale = StandardScaler()
X_train_s = scale.fit_transform(X_train)
X_test_s = scale.transform(X_test)
# 将训练数据转为数据表
housedatadf = pd.DataFrame(data=X_train_s, columns = housedata.feature_names)
housedatadf['target'] = y_train
housedatadf
MedInc | HouseAge | AveRooms | AveB |
---|