利用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) + b loss = 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实现多元逻辑回归,代码如下: import tensorflow as tf import numpy as np from sklearn.linear_model impo ...
- Tensorflow之多元线性回归问题(以波士顿房价预测为例)
一.根据波士顿房价信息进行预测,多元线性回归+特征数据归一化 #读取数据 %matplotlib notebook import tensorflow as tf import matplotlib. ...
- TensorFlow从0到1之TensorFlow实现多元线性回归(16)
在 TensorFlow 实现简单线性回归的基础上,可通过在权重和占位符的声明中稍作修改来对相同的数据进行多元线性回归. 在多元线性回归的情况下,由于每个特征具有不同的值范围,归一化变得至关重要.这里 ...
- 机器学习与Tensorflow(1)——机器学习基本概念、tensorflow实现简单线性回归
一.机器学习基本概念 1.训练集和测试集 训练集(training set/data)/训练样例(training examples): 用来进行训练,也就是产生模型或者算法的数据集 测试集(test ...
- 【TensorFlow篇】--Tensorflow框架初始,实现机器学习中多元线性回归
一.前述 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,T ...
- TensorFlow多元线性回归实现
多元线性回归的具体实现 导入需要的所有软件包: 因为各特征的数据范围不同,需要归一化特征数据.为此定义一个归一化函数.另外,这里添加一个额外的固定输入值将权重和偏置结合起来.为此定义函数 appe ...
- R语言解读多元线性回归模型
转载:http://blog.fens.me/r-multi-linear-regression/ 前言 本文接上一篇R语言解读一元线性回归模型.在许多生活和工作的实际问题中,影响因变量的因素可能不止 ...
- 利用R进行多元线性回归分析
对于一个因变量y,n个自变量x1,...,xn,要如何判断y与这n个自变量之间是否存在线性关系呢? 肯定是要利用他们的数据集,假设数据集中有m个样本,那么,每个样本都分别对应着一个因变量和一个n维的自 ...
- 多元线性回归模型的特征压缩:岭回归和Lasso回归
多元线性回归模型中,如果所有特征一起上,容易造成过拟合使测试数据误差方差过大:因此减少不必要的特征,简化模型是减小方差的一个重要步骤.除了直接对特征筛选,来也可以进行特征压缩,减少某些不重要的特征系数 ...
随机推荐
- zouxy09-图像卷积与滤波的一些知识点
原文地址 图像卷积与滤波的一些知识点 zouxy09@qq.com http://blog.csdn.net/zouxy09 之前在学习CNN的时候,有对卷积进行一些学习和整理,后来就烂尾了,现在稍微 ...
- background-color没有none值
在工作中发现, 这样是没反应的, 要写这个样式才能去掉背景颜色(background-color:transparent;)
- SecureCRT安装使用
● 解决自动断开 echo "TMOUT=6000 " >>/etc/profile source /etc/profile 在连接上右键属性,然后“终端”,“反空闲” ...
- Linux/Unix 常用参数使用说明
参数使用说明 ~ 表示当前用户目录 绝对路径 .表示当前目录 绝对路径 | 命令格式:命令A|命令B,即命令1的正确输出作为命令B的操作对象(下图应用别人的图片) 举例 ps aux | grep & ...
- 运行gedit报No protocol specified
No protocol specifiedxhost: unable to open display ":0" 这是因为在Xserver默认情况下,不允许Linux非当前登录用户( ...
- 【RMAN】RMAN-05001: auxiliary filename conflicts with the target database
oracle 11.2.0.4 运行以下脚本,使用活动数据库复制技术创建dataguard备库报错rman-005001: run{ duplicate target database for sta ...
- AlphaRacks 2018年黑五 VPS $3.99/年
发现这么久了这些链接还是能购买.算是捡了便宜了. 搭建shadowsocks非常合算. 我买了6.99美元的那个. VPS OVZ构架 1核/125MB/5GB/800GB流量/1 IPv4/OVZ/ ...
- android 平台签名
特定程序为了获取平台的权限,需要用平台签名 可以用 build/target/product 下的私钥来签名 bat代码如下: java -jar signapk.jar emt100d\platfo ...
- a buzzword to refer to modern Web technologies
https://html.spec.whatwg.org/multipage/introduction.html#is-this-html5? HTML Living Standard — Last ...
- php安全
1.会话安全性 会话固化 一种获取有效回话标识符的方法,他将运行恶意用户通过强制使用回话ID来轻松模拟一个真实用户 攻击方法:<a href="http://a.com/index.p ...