1.Pytorch对房价数据集的预测
1.1 加载原始数据以及所需要的包
–使用sklearn.datasets.load_boston对原始数据集进行加载
import torch.nn as nn
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torch.utils.data as Data
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
对数据进行规范化,并划分训练集与测试集
data = load_boston()
x =data['data']
y = data['target']
y = y.reshape(-1,1)
mm_scale = MinMaxScaler()
x = mm_scale.fit_transform(x)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)
1.2 搭建网络
构建最简单的网络:全连接,只有三层,13输入—>10个神经元的隐藏层—>10个神经元的隐藏层—>1输出。
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(13, 10)
self.active2 = torch.nn.ReLU()
self.linear3 = torch.nn.Linear(10,10)
self.active4 = torch.nn.ReLU()
self.linear5 = torch.nn.Linear(10,1)
def forward(self, x):
x = self.linear1(x)
x = self.active2(x)
x = self.linear3(x)
x = self.active4(x)
x = self.linear5(x)
return x
model = Model()
1.3定义损失函数与优化器
nn.MSELoss()函数就是均方损失函数(y-y*)^2 优化器就是根据网络反向传播的梯度信息来更新网络的参数,以起到降低loss函数值的作用
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.01)
1.4 模型训练
网络输入只能是张量,数据集是numpy.ndarray(可以使用train_set_input.type()查看数据类型),需要使用torch.FloatTensor()进行类型转换。
x_train = torch.FloatTensor(x_train)
x_test = torch.FloatTensor(x_test)
y_train = torch.FloatTensor(y_train)
y_test = torch.FloatTensor(y_test)
max_epoch = 10000
for i in range(max_epoch):
y_pred = model(x_train)
loss = criterion(y_pred,y_train)
optimizer.zero_grad()
loss.backward()
optimizer.step()
1.5 验证模型效果
output = model(x_test)
predict_list = output.detach().numpy()
print(np.hstack((y_test.numpy(),predict_list))[:10].T)
plt.plot(y_test.numpy(),color="red",linewidth=2)
plt.plot(predict_list,color="green",linewidth=2)
plt.show
预测结果展示:
--------------------------------------------------------------------------------------------------------------------------------
2.Sklearn对房价数据集的预测
2.1 加载原始数据以及所需要的包
–使用sklearn.datasets.load_boston对原始数据集进行加载
mport numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
data = load_boston()
X,y = data["data"],data["target"]
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
2.2 数据预处理-归一化
MMS = MinMaxScaler()
x_train = MMS.fit_transform(x_train)
x_test = MMS.fit_transform(x_test)
2.3 模型的训练与预测
LR = LinearRegression()
LR.fit(x_train,y_train)
y_pred = LR.predict(x_test)
print(y_pred[:20],y_test[:20])
plt.plot(y_pred,color="red",linewidth=2)
plt.plot(y_test,color="green",linewidth=2)
plt.show
结果展示:
--------------------------------------------------------------------------------------------------------------------------------
3.Keras对房价数据集的预测
3.1 加载原始数据以及所需要的包
–使用sklearn.datasets.load_boston对原始数据集进行加载
import numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras import models
import matplotlib.pyplot as plt
data = load_boston()
X,y = data["data"],data["target"]
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
3.2 数据预处理-归一化
MMS = MinMaxScaler()
x_train = MMS.fit_transform(x_train)
x_test = MMS.fit_transform(x_test)
3.3 搭建网络
构建最简单的网络:全连接,只有三层,13输入—>64个神经元的隐藏层—>64个神经元的隐藏层—>1输出。
model = models.Sequential()
model.add(Dense(64,activation='relu',input_dim=13))
model.add(Dense(64,activation='relu'))
model.add(Dense(1))
model.compile(loss='mse',optimizer='rmsprop',metrics=['mae'])
history = model.fit(x_train,y_train,batch_size=32,epochs=1000,verbose=0)
3.4 模型的预测
y_pred = model.predict(x_test)
print(np.hstack((y_pred,y_test.reshape(-1,1)))[:10])
plt.plot(y_pred,color="red",linewidth=2)
plt.plot(y_test,color="green",linewidth=2)
plt.show
预测结果展示:
以上,我们完成了使用pytorch,sklearn,keras分别对Boston房价数据集进行预测。
|