You have opened the file in binary mode and are trying to write text to the file. That's why it is giving error.
Change "wb" to "wt" if you want to write text data.
Here is the modified code:
# Write the header
ofile = gzip.open(outfile, "wt")
hdr = "mv" + "\t" + "predicted_prob" + "\t" + "true_label" + "\n"
ofile.write(hdr)