将混淆矩阵打印到文件会产生非法字符

Printing confusion matrix to file produces illegal characters

我正在对存储在 csv 文件中的一组图像进行分类。 我在终端显示上得到的混淆矩阵是正确的。但是当我写同样的 conf 时。矩阵到文件,它会产生非法字符(32 位十六进制)。 这是代码-

from sklearn.metrics import confusion_matrix
import numpy as np 
import os
import csv
from sklearn import svm
from sklearn import cross_validation
from sklearn import linear_model
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
from sklearn import metrics
import cPickle

def prec(num):
    return "%0.5f"%num

outfile = open("output/linear_svm_output.txt","a")

for dim in [20,30,40]:
    images=[]
    labels=[]
    name = str(dim)+"x"+str(dim)+".csv"
    with open(name,'r') as file:
        reader = csv.reader(file,delimiter=',')
        for line in file:
            labels.append(line[0])
            line=line[2:] # Remove the label
            image=[int(pixel) for pixel in line.split(',')]
            images.append(np.array(image))

    clf = svm.LinearSVC()

    print clf
    kf = cross_validation.KFold(len(images),n_folds=10,indices=True, shuffle=True, random_state=4)
    print "\nDividing dataset using `Kfold()` -:\n\nThe training dataset has been divided into " + str(len(kf)) + " parts\n"
    for train, test in kf:
        training_images=[]
        training_labels=[]
        for i in train:
            training_images.append(images[i])
            training_labels.append(labels[i])
        testing_images=[]
        testing_labels=[]
        for i in test:
            testing_images.append(images[i])
            testing_labels.append(labels[i])

        clf.fit(training_images,training_labels)
        predicted = clf.predict(testing_images)
        print prec(clf.score(testing_images, testing_labels))
        outfile.write(prec(clf.score(testing_images, testing_labels)))
        outfile.write(str(clf)) 
        outfile.write(confusion_matrix(testing_labels, predicted))
        print confusion_matrix(testing_labels, predicted)
#       outfile.write(metrics.classification_report(testing_labels, predicted))

    print "\nDividing dataset using `train_test_split()` -:\n"
    training_images, testing_images, training_labels, testing_labels = cross_validation.train_test_split(images,labels, test_size=0.2, random_state=0)
    clf = clf.fit(training_images,training_labels)
    score = clf.score(testing_images,testing_labels)
    predicted = clf.predict(testing_images)
    print prec(score)
    outfile.write(str(clf)) 
    outfile.write(confusion_matrix(testing_labels, predicted))
    print confusion_matrix(testing_labels, predicted)
#   outfile.write(metrics.classification_report(testing_labels, predicted))

在文件中输出-

302e 3939 3338 374c 696e 6561 7253 5643
2843 3d31 2e30 2c20 636c 6173 735f 7765
...

因为outfile.write(confusion_matrix(testing_labels, predicted))会以二进制格式写出矩阵。如果你想把它写成人类可读的文本,如果你使用 python 2.x

试试这个
print >> outfile, confusion_matrix(testing_labels, predicted)

它只是将 stdout 重定向到 outfile

使用以下命令将矩阵正确打印到文件:

with open(filename, 'w') as f:
            f.write(np.array2string(confusion_matrix(y_test, pred), separator=', '))