–使用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)
构建最简单的网络:全连接,只有三层,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()
nn.MSELoss()函数就是均方损失函数(y-y*)^2
优化器就是根据网络反向传播的梯度信息来更新网络的参数,以起到降低loss函数值的作用
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.01)
网络输入只能是张量,数据集是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
loss = criterion(y_pred,y_train)
#梯度清0
optimizer.zero_grad()
#反向传播
loss.backward()
#权重调整
optimizer.step()
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
–使用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)
MMS = MinMaxScaler()
x_train = MMS.fit_transform(x_train)
x_test = MMS.fit_transform(x_test)
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
–使用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)
MMS = MinMaxScaler()
x_train = MMS.fit_transform(x_train)
x_test = MMS.fit_transform(x_test)
构建最简单的网络:全连接,只有三层,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)
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房价数据集进行预测。