본문 바로가기
머신러닝과 딥러닝/실습

TensorFlow로 Logistic Classification의 구현 및 당뇨병 예측 실습

by Bentist 2020. 2. 13.
import tensorflow as tf

# 선형 회귀를 위해서 텐서 플로우 1의 코드 실행을 위한 임포트
# 현재는 텐서 플로우 2로 선형 회귀를 훨씬 간단하게 구현할 수 있다.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

x_data = [[1,2],[2,3],[3,1],[4,3],[5,3],[6,2]]
y_data = [[0],[0],[0],[1],[1],[1]] # 0은 fail, 1은 pass

X = tf.placeholder(tf.float32, shape=[None, 2])
Y = tf.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.random_normal([2,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.sigmoid(tf.matmul(X,W) + b)
cost = -tf.reduce_mean(Y*tf.log(hypothesis) + (1-Y)*tf.log(1 - hypothesis))

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

# Accuracy computation
# True if hypothesis > 0,5 else false
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    for step in range(10001):
        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val)
            
    # Accuracy report
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                      feed_dict={X: x_data, Y: y_data})
    
    print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

> Hypothesis: [[0.03380238]   --> cast한 이후, 0

                [0.16279992]      --> cast한 이후, 0

                [0.31919888]      --> cast한 이후, 0

                [0.7749096 ]      --> cast한 이후, 1

                [0.93542284]      --> cast한 이후, 1

                [0.9787854 ]]      --> cast한 이후, 1

> Correct (Y): [[0.] [0.] [0.] [1.] [1.] [1.]]

> Accuracy: 1.0

 

tf.cast(hypothesis > 0.5, dtype = tf.float32)를 하면, cast를 통해 예측값으로 1.0 또는 0.0이 나온다.
tf.cast(tf.equal(predicted, Y), dtype=tf.float32)는 우리가 예측한 값실제 Y가 같은지 비교하는 코드이며,
두 값이 같으면 1.0, 다르면 0.0이 나올 것이다. 여기에 tf.reduce_mean()으로 전체 평균을 낼 수 있고, 
내가 10번 예측했는데 5번 맞았으면 50%의 accuracy가 나오게 된다.
출력되는 걸 보면, step에 따라 cost가 계속 작아지는 것을 볼 수 있다.

Hypothesis는 0.5를 기준으로 실제 Y값과 비교해봤을 때, 동일한 출력 결과를 확인할 수 있다.

 

당뇨병 예측해보기

import numpy as np

# 선형 회귀를 위해서 텐서 플로우 1의 코드 실행을 위한 임포트
# 현재는 텐서 플로우 2로 선형 회귀를 훨씬 간단하게 구현할 수 있다.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()


xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)
xy.shape 
# (759, 9)

# 8개의 당뇨병 요인 feature
x_data = xy[:, 0:-1]
# 0이면 당뇨병이 없고, 1이면 당뇨병 있음
y_data = xy[:, [-1]]

X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.random_normal([8,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.sigmoid(tf.matmul(X,W) + b)
cost = -tf.reduce_mean(Y*tf.log(hypothesis) + (1-Y)*tf.log(1 - hypothesis))

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

# Accuracy computation
# True if hypothesis > 0,5 else false
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

# Launch graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    for step in range(10001):
        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val)
            
    # Accuracy report
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                      feed_dict={X: x_data, Y: y_data})
    
    print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

댓글