Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing confusion matrix to file produces illegal characters

I am classifying a set of images stored as tuples in a csv file. The confusion matrix that I get on terminal display is correct. But when I write that same conf. matrix to a file, it produces illegal characters (32bit hex). Here's the code-

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))

Output in file-

302e 3939 3338 374c 696e 6561 7253 5643
2843 3d31 2e30 2c20 636c 6173 735f 7765
...
like image 237
goelakash Avatar asked Jan 09 '23 09:01

goelakash


1 Answers

Use the following to print the matrix to file properly:

with open(filename, 'w') as f:
            f.write(np.array2string(confusion_matrix(y_test, pred), separator=', '))
like image 132
Pranav Waila Avatar answered Jan 10 '23 23:01

Pranav Waila