티스토리 뷰

첨자의 위치가 : 참조 데이터 인덱스(트레이닝 데이터)



첨자의 위치가 아래: 참조할 데이터의 개수(키, 몸무게, 성별...)




Min * Max 정규화 : (value - min(value)) / (max(value) - min(value)): 데이터값을 비교했을 경우, 차이가 많이 나는 경우가 발생한다. 그런 경우 해당 정규화를 사용하면 0 ~ 1사이의 실수값들을 얻을 수 있다.


주식 가격을 예측 해보자. 아래는 소스 코드 샘플

해당 url의 데이터를 사용했음.

데이터 사용: https://raw.githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-02-stock_daily.csv

import tensorflow as tf
import numpy as np

data = np.loadtxt("../resource/02_stock_daily.csv", dtype=np.float32, delimiter=",")

print(data)
print(data.shape)

x_train = data[:, 0:-1]
y_train = data[:, [-1]]


# How to Min-Max?
# (x - min(x)) / (max(x) - min(x))
def nomalize(input):
max = np.max(input, axis=0)
min = np.min(input, axis=0)
out = (input - min) / (max - min)
return out


testM = 10
m = len(x_train) - testM
print('m: ', m)

x_data = nomalize(x_train)
y_data = nomalize(y_train)

x_train = x_data[0:m, :]
y_train = y_data[0:m, :]

x_test = x_data[m:, :]
y_test = y_data[m:, :]

X = tf.placeholder(tf.float32, shape=[None, 4])
Y = tf.placeholder(tf.float32, shape=[None, 1])

w = tf.Variable(tf.random_normal([4, 1]))
b = tf.Variable(0.0)

hypothesis = tf.matmul(X, w) + b
loss = tf.square(Y - hypothesis)
loss = tf.reduce_mean(loss)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=5e-1)
train = optimizer.minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for iter in range(10000):
t_, w_, l, h = sess.run([train, w, loss, hypothesis], feed_dict={X: x_train, Y: y_train})
if iter % 1000 == 0:
print('iter:%d, loss:%f ' % (iter, l))

h = sess.run(hypothesis, feed_dict={X: x_test})
print('input', x_test)
print('Close Price Predict: ', h)
print('close Price Real: ', y_test)
print('why ', sess.run(w))

결과값:


해석: Predict의 값과 Real의 값이 차이가 없다. 왜냐하면 Cost가 엄청 낮기 때문에 그만큼 정확하다.

그리고 w값을 분석하면, 1번째와 4번째의 w값들에 대한 중요도 보다 2,3번쨰의 w값들의 중요도를 알 수 있다. (3 > 2 > 4 > 1)


해당 데이터를 10개는 테스트 케이스로 사용하기 위해, 위의 소스 중, m(testM)을 10으로 둬 트레이닝 값과 테스트 값을 분류 하였다.

댓글
공지사항
최근에 올라온 글
링크
«   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
글 보관함