撰写时间:2017.5.18
这篇博文记录的是如何使用tensorflow实现LR,其中没有使用tensorflow提供的learn包里面的函数.因为如果使用函数包里面的API,不仅数据格式处理麻烦,而且扩展性不强.所以就自己实现了一下.
运行环境:ubuntu14.04,python2.7,tensorflow0.12
数据集:波士顿房价
loss:平方损失函数
优化方法:梯度下降
下面直接贴代码
1.文件处理的代码
文件处理的能力真的很弱,所以代码写的应该不怎么样,看看就行,只能保证运行
其中利用了sklearn包里面的数据处理函数.
#coding:utf-8
from __future__ import division
import sklearn
import numpy as np
from sklearn.model_selection import train_test_split
def read_data_housing():
# file = open("housing.data.txt",'r');
# for line in file:
# print line;
a = np.loadtxt("housing.data.txt");
data = a[:,0:-1];
target = a[:,-1];
keys = ['data','labels']
train = {}.fromkeys(keys);
test = {}.fromkeys(keys);
train['data'],test['data'],train['labels'],test[