利用TensorFlow实现多元线性回归
利用TensorFlow实现多元线性回归,代码如下:
# -*- coding:utf-8 -*- import tensorflow as tf import numpy as np from sklearn import linear_model from sklearn import preprocessing# Read x and y x_data = np.loadtxt("ex3x.dat").astype(np.float32) y_data = np.loadtxt("ex3y.dat").astype(np.float32)# We evaluate the x and y by sklearn to get a sense of the coefficients. reg = linear_model.LinearRegression() reg.fit(x_data, y_data) print ("Coefficients of sklearn: K=%s, b=%f" % (reg.coef_, reg.intercept_))# Now we use tensorflow to get similar results. # Before we put the x_data into tensorflow, we need to standardize it # in order to achieve better performance in gradient descent; # If not standardized, the convergency speed could not be tolearated. # Reason: If a feature has a variance that is orders of magnitude larger than others, # it might dominate the objective function # and make the estimator unable to learn from other features correctly as expected. scaler = preprocessing.StandardScaler().fit(x_data) print (scaler.mean_, scaler.scale_) x_data_standard = scaler.transform(x_data)W = tf.Variable(tf.zeros([2, 1])) b = tf.Variable(tf.zeros([1, 1])) y = tf.matmul(x_data_standard, W) + bloss = tf.reduce_mean(tf.square(y - y_data.reshape(-1, 1)))/2 optimizer = tf.train.GradientDescentOptimizer(0.3) train = optimizer.minimize(loss)init = tf.initialize_all_variables()sess = tf.Session() sess.run(init) for step in range(100):sess.run(train)if step % 10 == 0:print (step, sess.run(W).flatten(), sess.run(b).flatten())print ("Coefficients of tensorflow (input should be standardized): K=%s, b=%s" % (sess.run(W).flatten(), sess.run(b).flatten())) print ("Coefficients of tensorflow (raw input): K=%s, b=%s" % (sess.run(W).flatten() / scaler.scale_, sess.run(b).flatten() - np.dot(scaler.mean_ / scaler.scale_, sess.run(W))))
数据集下载:下载地址
利用TensorFlow实现多元线性回归相关推荐
- Tensorflow实现多元线性回归
上一篇使用tensorflow实现了简单的线性回归,这次在简单的线性回归基础上,通过在权重和占位符声明中 修改来对相同的数据进行多元线性回归. 同样以波士顿房价数据为例 波士顿房价数据集可从http: ...
- 多元线性回归分析matlab实验报告,利用MATLAB进行多元线性回归.ppt
<利用MATLAB进行多元线性回归.ppt>由会员分享,可在线阅读,更多相关<利用MATLAB进行多元线性回归.ppt(15页珍藏版)>请在人人文库网上搜索. 1.2.线性回归 ...
- 怎么用matlab做多元线性回归方程,利用MATLAB进行多元线性回归
<利用MATLAB进行多元线性回归>由会员分享,可在线阅读,更多相关<利用MATLAB进行多元线性回归(15页珍藏版)>请在人人文库网上搜索. 1.2.线性回归,b=regre ...
- python多元线性回归模型_python – 使用Tensorflow的多元线性回归模型
我想通过使用Tensorflow构建一个多元线性回归模型. 一个数据示例:2104,3,399900(前两个是功能,最后一个是房价;我们有47个示例) 代码如下: import numpy as np ...
- 利用python实现多元线性回归确定喷墨大师的命中方向
喷墨大师是玩吧app的一款小游戏,两只章鱼相互用墨汁进行投射对战,利用风力.角度.力度.距离四个因素向敌方进行投射,命中后会对敌方造成一定的血量损失,直到地方血量为0即可获得胜利 一.游戏介绍,变量设 ...
- 利用python实现多元线性回归
前言 本文介绍了如何用python进行回归分析 一.简单线性回归 直线回归分析是研究两变量(自变量和因变量)之间的依存关系及其关系的具体方程的形式.分析中所形成的这种关系式称为回归模型,其中以一条直线 ...
- 利用tensorflow语法实现线性回归
#-*-coding:utf-8-*- import tensorflow as tf #面向对象 class MyLinearRegression(object):def __init__(self ...
- matlab regress RMSE,在利用regress进行多元线性回归中出现的问题
在利用这个命令X=[ones(size(T1)),T1,T2,T3,T4];[b,bint,r,rint,stats]=regress(Y,X),Q=r'*r,Q2=Q/(15-2),stepwise ...
- TensorFlow进行多元线性回归
设置四个输入x1,x2,x3,x4,y为输出,通过TensorFlow拟合得到各自系数. 自定义数据集,x1,x2,x3,x4为随机生成(0,1)之间的随机数,y为x1+x2+x3+x4+随机噪声. ...
最新文章
- linux复杂逻辑判断,20.5 shell脚本中的逻辑判断
- dijkstra算法_Python实现图的经典DFS、BFS、Dijkstra、Floyd、Prim、Kruskal算法
- ubuntu 网卡双网口 配置_无线网卡m2 ngff keya keye、minipcie接口改转多口有线网卡实现软路...
- Android无线测试之—UiAutomator UiDevice API介绍六
- Mariadb升级到mysql_mariadb的版本升级方法
- ECCV 2020,一种灵活高效的权重生成网络框架
- Spark API编程动手实战-08-基于IDEA使用Spark API开发Spark程序-01
- mangle 打标签冲突
- vb net excel 剪贴板 粘贴_excel表格操作: 图形和图表编辑技巧汇总(一)
- 开启html元素的编辑模式contenteditable=true
- 如何找到稳定的网站空间
- Java jar包在线反编译
- java 后端处理PDF图册
- 计算机excel公式教案,Excel利用函数进行数据计算(教案)
- 系列4—BabeLua常见问题
- 谐振功率放大器的工作原理
- Google插件switchysharp的用法
- 关于 Alpine Docker 镜像漏洞 CVE-2019-5021
- ZBrush软件中的笔触类型
- 《Understanding and Diagnosing Visual Tracking Systems》论文笔记
热门文章
- 华为云NP考试题库_华为认证网络工程师怎么考
- Java项目:精品酒店管理系统(java+SSM+mysql+maven+tomcat)
- ios 自动打包命令_iOS自动打包上传脚本
- layUI 学习记录
- WKWebView Safari调试、JS互调、加载进度条、JS中alert、confirm、prompt
- springboot过滤器排除掉一些url_理解这9大内置过滤器,才算是精通Shiro
- 小程序判断用户在线状态
- 微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能
- 贝塞尔曲线动画demo(仿美人相机效果)
- 优化实战:不要随便将字段折腾来折腾去的