基于skflow单变量逻辑回归

本例中,我们将会探索单变量的回归问题,不过要用一个新的库,这将会简化我们的创建流程,叫做skflow
1.有用的库和方法
就在TensorFlow发布后不久,skflow就发布了.skflow的目标是模拟sklearn的接口运行TensorFlow.这样比在TensorFlow的会话环境中运行会简化掉很多工作.
下面的实例中,我们将会重复之前的回归,不过以skflow的接口运行
本例中,我们将会学习如何生成一个具体的非常有组织的回归模型,只要设定一个日志文件夹做为参数即可
2.数据集描述
数据集跟之前一样,我们使用pandas加载数据

df = pd.read_csv("data/CHD.csv", header=0)
print df.describe()
3.模型结构
模型结构的代码在my_model中:

def my_model(X, y):
    return skflow.models.logistic_regression(X, y)
4.结果描述

score = metrics.accuracy_score(df['chd'].astype(float), classifier.predict(X))
print("Accuracy: %f" % score)
5.完整代码

import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics, preprocessing
import numpy as np
import pandas as pd
 
df = pd.read_csv("./CHD.csv", header=0)
print(df.describe())
 
def my_model(X, y):
return skflow.models.logistic_regression(X, y)
 
a = preprocessing.StandardScaler()
 
X =a.fit_transform(df['age'].astype(float).reshape(-1,1))
 
print(a.get_params())
classifier = skflow.TensorFlowEstimator(model_fn=my_model, n_classes=1)
classifier.fit(X, df['chd'].astype(float), logdir='./logistic')
print(classifier.get_tensor_value('logistic_regression/bias:0'))
print(classifier.get_tensor_value('logistic_regression/weight:0'))
score = metrics.accuracy_score(df['chd'].astype(float), classifier.predict(X))
print("Accuracy: %f" % score)