import json as json
import numpy as np
import tensorflow as tf

with open('data.json') as data_file:
    data = json.load(data_file)

x = tf.placeholder(tf.float32, [None, 3])

W = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([1]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder("float", [None, 1])

cross_entropy = -tf.reduce_sum(y_ * tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

userInfoData = np.empty((0, 3), dtype=np.float32)
resultData = np.empty((0, 1), dtype=np.float32)

for item in data:
    age = np.float32(item["age"])
    male = np.float32(item["male"])
    single = np.float32(item["single"])
    frequency = np.float32(item["frequency"])

    inputArray = np.array([[age, male, single]])
    answerArray = np.array([[frequency]])

    userInfoData = np.append(userInfoData, inputArray, axis=0)
    resultData = np.append(resultData, answerArray, axis=0)

print userInfoData.shape
print resultData.shape

sess.run(train_step, feed_dict={x: userInfoData, y_: resultData})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# TODO: Result Test Wrong!
print sess.run(accuracy, feed_dict={x: np.array([[23, 0, 2]]), y_: np.array([[-1]])})
