Implementing a confusion matrix to your logreg code in Theano

This implementation is to be done in this code – link

Install scikit-learn library and then import-

from sklearn.metrics import confusion_matrix 

Define this theano function to obtain predicted labels from your logistic regression model.

evaluate_model = theano.function(
         x: test_set_x[index * batch_size: (index + 1) * batch_size]

Add the following code next to the section where you are displaying the accuracy.  labels_y is a vector of your actual labels, make sure its orientation matches to that of predicted labels.

 test_pred_y = numpy.concatenate([evaluate_model(i) for i in xrange(n_test_batches)])
labels= numpy.transpose(test_set['train_labels'])[0]
 for i in xrange(n_test_batches):
          labels_y= numpy.concatenate((labels_y, labels[i * batch_size: (i + 1) *                     batch_size]))
 print confusion_matrix(labels_y, test_pred_y)

Hola!! you now have confusion matrix integrated to your code.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s