TensorFlow 实现实现Boston房价预测房价预测
TensorFlow 实现实现Boston房价预测房价预测综述代码与解释关键点结语
综述综述
本文使用了 TensorFlow 2.0 框架,搭建了 ANN(人工神经网络)(人工神经网络),实现 Boston 房价预测。本文使用的编程工具为 jupyter notebook,完整代码可以在我的GitHub中找到,GitHub
链接在此
Boston 房价预测,是一个非常经典的案例了,已有许多学者对其进行了各式各样的研究,也通过拟合各种各样的模型,对该问题做出了实现。
通过该案例,我相信你一定能进一步的学习到 TensorFlow 2.0 下的 ANN 的搭建,相信你可以通过该案例,有所收益。
代码与解释代码与解释
导入所需的库函数
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import sklearn
读入数据,并查看数据维度
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
print(housing.data.shape)
print(housing.target.shape)
由图可得,数据一共有20640条,其中自变量数据有20640行、8列的数据。
部分数据查看,查看前五条数据
print((housing.data[0:5], housing.target[0:5]))
数据拆分,1/4分
from sklearn.model_selection import train_test_split
x_train_all, x_test, y_train_all, y_test = train_test_split(housing.data, housing.target, random_state = 7)
x_train, x_valid, y_train, y_valid = train_test_split(x_train_all, y_train_all, random_state = 11)
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()
x_train_scaled = scaler.fit_transform(x_train)
x_vaild_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)