Skip to content

Instantly share code, notes, and snippets.

@ligurio

ligurio/test.py

Created Feb 13, 2017
Embed
What would you like to do?
#!/usr/bin/env python
# ./test.py --test-file test_labels.csv --bayes-file NaiveBayes_pred.csv --log-file LogisticRegression_pred.csv
#
import argparse
import sys
labels = []
bayes = []
log = []
def evaluate(class_results, correct_answers):
#TP = 0
#TN = 0
#FP = 0
#FN = 0
#for i in range(0, len(class_results)):
# if (int(class_results[i]) == 1 and int(correct_answers[i]) == 1):
# TP = TP + 1
# elif (int(class_results[i]) == 1 and int(correct_answers[i]) == 0):
# FP = FP + 1
# elif (int(class_results[i]) == 0 and int(correct_answers[i]) == 1):
# FN = FN + 1
# elif (int(class_results[i]) == 0 and int(correct_answers[i]) == 0):
# TN = TN + 1
#return float(TP + TN)/float(TP + FP + FN + TN)
num = 0
for i in range(0, len(class_results)):
diff = float(class_results[i]) - float(correct_answers[i])
err = pow(diff, 2)
num = num + err
return float(num)/2
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--test-file', dest="test_file")
parser.add_argument('--bayes-file', dest="bayes_file")
parser.add_argument('--log-file', dest="log_file")
args = parser.parse_args()
if len(sys.argv) < 3:
parser.print_help()
sys.exit(0)
# read data from files
with open(args.test_file) as f:
numbers = f.readlines()
labels = [num.strip() for num in numbers]
with open(args.bayes_file) as f:
numbers = f.readlines()
bayes = [num.strip() for num in numbers]
with open(args.log_file) as f:
numbers = f.readlines()
log = [num.strip() for num in numbers]
# make sure the lengths of our lists are the same
assert len(log) == len(bayes) == len(labels)
# round values in the lists
#for i in range(0, len(bayes)):
# bayes[i] = round(float(bayes[i]))
# log[i] = round(float(log[i]))
print "Naive Bayes accuracy:", evaluate(bayes, labels)
print "Logistic Regression accuracy:", evaluate(log, labels)
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment