【四二学堂】深度学习案例一-波士顿房价预测

【四二学堂】深度学习案例一-波士顿房价预测_第1张图片

#波士顿房价分析

from sklearn import datasets
import numpy as np
from keras.models import Sequential
from keras.layers import  Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

dataset=datasets.load_boston()

x=dataset.data
Y=dataset.target

seed=7
np.random.seed(seed)

def create_model(units_list=[13],optimizer='adam',init='normal'):
    #构建模型
    model=Sequential()
    #构建第一个隐藏层和输入层
    units=units_list[0]
    model.add(Dense(units=units,activation='relu',input_dim=13,kernel_initializer=init))

    #构建更多的隐藏层
    for units in units_list[1:]:
        model.add(Dense(units=units,activation='relu',kernel_initializer=init))
    model.add(Dense(units=1,kernel_initializer=init))
    #编译模型
    model.compile(loss='mean_squared_error',optimizer=optimizer)
    return model

model=KerasRegressor(build_fn=create_model,epochs=200,batch_size=5,verbose=0)

#设置算法评估基准
kfold=KFold(n_splits=10,shuffle=True,random_state=seed)
results=cross_val_score(model,x,Y,cv=kfold)
print('Baseline:%.2f(%.2f)MSE'%(results.mean(),results.std()))

steps=[]
steps.append(('standardize',StandardScaler()))
steps.append(('mlp',model))
pipeline=Pipeline(steps)
kfold=KFold(n_splits=10,shuffle=True,random_state=seed)
results=cross_val_score(pipeline,x,Y,cv=kfold)
print('Standize:%.2f(%.2f)MSE'%(results.mean(),results.std()))

你可能感兴趣的:(深度学习)