利用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回归
多元线性回归模型中,如果所有特征一起上,容易造成过拟合使测试数据误差方差过大:因此减少不必要的特征,简化模型是减小方差的一个重要步骤.除了直接对特征筛选,来也可以进行特征压缩,减少某些不重要的特征系数 ...
随机推荐
- limits.conf文件修改注意事项
limits.conf文件限制着用户可以使用的最大文件数,最大线程,最大内存等资源使用量. vim /etc/security/limits.conf * soft nofile * hard nof ...
- eclipse启动报错:Could not create the java virtual machine
用maven.springboot开发时,安装了当时最新版的eclipse(3.5.5).eclipse解压版的非常方便,想先安装了看看.暂时没有升级其他软件. 打开的时候报错: 原因其实就是还木有升 ...
- find实现特殊功能示例
find列出目录下所有文件: # find /shell-script/ # find /shell-script/ -print find列出文件夹中所有开头为text的文件,参数-iname意思忽 ...
- C# RichTextBox的用法
https://www.cnblogs.com/arxive/p/5725570.html
- ORM Active Record Data Mapper
What's the difference between Active Record and Data Mapper? https://www.culttt.com/2014/06/18/whats ...
- [nginx] load balancing & location
一 将NGINX配置成7层load balancer,该怎么做? 参见: http://nginx.org/en/docs/http/load_balancing.html https://docs. ...
- [daily] pandoc
学了LaTeX之后,你就会很自然的接触的另一种观点: LaTeX是专注于排版的,你需要专注于内容. 于是,请使用pandoc. 简单的说, pandoc就是可以将各种个样格式的文档转换成各种各样格式的 ...
- Flink – submitJob
Jobmanager的submitJob逻辑, /** * Submits a job to the job manager. The job is registered at the library ...
- 内部排序->选择排序->简单选择排序
文字描述 简单排序的基本思想是:每一趟在n-i+1(i=1,2,…,n)个记录中选取关键字最小的记录作为有序列表中的第i个记录. 示意图 略 算法分析 简单排序算法中,所需进行记录移动的操作次数较少, ...
- jMeter_响应数据乱码
jMeter测试时服务响应数据乱码 方式一:修改配置文件参数 1.修改jMeter安装目录 jmeter.properties 中 sampleresult.default.encoding=UTF- ...