we will build a logistic regression model of some (fake) graduate school admissions decisions. The data consists csv file of a GRE exam score in the range 0 to 800, a grade point average in the range 0 to 4.0 and the rank of the student's undergrad institution in the range 4 to 1 (top). The Admission Decision in binary.
There are six columns in the data. Ignore column 0. the GRE, GPA and school rank are in columns 1 through 3. Column 4 is an admission decision that is only based on the rank of the school: only students with a rank 1 school get in and others not. Column 5 contains a more interesting (but still silly) admission model where you get in if your GRE score is 800 or your rank is 1. in the example below we use the former model. It is easy for the system to learn it perfectly.
The csv file is called dat.csv and it is here https://1drv.ms/u/s!AkRG9Zk_IOUag5IX1xzlv7rP9T5FFg
import tensorflow as tf
import numpy as np
import csv
sess = tf.InteractiveSession()
train_datas = []
train_labels = []
test_datas = []
test_labels = []
i = 0
scale = np.array([[0.01, 1.0, 1.0]])
with open('your path to/dat.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
if i < 300:
train_datas.append([np.float32(x) for x in row[1:4]])
#print np.float(row[0])
train_labels.append([np.float32(row[4])])
else:
test_datas.append([np.float32(x) for x in row[1:4]])
test_labels.append([np.float(row[4])])
i +=1
test_data = np.array(test_datas, dtype=np.float32)
test_label = np.array(test_labels, dtype=np.float32)
train_data = np.array(train_datas, dtype=np.float32)
train_label = np.array(train_labels, dtype=np.float32)
test_data[0:20]
train_label[0:20]
first define the placeholders and variables
x = tf.placeholder(tf.float32, shape=(None,3))
y = tf.placeholder(tf.float32, shape =(None,1))
# Set model weights
W = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([1]))
training_epochs = 400000
batch_size = 100
display_step = 1000
pred = tf.sigmoid(tf.matmul(x, W) + b) # Softmax
cost = tf.sqrt(tf.reduce_sum((y - pred)**2/batch_size))
opt = tf.train.AdamOptimizer()
optimizer = opt.minimize(cost)
# Initializing the variables old version is initialize_all_variable. New version
# has global_variable_initializer.
#init = tf.global_variables_initializer()
init = tf.initialize_all_variables()
def get_batch2(batch_size, x, y):
indices = np.random.choice(299, batch_size)
return x[indices], y[indices]
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(len(train_data)/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_xs, batch_ys = get_batch2(batch_size, train_data, train_label)
# Fit training using batch data
_, c = sess.run([optimizer, cost], feed_dict={x:batch_xs,y:batch_ys})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if (epoch+1) % display_step == 0:
print "Epoch:", '%04d' % (epoch+1), "cost=", str(avg_cost)
print "Optimization Finished!"
define a function in python to compute sigmoid of an array.
def sigmoida(z):
res = np.zeros(len(z))
for i in range(len(z)):
y = 1.0/(1.0+np.exp(-z[i]))
if y > 0.5:
res[i] = 1
else:
res[i] = 0
return res
p = sigmoida(pred.eval({x:test_data}))
q = y.eval({y:test_label})
how accurate is this. In other words whaat fraction of the cases have p = q.
def accurate(p,q):
total = 0.0
numones = 0.
numzeros = 0.
for i in range(len(p)):
if p[i] == 0:
numzeros += 1
else:
numones += 1
if p[i] == q[i]:
total +=1
print "accuracy = "+str(total/len(p))
print 'ratio of 1s ='+str(numones/len(p))
print 'ratio of 0s ='+str(numzeros/len(p))
accurate(p,q)
def softmax(v):
k = len(v)
res = np.zeros(k)
tot = 0.0
for i in range(k):
res[i] = np.exp(v[i])
tot += res[i]
for i in range(k):
res[i] = res[i]/tot
return res
def sigmoid(x):
y = 1.0/(1.0+np.exp(-x))
if y > 0.5:
return 1
else:
return 0
if we wish, we may look at the trained W and b arrays.
w =W.eval()
w
lb =b.eval()
lb
for i in range(20):
print sigmoid(np.matmul(train_data[i], w)+lb)