吴恩达机器学习作业TensorFlow实现(ex2,逻辑回归)

1、逻辑回归

tips:

  • 源数据需要进行归一化(我这里直接/100,因为原始数据范围为0-100)
  • 更换了优化方法,需要一定的训练次数,不然一定几率出现欠拟合
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 10 20:04:03 2019

@author: 无限未来
"""

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


#path = "G:\DeepLearning\吴恩达作业\ex2data1.txt"
#data = pd.read_csv(path, header=None, names=['ex1score', 'ex2score','judge'])
# 
#X = np.column_stack((data['ex1score'].values,data['ex2score'].values))
#Y = data['judge'].values
#Y.dtype='float'


data=np.loadtxt('G:\DeepLearning\吴恩达作业\ex2data1.txt',delimiter=',')       #载入数据集           
X=data[:,0:2]/100                                       #x取前两列数据,即学生两门课程成绩
Y=data[:,2]                                         #y取第三列数据
Y=Y.reshape([100,1])                                #读进来y的shape是[100,?]      


ok = np.where(Y==1)
no = np.where(Y==0)
#on = ['r' if l==0 else 'b' for l in train_Y[:]]

plt.figure(1)
#显示模拟数据点
plt.rcParams['font.sans-serif']=['SimHei'] 
#plt.rcParams['axes.unicode_minus']=False

#plt.scatter(train_X[:,0], train_X[:,1], c=on)
plt.scatter(X[ok,0], X[ok,1], c='b', marker='+',label='合格')
plt.scatter(X[no,0], X[no,1], c='r', marker='o',label='不合格')
plt.legend(loc='upper right')

plt.rcParams['figure.dpi'] = 100 #分辨率
plt.rcParams['savefig.dpi'] = 100 #图片像素

#plt.savefig('test.png', dpi=100)
plt.show(1)
plt.close(1) 
input_dim = 2 
lab_dim = 1

# tf Graph Input
input_features = tf.placeholder(tf.float32, [None, input_dim])
input_lables = tf.placeholder(tf.float32, [None, lab_dim])
# Set model weights
W = tf.Variable(tf.random_normal([input_dim,lab_dim]), name="weight")
b = tf.Variable(tf.zeros([lab_dim]), name="bias")

output =tf.nn.sigmoid( tf.matmul(input_features, W) + b)
cross_entropy = -(input_lables * tf.log(output) + (1 - input_lables) * tf.log(1 - output))
loss = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(0.1) #尽量用这个--收敛快,会动态调节梯度
train = optimizer.minimize(loss)  # let the optimizer train

maxEpochs = 200

# 启动session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(maxEpochs):
        x1=X
        y1=Y
        _,lossval= sess.run([train,loss], feed_dict={input_features: x1, input_lables:y1})
        print ("Epoch:", '%04d' % (epoch+1), "cost=","{:.9f}".format(lossval))

    
#    x1w1+x2*w2+b=0
#    x2=-x1* w1/w2-b/w2
    x = np.linspace(0.3,1,200) 
    y=-x*(sess.run(W)[0]/sess.run(W)[1])-sess.run(b)/sess.run(W)[1]
    
    plt.figure(2)
    plt.scatter(X[ok,0], X[ok,1], c='b', marker='+',label='合格')
    plt.scatter(X[no,0], X[no,1], c='r', marker='o',label='不合格')
    plt.plot(x,y, label='Fitted line')
    plt.legend(loc='upper right')
    plt.show() 
    

2、正则化逻辑回归(准确率较低)

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 11 21:10:41 2019

@author: 无限未来
"""

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.colors import colorConverter, ListedColormap 

data=np.loadtxt('G:\DeepLearning\吴恩达作业\ex2data2.txt',delimiter=',')       #载入数据集           
X=data[:,0:2]                                      #x取前两列数据,即学生两门课程成绩
Y=data[:,2]                                         #y取第三列数据
Y=Y.reshape([118,1])                                #读进来y的shape是[118,?]      


ok = np.where(Y==1)
no = np.where(Y==0)

plt.figure(1)
plt.rcParams['axes.unicode_minus']=False
#显示模拟数据点
plt.rcParams['font.sans-serif']=['SimHei'] 
#plt.rcParams['axes.unicode_minus']=False

#plt.scatter(train_X[:,0], train_X[:,1], c=on)
plt.scatter(X[ok,0], X[ok,1], c='b', marker='+',label='合格')
plt.scatter(X[no,0], X[no,1], c='r', marker='o',label='不合格')
plt.legend(loc='upper right')

plt.rcParams['figure.dpi'] = 100 #分辨率
plt.rcParams['savefig.dpi'] = 100 #图片像素

#plt.savefig('test.png', dpi=100)
plt.show(1)
plt.close(1) 

input_dim = 27 
lab_dim = 1
reg = 1

#数据预处理,相当于有28个特征,特征量增加
degree = 6
out = np.ones([X[:,1].size,27])
endl=0
for i in range(1,degree+1):
    for j in range(i+1):
        out[:,endl] = (X[:,0]**(i-j))*(X[:,1]**j)
        endl=endl+1

# tf Graph Input  输入为(?,27)  输出为(?,1)
input_features = tf.placeholder(tf.float32, [None, input_dim])
input_lables = tf.placeholder(tf.float32, [None, lab_dim])

# Set model weights W为(27,1)  b为(1,1)
W = tf.Variable(tf.random_normal([input_dim,lab_dim]), name="weight")
b = tf.Variable(tf.ones([lab_dim]), name="bias")

y_pred = tf.add(tf.matmul(input_features, W) , b)
output = tf.nn.sigmoid(y_pred)

loss=tf.reduce_mean(tf.pow(y_pred-input_lables,2))

optimizer = tf.train.GradientDescentOptimizer(0.1) 
train = optimizer.minimize(loss)  # let the optimizer train

maxEpochs = 2000

# 启动session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(maxEpochs):

        _,lossval= sess.run([train,loss], feed_dict={input_features: out, input_lables:Y})
        print ("Epoch:", '%04d' % (epoch+1), "cost=","{:.9f}".format(lossval))
    print(sess.run(W))
    

    OUTo= np.ones([1,118])
    out = np.ones([1,27])
    xxx=sess.run(W)
    yyy=sess.run(b)
    for iii in range(118):
        
        out = np.ones([1,27])
        endl=0
        for i in range(1,degree+1):
            for j in range(i+1):
                out[0,endl] = (X[iii,0]**(i-j))*(X[iii,1]**j)
                endl=endl+1
        OUTo[0,iii]=np.dot(out,xxx)+yyy
    
    OUTo2= np.ones([1,118])
    sum=0
    for i in range(118):
        OUTo2[0,i]=round(abs(OUTo[0,i]))
    ac = OUTo2 - np.transpose(Y)
    for i in range(118):    
        if (ac[0,i] == 0):
            sum=sum+1
    accuracy = sum/118*100
   

 

你可能感兴趣的:(机器学习)