集团站切换校区

验证码已发送,请查收短信

复制成功
微信号:togogoi
添加微信好友, 详细了解课程
已复制成功,如果自动跳转微信失败,请前往微信添加好友
打开微信
图标

学习文章

当前位置:首页 > >学习文章 > >

{HCNA-AI TensorFlow编程基础}之线性回归——房价预测

发布时间: 2019-01-24 17:53:29

9.1.1 实验介绍9.1.2 关于本实验本实验为 TensorFlow 线性回归的一个实际案例即房价预测。9.1.3 实验目的理解线性回归。

理解如何利用 TensorFlow 做预测。9.1.4 实验介绍本实验通过一个预测房屋价格的实例来讲解利用线性回归预测房屋价格,以及在 tensorflow 中如何实现。平时常用的房价预测数据集为波士顿房价数据集,本实验采用的是北京的房价数据集,更贴近国人的生活。9.1.5 实验步骤步骤 1 步骤 1 登陆华为云。

步骤 2 点击右上方的控制台。

步骤 3 选择弹性云服务器,网页中会显示该弹性云的可进行的操作,选择远程登录。即登录到弹性云服务器。

步骤 4 输入指令 ll,查看当前目录下的文件。

步骤 5 输入命令 vi house_price.py,创建新的 Python 脚本。

步骤 6 输入命令 i,进入编辑模式开始编辑,输入脚本内容。

步骤 7 输入命令 :wq!,保存并退出。

步骤 8 输入命令 cat house_price.py 查看代码。

步骤 9 运行测试。输入命令:python3 house_price.py。




9.2 实验过程9.2.1 设置编码说明# coding:utf-89.2.2 导入模块#载入此项目所需的库

from   future import print_function, division import tensorflow as tf

import pandas as pd import numpy as np

import matplotlib.pyplot as plt import seaborn

9.2.3 导入数据

该实验数据来源为:https://github.com/cunxi1992/boston_housing 中的 bj_housing2.csv 文件。读取数据:

train = pd.read_csv("bj_housing2.csv")

9.2.4 定义参数

train = train[train['Area'] < 12000]

train_X = train['Area'].values.reshape(-1, 1) train_Y = train['Value'].values.reshape(-1, 1) n_samples = train_X.shape[0]

# 定义参数,设置学习率

learning_rate = 2

# 设置训练次数

training_epochs = 1000

# 设置多少次显示一次

display_step = 50

9.2.5 定义占位符

# 定义 X,Y 占位符

X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32)

# 使用 Variable 定义的学习参数

W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32) b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32)

9.2.6 构建正向传播结构

# 构建正向传播结构

pred = tf.add(tf.multiply(W, X), b)




#损失函数

cost = tf.reduce_sum(tf.pow(pred-Y, 2)) / (2 * n_samples) # ʹ使用梯度下降优化器

optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

9.2.7 初始化

# 激 活 Init

init = tf.global_variables_initializer()


# 启动 session,初始化变量with tf.Session() as sess:

sess.run(init)

9.2.8 启动循环

#启动循环开始训练

for epoch in range(training_epochs): for (x, y) in zip(train_X, train_Y):

sess.run(optimizer, feed_dict={X: x, Y: y})

#显示训练中的详细信息

if (epoch + 1) % display_step == 0:

c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})

print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.3f}".format(c), "W=", sess.run(W), "b=", sess.run(b)) #显示训练中的详细信息


print("Optimization Finished!")

training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})

print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

9.2.9 展示训练结果

#展示训练结果

plt.plot(train_X, train_Y, 'ro', label="Original data")

plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label="Fitted line") plt.legend()

plt.show()

9.2.10 实验结果

输出结果:Epoch:0050cost=16308.796W=1.8889627b=155.08276Epoch:0100cost=16308.796W=1.8889627b=155.08276Epoch:0150cost=16308.796W=1.8889627b=155.08276Epoch:0200cost=16308.796W=1.8889627b=155.08276Epoch:0250cost=16308.796W=1.8889627b=155.08276Epoch:0300cost=16308.796W=1.8889627b=155.08276Epoch:0350cost=16308.796W=1.8889627b=155.08276Epoch:0400cost=16308.796W=1.8889627b=155.08276



Epoch:0450cost=16308.796W=1.8889627b=155.08276Epoch:0500cost=16308.796W=1.8889627b=155.08276Epoch:0550cost=16308.796W=1.8889627b=155.08276Epoch:0600cost=16308.796W=1.8889627b=155.08276……        

9.3 实例描述

本实验利用网上已有的北京房价数据集预测了北京的房价,实现了 TensorFlow 的线性回归应

上一篇: {HTML5}表单选择器-第一节

下一篇: {SpringBoot}springboot引入模版引擎

十五年老品牌
微信咨询:togogoi 咨询电话:18922156670 咨询网站客服:在线客服

相关课程推荐

在线咨询 ×

您好,请问有什么可以帮您?我们将竭诚提供最优质服务!