我想通过使用Tensorflow构建一个多元线性回归模型.

一个数据示例:2104,3,399900(前两个是功能,最后一个是房价;我们有47个示例)

代码如下:

import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

# model parameters as external flags

flags = tf.app.flags

FLAGS = flags.FLAGS

flags.DEFINE_float('learning_rate', 1.0, 'Initial learning rate.')

flags.DEFINE_integer('max_steps', 100, 'Number of steps to run trainer.')

flags.DEFINE_integer('display_step', 100, 'Display logs per step.')

def run_training(train_X, train_Y):

X = tf.placeholder(tf.float32, [m, n])

Y = tf.placeholder(tf.float32, [m, 1])

# weights

W = tf.Variable(tf.zeros([n, 1], dtype=np.float32), name="weight")

b = tf.Variable(tf.zeros([1], dtype=np.float32), name="bias")

# linear model

activation = tf.add(tf.matmul(X, W), b)

cost = tf.reduce_sum(tf.square(activation - Y)) / (2*m)

optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)

with tf.Session() as sess:

init = tf.initialize_all_variables()

sess.run(init)

for step in range(FLAGS.max_steps):

sess.run(optimizer, feed_dict={X: np.asarray(train_X), Y: np.asarray(train_Y)})

if step % FLAGS.display_step == 0:

print "Step:", "%04d" % (step+1), "Cost=", "{:.2f}".format(sess.run(cost, \

feed_dict={X: np.asarray(train_X), Y: np.asarray(train_Y)})), "W=", sess.run(W), "b=", sess.run(b)

print "Optimization Finished!"

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

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

print "Predict.... (Predict a house with 1650 square feet and 3 bedrooms.)"

predict_X = np.array([1650, 3], dtype=np.float32).reshape((1, 2))

# Do not forget to normalize your features when you make this prediction

predict_X = predict_X / np.linalg.norm(predict_X)

predict_Y = tf.add(tf.matmul(predict_X, W),b)

print "House price(Y) =", sess.run(predict_Y)

def read_data(filename, read_from_file = True):

global m, n

if read_from_file:

with open(filename) as fd:

data_list = fd.read().splitlines()

m = len(data_list) # number of examples

n = 2 # number of features

train_X = np.zeros([m, n], dtype=np.float32)

train_Y = np.zeros([m, 1], dtype=np.float32)

for i in range(m):

datas = data_list[i].split(",")

for j in range(n):

train_X[i][j] = float(datas[j])

train_Y[i][0] = float(datas[-1])

else:

m = 47

n = 2

train_X = np.array( [[ 2.10400000e+03, 3.00000000e+00],

[ 1.60000000e+03, 3.00000000e+00],

[ 2.40000000e+03, 3.00000000e+00],

[ 1.41600000e+03, 2.00000000e+00],

[ 3.00000000e+03, 4.00000000e+00],

[ 1.98500000e+03, 4.00000000e+00],

[ 1.53400000e+03, 3.00000000e+00],

[ 1.42700000e+03, 3.00000000e+00],

[ 1.38000000e+03, 3.00000000e+00],

[ 1.49400000e+03, 3.00000000e+00],

[ 1.94000000e+03, 4.00000000e+00],

[ 2.00000000e+03, 3.00000000e+00],

[ 1.89000000e+03, 3.00000000e+00],

[ 4.47800000e+03, 5.00000000e+00],

[ 1.26800000e+03, 3.00000000e+00],

[ 2.30000000e+03, 4.00000000e+00],

[ 1.32000000e+03, 2.00000000e+00],

[ 1.23600000e+03, 3.00000000e+00],

[ 2.60900000e+03, 4.00000000e+00],

[ 3.03100000e+03, 4.00000000e+00],

[ 1.76700000e+03, 3.00000000e+00],

[ 1.88800000e+03, 2.00000000e+00],

[ 1.60400000e+03, 3.00000000e+00],

[ 1.96200000e+03, 4.00000000e+00],

[ 3.89000000e+03, 3.00000000e+00],

[ 1.10000000e+03, 3.00000000e+00],

[ 1.45800000e+03, 3.00000000e+00],

[ 2.52600000e+03, 3.00000000e+00],

[ 2.20000000e+03, 3.00000000e+00],

[ 2.63700000e+03, 3.00000000e+00],

[ 1.83900000e+03, 2.00000000e+00],

[ 1.00000000e+03, 1.00000000e+00],

[ 2.04000000e+03, 4.00000000e+00],

[ 3.13700000e+03, 3.00000000e+00],

[ 1.81100000e+03, 4.00000000e+00],

[ 1.43700000e+03, 3.00000000e+00],

[ 1.23900000e+03, 3.00000000e+00],

[ 2.13200000e+03, 4.00000000e+00],

[ 4.21500000e+03, 4.00000000e+00],

[ 2.16200000e+03, 4.00000000e+00],

[ 1.66400000e+03, 2.00000000e+00],

[ 2.23800000e+03, 3.00000000e+00],

[ 2.56700000e+03, 4.00000000e+00],

[ 1.20000000e+03, 3.00000000e+00],

[ 8.52000000e+02, 2.00000000e+00],

[ 1.85200000e+03, 4.00000000e+00],

[ 1.20300000e+03, 3.00000000e+00]]

).astype('float32')

train_Y = np.array([[ 399900.],

[ 329900.],

[ 369000.],

[ 232000.],

[ 539900.],

[ 299900.],

[ 314900.],

[ 198999.],

[ 212000.],

[ 242500.],

[ 239999.],

[ 347000.],

[ 329999.],

[ 699900.],

[ 259900.],

[ 449900.],

[ 299900.],

[ 199900.],

[ 499998.],

[ 599000.],

[ 252900.],

[ 255000.],

[ 242900.],

[ 259900.],

[ 573900.],

[ 249900.],

[ 464500.],

[ 469000.],

[ 475000.],

[ 299900.],

[ 349900.],

[ 169900.],

[ 314900.],

[ 579900.],

[ 285900.],

[ 249900.],

[ 229900.],

[ 345000.],

[ 549000.],

[ 287000.],

[ 368500.],

[ 329900.],

[ 314000.],

[ 299000.],

[ 179900.],

[ 299900.],

[ 239500.]]

).astype('float32')

return train_X, train_Y

def feature_normalize(train_X):

train_X_tmp = train_X.transpose()

for N in range(2):

train_X_tmp[N] = train_X_tmp[N] / np.linalg.norm(train_X_tmp[N])

train_X = train_X_tmp.transpose()

return train_X

import sys

def main(argv):

if not argv:

print "Enter data filename."

sys.exit()

filename = argv[1]

train_X, train_Y = read_data(filename, False)

train_X = feature_normalize(train_X)

run_training(train_X, train_Y)

if __name__ == '__main__':

tf.app.run()

结果我得到:

with learning rate 1.0 and 100 iterations, the model finally predicts a house with 1650 square feet and 3 bedrooms get a price $752,903, with:

Training Cost= 4.94429e+09

W= [[ 505305.375] [ 177712.625]]

b= [ 247275.515625]

我的代码中肯定会有一些错误,因为不同学习率的成本函数图与solution不一样

我应该得到以下结果:

theta_0: 340,413

theta_1: 110,631

theta_2: -6,649

The predicted price of the house should be $293,081.

我使用tensorflow有什么问题吗?

python多元线性回归模型_python – 使用Tensorflow的多元线性回归模型相关推荐

  1. 【深度学习-微调模型】使用Tensorflow Slim fine-tune(微调)模型

    本文主要讲解在现有常用模型基础上,如何微调模型,减少训练时间,同时保持模型检测精度. 首先介绍下Slim这个Google公布的图像分类工具包,可在github链接:modules and exampl ...

  2. python网页动图_python,tensorflow线性回归Django网页显示Gif动态图

    1.工程组成 2.urls.py """Django_machine_learning_linear_regression URL Configuration The ` ...

  3. python数据趋势算法_Python数据拟合与广义线性回归算法学习

    机器学习中的预测问题通常分为2类:回归与分类. 简单的说回归就是预测数值,而分类是给数据打上标签归类. 本文讲述如何用Python进行基本的数据拟合,以及如何对拟合结果的误差进行分析. 本例中使用一个 ...

  4. python分类算法评估模型_Python机器学习(sklearn)——分类模型评估与调参总结(下)...

    21.集成方法有随机森林(random forest)和梯度提升树(gradient boosted decision tree)GBDT 随机森林中树的随机化方法有两种: (1)通过选择用于构造树的 ...

  5. python 训练识别验证码_python使用tensorflow深度学习识别验证码

    本文介绍了python使用tensorflow深度学习识别验证码 ,分享给大家,具体如下: 除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorflow训练来识 ...

  6. python生产和消费模型_python queue和生产者和消费者模型

    queue队列 当必须安全地在多个线程之间交换信息时,队列在线程编程中特别有用. classqueue.Queue(maxsize=0) #先入先出classqueue.LifoQueue(maxsi ...

  7. python面向对象设计管理系统_python面向对象之单例设计模型

    单例 目标 单例设计模式 `__new__` 方法 Python 中的单例 01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的 ...

  8. python 两阶段聚类_Python,如何对多元时间序列进行聚类?

    我有一个玩具时间序列数据框,格式如下: >>> df dtime dev sw1 sw2 0 2020-01-01 00:00:00 A1 5.496714 5.593792 1 2 ...

  9. python多元线性回归实例_Python机器学习多元线性回归模型 | kTWO-个人博客

    前言 在上一篇文章<机器学习简单线性回归模型>中我们讲解分析了Python机器学习中单输入的线性回归模型,但是在实际生活中,我们遇到的问题都是多个条件决定的问题,在机器学习中我们称之为多元 ...

最新文章

  1. Spring MVC 五大组件
  2. pandas读写结构化数据(read_csv,read_table, read_excel, read_html, read_sql)
  3. CSS3 box-orient box-direction box-align box-flex box-pack
  4. XShell常用快捷键
  5. 【SDL】SDL学习笔记一 SDL的子系统的初始化和退出
  6. spring之:XmlWebApplicationContext作为Spring Web应用的IoC容器,实例化和加载Bean的过程...
  7. 5个经典的javascript面试问题
  8. 登陆时验证码的制作(asp.net)
  9. php 周末 培训,济南php周末培训班
  10. VB 利用fso 枚举文件和文件夹
  11. RS-232、RS422和RS-485的区别和各自的实现方式
  12. ELK logstash gork匹配在线测试
  13. cecore.cls.php 08cms,08cms小说系统 v1.0PHP CMS源码下载-华软网
  14. vue.js可视化开发工具_Vue.js开发工具
  15. 金盘系统无法连接服务器,西数金盘Gold系列主要面向企业级服务器及存储系统...
  16. matlab学习笔记 clc和clear
  17. h5页面如何切图_H5设计稿切图按照什么尺寸,微信公众号版本的
  18. 张景明:方剂【方歌】——温里剂
  19. 一键生成表白页面,个人网站,在线制作生成网站php源码
  20. mysql和mongo+查询效率_Mongodb VS Mysql 查询性能

热门文章

  1. 获取SQL Server 2000数据库和表空间使用信息
  2. 【转】Java中File常用的方法汇总
  3. Maven学习 使用Nexus搭建Maven私服(转)
  4. mysql一些查询方法记录
  5. 【TCP/IP详解 卷一:协议】第十二章 广播和多播
  6. JAVA中类似C中memcpy功能
  7. Linux下简单的邮件服务器搭建
  8. 问题1:程序员要做一辈子?
  9. Adobe Flex 3.0 和 AIR 1.0 正式发布
  10. bat自动输入密码登录_如何制作自动设置计算机管理员密码的脚本