This is probably overkill, but scikit-learn will do that pretty easily:
from sklearn.metrics import confusion_matrix
# Read the data
with open('file1', 'r') as infile:
true_values = [int(i) for i in infile]
with open('file2', 'r') as infile:
predictions = [int(i) for i in infile]
# Make confusion matrix
confusion = confusion_matrix(true_values, predictions)
print(confusion)
With output
[[0 2]
[0 2]]
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
Update:
To print with labels, you could either convert to a dataframe with pandas or something like this:
def print_confusion(confusion):
print(' ' + ' '.join([str(n) for n in range(confusion.shape[1])]))
for rownum in range(confusion.shape[0]):
print(str(rownum) + ' ' + ' '.join([str(n) for n in confusion[rownum]]))
which prints
0 1
0 0 2
1 0 2