728x90
728x170

data-04-zoo.csv

import tensorflow as tf
import numpy as np

# 분류하고자하는 동물의 특징들과 분류 결과가 있는 학습데이터를 읽어옵니다.
xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)

# 처름부터 마지막 컬럼 전까지가 특징들입니다.
x_data = xy[:, 0:-1]

# 마지막 컬럼이 분류된 결과 입니다. (0~6 로 구분)
y_data = xy[:, [-1]]

# x, y 데이터 구성을 출력합니다.
print(x_data.shape, y_data.shape)

# 0~6 까지 7까지 분류를 합니다.
nb_classes = 7 

# n 개의 데이터들이 16 가지 특징을 가집니다.
X = tf.placeholder(tf.float32, [None, 16])
# n 개의 데이터가 1개의 결과를 나타냅니다.
Y = tf.placeholder(tf.int32, [None, 1])  # 0 ~ 6
# One Hot 을 이용해 7개의 분류도 나누되 0,1 로 구분지을수 있도록 합니다. [[0],[3]] => [[[1000000]],[[0001000]]]
Y_one_hot = tf.one_hot(Y, nb_classes)  # one hot
print("one_hot", Y_one_hot)
# 차원이 하나 더 생겼으므로 차원을 제거합니다. [[[1000000]][[0001000]]] => [[1000000],[0001000]]
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
print("reshape", Y_one_hot)

# 입력은 16개 출력은 7가지로
W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')
# 출력은 7가지
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')

# cost 를 계산한다.
# logits 는 결과 데이터 이며
logits = tf.matmul(X, W) + b
# 결과데이터를 softmax 를 이용하여 합계가 1 이되도록 확률로 계산하도록 가설을 세운다.

hypothesis = tf.nn.softmax(logits)

# cost 가 최소화되도록 한다.
# (cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) 를 함수화 함)
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot) 
cost = tf.reduce_mean(cost_i)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

# 가설을 이용한 결과 값(prediction)과 onehot 을 이용한 결과 값이 같은지 비교하고 얼마나 정확한지 (accuracy)를 나타냅니다.
prediction = tf.argmax(hypothesis, 1)
correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 학습합니다.
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for step in range(2000):
        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
        if step % 100 == 0:
            loss, acc = sess.run([cost, accuracy], feed_dict={X: x_data, Y: y_data})
            print("Step: {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format(step, loss, acc))

    # 학습한 결과로 엑셀데이터를 실제로 분류해봅니다.
    pred = sess.run(prediction, feed_dict={X: x_data})
    # flatten => [[0],[1]] => [0,1] ,zip 은 묶어서 처리하는것으로 아래 코드는 zip 으로 묶어 앞에껀 p 로 뒤에껀 y 로 처리한다는 의미.
    for p, y in zip(pred, y_data.flatten()):
        print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y))) # 결과(TRUE/FALSE), 예측한결과값, 실제값
        

아래 결과를 보면 알겠지만 accuracy 가 100% 입니다.
이를 보면 학습하는 데이터가 좋을수록 분류를 잘한다는걸 알 수 있습니다.

결과

728x90
그리드형
Posted by kjun
,