티스토리 뷰

[당뇨병 환자 예측]


코드는 이전 Logistic Regression 게시글의 코드를 거의 그대로 사용했습니다.

http://twoearth.tistory.com/25?category=795201


당뇨병에 걸린 환자들(1)과 걸리지 않은 환자들(0)의 데이터가 있습니다.

https://github.com/hunkim/DeepLearningZeroToAll/blob/master/data-03-diabetes.csv



이 데이터를 보면 instance는 800개가 조금 안되고, 알 수 없는 데이터들이 8열까지 있고 맨 마지막 9열에는 당뇨병에 걸린지에 대한 여부가 0과 1로 표시되어 있습니다.


변수가 어떤것을 의미하는지는 굉장히 중요합니다. 

그 이유는 데이터가 수치가 아닌 indexing일 수도 있기 때문입니다. 예를들어 당뇨병에 걸린 환자들의 지역을 단순히 숫자로 indexing했다고 해봅시다.


int 서울, 인천, 광주, 대구, 부산 = 1, 2, 3, 4, 5


이런식의 indexing은 연산이 가능하지만 실제 의미로 생각하면 연산이 불가능하고 의미도 없죠.

(서울x4 = 대구????)

하지만 이 데이터는 기계학습을 위한 좋은 데이터로 제공되기 때문에 믿고 사용해보죠!


import tensorflow as tf
import numpy as np

xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype = np.float32)
x_data = xy[:,0:-1]
y_data = xy[:,[-1]]

X = tf.placeholder(tf.float32, shape=[None, 8]) ## 행은 n개, 열은 2개의 Shape을 가지고 있다.
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) ## matmul = 행렬 내적, sigmoid = 시그모이드 함수 호출

## cost function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1-Y) * tf.log(1- hypothesis))

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

## True(1), False(0) 설정
predicted = tf.cast(hypothesis > 0.5, dtype = tf.float32) ## 0.5이상일 때 True(1), else False(0)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted,Y), dtype = tf.float32))
## hypothesis와 Y가 같을 때를 뽑아서 평균을 내면 정확도를 확인할 수 있다.

## 초기화
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(100001):
cost_val, _ = sess.run([cost, train], feed_dict = {X:x_data, Y:y_data})
if step % 200 == 0:
print(step, cost_val)

h, c , a = sess.run([hypothesis, Y, accuracy], feed_dict = {X:x_data, Y:y_data})
print("hypothesis:",h,"Y:", c,"accuracy:", a)



기계학습은 이렇게 상관관계가 있는 데이터간의 상호작용을 정확하게 알지 못해도 기계학습을 통해 의미있는 예측을 할 수 있습니다.

(8개 종류의 parameter로 무려 76%의 정확도를 보여주고 있습니다!)




'Data Science > Machine Learning' 카테고리의 다른 글

Softmax Regression을 이용해서 동물 맞추기  (0) 2018.11.15
Softmax Regression  (0) 2018.11.14
Logistic Regression  (0) 2018.11.09
Multi-Variable Linear Regression  (0) 2018.11.07
Linear Regression  (0) 2018.11.03
댓글
최근에 올라온 글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
공지사항
최근에 달린 댓글