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



我正在分类一组存储在csv文件中的图像元组。我在终端上得到的混淆矩阵是正确的。但是,当我将相同的conf. matrix写入文件时,它会产生非法字符(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()` -:nnThe training dataset has been divided into " + str(len(kf)) + " partsn"
    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
...

使用以下命令打印矩阵以正确归档:

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

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

print >> outfile, confusion_matrix(testing_labels, predicted)

它将stdout重定向到outfile

相关内容

  • 没有找到相关文章

最新更新