利用keras自带房价数据集进行房价预测
Posted rising-sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用keras自带房价数据集进行房价预测相关的知识,希望对你有一定的参考价值。
1 import numpy as np 2 from keras.datasets import boston_housing 3 from keras import layers 4 from keras import models 5 from keras import optimizers 6 from keras.utils.np_utils import to_categorical 7 import matplotlib.pyplot as plt 8 9 def main(): 10 (train_data, train_labels), (test_data, test_labels) = boston_housing.load_data() 11 mean = train_data.mean(axis = 0)# 通过(x-均值)/方差 把数据变为均值为0,方差为1 12 std = train_data.std(axis = 0) 13 train_data -= mean 14 train_data /= std 15 test_data -= mean 16 test_data /= std 17 18 model = models.Sequential() 19 model.add(layers.Dense(64, activation = ‘relu‘, input_shape = (train_data.shape[1], ))) 20 model.add(layers.Dense(64, activation = ‘relu‘)) 21 model.add(layers.Dense(1)) 22 23 model.compile(optimizer = ‘rmsprop‘, loss = ‘mse‘, metrics = [‘mae‘])# mae: 平均绝对误差 24 25 model.fit(train_data, train_labels, epochs = 80, batch_size = 16) 26 27 _, mae = model.evaluate(test_data, test_labels) 28 print(mae) 29 30 if __name__ == "__main__": 31 main()
最后预测的房价还是和实际价格相差约 2000美元左右
以上是关于利用keras自带房价数据集进行房价预测的主要内容,如果未能解决你的问题,请参考以下文章
TF之LSTM/GRU:基于tensorflow框架对boston房价数据集分别利用LSTMGRU算法(batch_size调优对比)实现房价回归预测案例