赞
踩
'''载入数据'''
from sklearn import datasets
boston = datasets.load_boston()
x,y = boston.data,boston.target
'''引入标准化函数'''
from sklearn import preprocessing
x_MinMax = preprocessing.MinMaxScaler()
y_MinMax = preprocessing.MinMaxScaler()
''' 将 y 转换成 列 '''
import numpy as np
y = np.array(y).reshape(len(y),1)
'''标准化'''
x = x_MinMax.fit_transform(x)
y = y_MinMax.fit_transform(y)
''' 按二八原则划分训练集和测试集 '''
from sklearn.model_selection import train_test_split
np.random.seed(2019)
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2)
'''模型构建'''
from sklearn.neural_network import MLPRegressor
fit1 = MLPRegressor(
hidden_layer_sizes=(100,50), activation='relu',solver='adam',
'''第一个隐藏层有100个节点,第二层有50个,激活函数用relu,梯度下降方法用adam'''
alpha=0.01,max_iter=200)
'''惩罚系数为0.01,最大迭代次数为200'''
print ("fitting model right now")
fit1.fit(x_train,y_train)
pred1_train = fit1.predict(x_train)
'''计算训练集 MSE'''
from sklearn.metrics import mean_squared_error
mse_1 = mean_squared_error(pred1_train,y_train)
print ("Train ERROR = ", mse_1)
'''计算测试集mse'''
pred1_test = fit1.predict(x_test)
mse_2 = mean_squared_error(pred1_test,y_test)
print ("Test ERROR = ", mse_2)
'''结果可视化'''
import matplotlib.pyplot as plt
xx=range(0,len(y_test))
plt.figure(figsize=(8,6))
plt.scatter(xx,y_test,color="red",label="Sample Point",linewidth=3)
plt.plot(xx,pred1_test,color="orange",label="Fitting Line",linewidth=2)
plt.legend()
plt.show()
结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。