<jupyter_start><jupyter_text>Test 1<NAME><jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import scale
from sklearn.metrics import accuracy_score
# read training data
df_train = pd.read_csv('./Data/HeartDisease_train.csv')
print(df_train.shape)
df_train.head()
# read test data
df_test = pd.read_csv('./Data/HeartDisease_test.csv')
print(df_test.shape)
df_test.head()
# one-hot-encode appropriate columns
col_to_one_hot_encode  = ['sex','fbs','num']
for col in col_to_one_hot_encode:
    df_train[col] = df_train[col].astype('category')
    df_test[col] = df_test[col].astype('category')
df_train = pd.get_dummies(df_train)
df_test  = pd.get_dummies(df_test)
# scale appropriate columns
col_to_scale = ['age','trestbps','chol']
df_train[col_to_scale] = scale(df_train[col_to_scale])
df_test[col_to_scale]  = scale(df_test[col_to_scale])
df_train.describe().T
df_test.describe().T
# define input and output values
inputs_train  = df_train.drop(['num_0.0','num_1.0'],axis=1).values
inputs_test   = df_test.drop(['num_0.0','num_1.0'],axis=1).values
outputs_train = df_train[['num_0.0','num_1.0']].values
outputs_test  = df_test[['num_0.0','num_1.0']].values

print('training input shape:',inputs_train.shape)
print('testing input shape:',inputs_test.shape)
print('training output shape:',outputs_train.shape)
print('testing output shape:',outputs_test.shape)
# define TensorFlow computation graph

x = tf.constant(inputs_train, dtype='float32', shape=[200, 7])
y = tf.constant(outputs_train, dtype='float32', shape=[200, 2])
x_test = tf.constant(inputs_test, dtype='float32', shape=[97, 7])

W = tf.Variable(tf.truncated_normal([7, 2], stddev=0.01))
b = tf.Variable(tf.truncated_normal([1, 2], stddev=0.01))

logits = tf.matmul(x, W) + b
CE = tf.reduce_mean(tf.reduce_sum(-y * tf.log(tf.nn.softmax(logits)), reduction_indices=[1]))

optimizer = tf.train.AdamOptimizer().minimize(CE)

y_train_pred = tf.nn.softmax(tf.matmul(x, W) + b)
y_test_pred = tf.nn.softmax(tf.matmul(x_test, W) + b)

init = tf.initialize_all_variables()
# create a graph session and initialize
sess = tf.Session()
sess.run(init)
# minimize CE
MAXSTEPS = 1000
for step in range(MAXSTEPS+1):
    (_,ce,outputs_train_pred,outputs_test_pred) = sess.run([optimizer,CE,y_train_pred,y_test_pred])
    if (step % 100) == 0:
        acc_train = accuracy_score(outputs_train.argmax(axis=1),outputs_train_pred.argmax(axis=1))
        acc_test  = accuracy_score(outputs_test.argmax(axis=1),outputs_test_pred.argmax(axis=1))
        print('step = %-5d  cross entropy = %f  train accuracy = %f  test accuracy = %f' % (step,ce,acc_train,acc_test))
print('done!')<jupyter_output>step = 0      cross entropy = 0.696132  train accuracy = 0.410000  test accuracy = 0.381443
step = 100    cross entropy = 0.636231  train accuracy = 0.680000  test accuracy = 0.608247
step = 200    cross entropy = 0.607548  train accuracy = 0.695000  test accuracy = 0.577320
step = 300    cross entropy = 0.590887  train accuracy = 0.700000  test accuracy = 0.577320
step = 400    cross entropy = 0.581196  train accuracy = 0.715000  test accuracy = 0.577320
step = 500    cross entropy = 0.575605  train accuracy = 0.705000  test accuracy = 0.577320
step = 600    cross entropy = 0.572412  train accuracy = 0.705000  test accuracy = 0.587629
step = 700    cross entropy = 0.570620  train accuracy = 0.705000  test accuracy = 0.587629
step = 800    cross entropy = 0.569640  train accuracy = 0.705000  test accuracy = 0.597938
step = 900    cross entropy = 0.569122  train accuracy = 0.705000  test accuracy = 0.597938
step = 1000   cross entropy = 0.568860  train accuracy = 0.705000  test accuracy[...]
{'fraction_non_alphanumeric': 0.13434452871072589, 'fraction_numerical': 0.08586132177681473, 'mean_word_length': 5.024469820554649, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '578728', 'license': 'MIT', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1325, 'n_words': 294, 'path': 'Test 1.ipynb', 'repo_name': 'derrowap/MA490-Deep-Learning', 'size': '3706'}