基于tensorflow的logistics回归--数据集为 horseColicTest and horseColicTraining.txt(文章底部附数据集链接)

初学tensorfow,实现了一个简单的logistics回归,代码中有写内容还是在numpy中完成,然后转换到tensor,如果tensorflow中有对应方法,麻烦各位大佬提点,,谢谢

import os
import tensorflow as tf
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
'''load file and convert file to train data or test data'''
def loadfile(path):
    f=open(path)
    data=[]
    label=[]
    for line in f:
        line=line.strip().split('\t')
        lineconvert=[]
        for j in line:
            lineconvert.append(float(j))
        data.append(lineconvert[:-1])
        label.append(lineconvert[-1])
'''
        d=np.array(data)
        l=np.array(label)
        l=l.reshape(l.shape[0],1)
'''   
        d=np.array(data) 
        l=np.array([label])##这样写就避免了 list转换numpy矩阵时候出现(num,)类型的矩阵
 return d,l
def forwardcal(w,traindata,theta):
    Z = tf.matmul(w, traindata) + theta
    y = 1 / (1 + tf.exp(-Z))
    y_clip = tf.clip_by_value(y, eps, 1.0 - eps)
    return y_clip
'''在tensorflow中只需要写出正向过程'''
path="horseColicTraining.txt"
path1="horseColicTest.txt"
eps = 1e-10
traindata,trainlabel=loadfile(path)
testdata,testlabel=loadfile(path1)
'''训练数据'''
traindata=tf.convert_to_tensor(traindata.T)#21*299
trainlabel=tf.convert_to_tensor(trainlabel.T)#1*299
'''测试数据'''
testdata=tf.convert_to_tensor(testdata.T)#21*299
testlabel=tf.convert_to_tensor(testlabel.T)#1*299
'''参数初始化'''
w=tf.Variable(tf.zeros([1,21],tf.float64))#1,21
theta=tf.Variable(tf.zeros([1,1],tf.float64))
'''计算正向loss'''
y_clip=forwardcal(w,traindata,theta)#为了避免log函数出现真数为0的情况
loss = tf.reduce_mean(- (trainlabel * tf.log(y_clip) + (1 - trainlabel) * tf.log(1 - y_clip)))
train = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
'''计算精确率'''
def Ac(testdata,w,theta,testlabel):
    print(sess.run(w),sess.run(theta))
    y_predict=forwardcal(w,testdata,theta)
'''这段代码有点麻烦,因为更擅长numpy'''
    y_p_numpy=y_predict.eval()#(1,67)
    print(y_p_numpy.shape)
    y_temp=[]
    for i in y_p_numpy[0]:
        if i>=0.5:
            i=1
            y_temp.append(i)
        else:
            i=0
            y_temp.append(i)
    y_temp=np.array([y_temp])##记住在变换list to numpy 需要加上[]  ,就不会发生[67,]bug
    y_predict=tf.convert_to_tensor(y_temp)
    return y_predict
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print("begin trainning...")
    for i in range(100):
        sess.run(train)
    print("trained...")
    print(sess.run(loss),sess.run(w),sess.run(theta))
    y_predict=Ac(testdata,w,theta,testlabel)
    correct_prediction = tf.equal(tf.argmax(testlabel, 1), tf.argmax(y_predict, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print(sess.run(accuracy))

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

测试集

https://pan.baidu.com/s/1h1t0LLxZlESPPdfn1zxTXQ

训练集

https://pan.baidu.com/s/1IVO2opIQ0e66_AEchw_X4Q

  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值