tensorflow采用keras序列模型,实现波士顿房价预测问题的建模与训练
时间: 2023-11-22 16:52:33 浏览: 33
好的,下面是用Keras序列模型实现波士顿房价预测问题的建模与训练的步骤:
1. 导入需要的库和数据集
```python
import numpy as np
from keras.datasets import boston_housing
from keras.models import Sequential
from keras.layers import Dense
# 导入波士顿房价数据集
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()
```
2. 数据预处理
```python
# 标准化数据
mean = x_train.mean(axis=0)
std = x_train.std(axis=0)
x_train -= mean
x_train /= std
x_test -= mean
x_test /= std
# 将房价转化为 1000 美元为单位
y_train *= 1000
y_test *= 1000
```
3. 构建Keras序列模型
```python
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(13,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(1))
```
4. 编译模型
```python
model.compile(loss='mse', optimizer='rmsprop', metrics=['mae'])
```
5. 训练模型
```python
history = model.fit(x_train, y_train, epochs=100, batch_size=16,
validation_split=0.2, verbose=0)
```
6. 评估模型
```python
test_mse_score, test_mae_score = model.evaluate(x_test, y_test)
print('Test MAE:', test_mae_score)
```
通过以上六步,我们可以使用Keras序列模型完成波士顿房价预测问题的建模与训练。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)