1

I got this code:

predicted_labels = clf.predict(X_test)

def print_cm(cm, labels, hide_zeroes=False, hide_diagonal=False, hide_threshold=None):
    """pretty print for confusion matrixes"""
    columnwidth = max([len(x) for x in labels]+[5]) # 5 is value length
    empty_cell = " " * columnwidth
    # Print header
    print ("    " + empty_cell,)
    for label in labels:
        print ("%{0}s".format(columnwidth) % label,)
    print
    # Print rows
    for i, label1 in enumerate(labels):
        print ("    %{0}s".format(columnwidth) % label1,)
        for j in range(len(labels)):
            cell = "%{0}.1f".format(columnwidth) % cm[i, j]
            if hide_zeroes:
                cell = cell if float(cm[i, j]) != 0 else empty_cell
            if hide_diagonal:
                cell = cell if i != j else empty_cell
            if hide_threshold:
                cell = cell if cm[i, j] > hide_threshold else empty_cell
            print (cell,)
        print

conf = confusion_matrix(y_test, predicted_labels)
# then print it in a pretty way
print_cm(conf, flowpatterns_labels)

When I print it, I got a vector, not a matrix. It works fine on python 2, but not on python 3. Any ideas?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 1
    I find it surprising that this *exact same* code would print a matrix in Python 2. You don't have any code that would print without a trailing newline, so every expression should print by itself (and wrapped in a singleton tuple, too!). I assume, then, that you originally did not have any parentheses, you saw the ["missing parentheses in call to print" error](https://stackoverflow.com/q/25445439/1340389), "fixed" it by adding parens, but it doesn't work because you didn't use the `end=` keyword argument, and didn't convert `print` to `print()`. – Kevin Apr 16 '18 at 22:26

0 Answers0