目录
波士顿房价数据集
数据集
数据归一化
模型训练和预测
模型建立和训练
模型预测
总结
回归主要基于波士顿房价数据库进行建模,官方文档地址为:https://tensorflow.google.cn/tutorials/keras/basic_regression
波士顿房价数据集
数据集
波士顿数据集是一个回归问题。每个类的观察值数量是均等的,共有 506 个观察,13 个输入变量和1个输出变量。每条数据包含房屋以及房屋周围的详细信息。其中包含城镇犯罪率,一氧化氮浓度,住宅平均房间数,到中心区域的加权距离以及自住房平均房价等等。
但是对于回归问题,需要读取数据后需要将数据集打散,代码如下:
数据集标签展示:
| CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT |
0 | 0.07875 | 45.0 | 3.44 | 0.0 | 0.437 | 6.782 | 41.1 | 3.7886 | 5.0 | 398.0 | 15.2 | 393.87 | 6.68 |
1 | 4.55587 | 0.0 | 18.10 | 0.0 | 0.718 | 3.561 | 87.9 | 1.6132 | 24.0 | 666.0 | 20.2 | 354.70 | 7.12 |
2 | 0.09604 | 40.0 | 6.41 | 0.0 | 0.447 | 6.854 | 42.8 | 4.2673 | 4.0 | 254.0 | 17.6 | 396.90 | 2.98 |
3 | 0.01870 | 85.0 | 4.15 | 0.0 | 0.429 | 6.516 | 27.7 | 8.5353 | 4.0 | 351.0 | 17.9 | 392.43 | 6.36 |
4 | 0.52693 | 0.0 | 6.20 | 0.0 | 0.504 | 8.725 | 83.0 | 2.8944 | 8.0 | 307.0 | 17.4 | 382.00 | 4.63 |
数据归一化
数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间。在某些比较和评价的指标处理中经常会用到,去除数据的单位限制,将其转化为无量纲的纯数值,便于不同单位或量级的指标能够进行比较和加权。
模型训练和预测
模型建立和训练
模型建立的通用模式为网络结构确定(网络层数,节点数,输入,输出)、模型训练参数确定(损失函数,优化器、评价标准)、模型训练(训练次数,批次大小)
模型的训练代码如下:
模型预测
调用history函数可以实现训练过程的可视化
为了提前停止训练,可以通过设置回调函数EarlyStopping设置训练停止条件。
模型预测代码如下:
总结
总体代码如下:
对于回归问题的官方总结:
- 均方误差(MSE)是一种常见的用于回归问题损失函数。
- 平均绝对误差(MAE)也是一种常用评价指标而不是精度。
- 对于输入数据,归一化是十分必要的。
- 训练数据较少,则模型结构较小更合适,防止过拟合。
- 提前停止是防止过拟合的好办法。