Function iaconfmtx

Description

Compute confusion matrix to evaluate the accuracy of a classification

Synopse

Create confusion matrix.

  • confmxt = iaconfmtx(y_true, y_pred)
    • confmxt: Incidence graph.
    • y_true: Data True/real classification.
    • y_pred: Classification Result.

Function_Code

 1 from numpy import *
 2 
 3 def iaconfmtx(y_true, y_pred):
 4 
 5     if y_true.shape == y_pred.shape:
 6         prim_array = unique(y_true) # array with primary elements
 7         classes_number =  prim_array.shape[0] # number of classes
 8         confmtx = zeros((classes_number,classes_number)) # confusion matrix creation
 9         aux = arange((y_pred.shape[0])) # auxiliary matrix with values equal do index
10 
11         for i in range(y_pred.shape[0]):
12             # add 1 to the correct element
13             confmtx[aux[prim_array == y_true[i]],aux[prim_array == y_pred[i]]] = confmtx[aux[prim_array == y_true[i]],aux[prim_array == y_pred[i]]] +1
14 
15         return confmtx
16     else:
17         print 'Insert Correct Parameters' # wrong parameters

Example

1 from numpy import *
2 from iaOPF import iaconfmtx
3 
4 y_true = array([0,2,4,7])
5 y_pred = array([0,2,2,2])
6 print iaconfmtx(y_true,y_pred)
/usr/local/lib/python2.6/dist-packages/scikits/__init__.py:1: UserWarning: Module dateutil was already imported from /usr/local/lib/python2.6/dist-packages/matplotlib-1.1.0-py2.6-linux-x86_64.egg/dateutil/__init__.pyc, but /usr/lib/pymodules/python2.6 is being added to sys.path
  __import__('pkg_resources').declare_namespace(__name__)
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  1.  0.  0.]]