TensorFlow 回归模型--房价预测数据集

TensorFlow 回归模型--房价预测数据集_第1张图片
TensorFlow 回归模型--房价预测数据集_第2张图片

TensorFlow 房价预测数据集(模型,训练,测试)

运行环境:
TensorFlow 回归模型--房价预测数据集_第3张图片

import pprint
import sys

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
import tensorflow as tf
from tensorflow import keras

print(tf.__version__)
print(sys.version_info)

for module in mpl, np, pd, sklearn, keras, tf:
    print(module.__name__, module.__version__)

from sklearn.datasets import fetch_california_housing

# 1.加载数据集 波士顿房价预测
housing = fetch_california_housing()
print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)

pprint.pprint(housing.data[:5])
pprint.pprint(housing.target[:5])

from sklearn.model_selection import train_test_split

# 2.拆分数据集
#   训练集与测试集拆分
x_train_all, x_test, y_train_all, y_test = train_test_split(housing.data,
                                                            housing.target,
                                                            random_state=7,
                                                            test_size=0.20)
# 训练集与验证集的拆分
x_train, x_valid, y_train, y_valid = train_test_split(
    x_train_all, y_train_all, random_state=11, test_size=0.20)

print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

# 3、数据预处理 数据集的归一化
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)

# 4、网络模型的搭建
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(30, activation='relu', input_shape=x_train.shape[1:]),
    tf.keras.layers.Dense(20, activation='relu'),
    tf.keras.layers.Dense(1),
])

print(model.layers)
model.summary()

# 5、模型的编译  设置损失函数 优化器
model.compile(loss='mean_squared_error',
              optimizer='sgd')

# 6、设置回调函数
callbacks = [tf.keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)]

# 7、训练网络
history = model.fit(x_train_scaled, y_train,
                    validation_data=(x_valid_scaled, y_valid),
                    epochs=100,
                    callbacks=callbacks)


# 8、绘制训练过程数据
def plot_learning_curves(hst):
    pd.DataFrame(hst.history).plot()
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()


plot_learning_curves(history)

# 9.验证数据
model.evaluate(x_test_scaled, y_test)

TensorFlow 回归模型--房价预测数据集_第4张图片

你可能感兴趣的:(TensorFlow)