[ML]keras波士顿房价预测模型

Posted otwos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ML]keras波士顿房价预测模型相关的知识,希望对你有一定的参考价值。

from keras.datasets import boston_housing
from keras.models import Sequential
from keras.layers import Dense,BatchNormalization,Dropout,Reshape,Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D,Conv1D,MaxPooling1D
import numpy as np
import os

(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

def build0():
    model=Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def build1():
    model=Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Dense(13))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def build2():
    model=Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Dense(13,activation=tanh))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def build3():
    model=Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Dense(13,activation=sigmoid))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def build4():
    model=Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Reshape((13,1)))
    model.add(Conv1D(filters=13,strides=1,padding=same,kernel_size=2,activation=sigmoid))
    model.add(Conv1D(filters=26, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(MaxPooling1D(pool_size=2,strides=1,padding=same))
    model.add(Conv1D(filters=52, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(Conv1D(filters=104, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(MaxPooling1D(pool_size=2, strides=1, padding=same))
    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

def build5():
    model = Sequential(name=boston)
    model.add(BatchNormalization(input_shape=(13,)))
    model.add(Reshape((13, 1,1)))
    model.add(Conv2D(filters=13, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(Conv2D(filters=26, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(MaxPooling2D(pool_size=2, strides=1, padding=same))
    model.add(Conv2D(filters=52, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(Conv2D(filters=104, strides=1, padding=same, kernel_size=2, activation=sigmoid))
    model.add(MaxPooling2D(pool_size=2, strides=1, padding=same))
    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(1))
    return model

for i in range(6):
    model=eval("build"+str(i)+"()")
    model.compile(adam,mae)#3.0895
    history=model.fit(x_train,y_train,batch_size=404,epochs=10000,verbose=1,validation_data=(x_test,y_test))
    print(history.history)

    f=open("result.txt",a)
    f.write(str(history.history[val_loss][-1])+"\n")
    f.close()

其中build4方法预测最准确mae loss最小

以上是关于[ML]keras波士顿房价预测模型的主要内容,如果未能解决你的问题,请参考以下文章

ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例

Keras深度学习实战——房价预测

线性回归模型之波士顿房价预测

深度学习建模预测全流程(Python)!

波士顿房价处理

利用PaddlePaddle预测波士顿房价