mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-18 13:55:14 +02:00
Bug fix in the confusion matrix
This commit is contained in:
parent
73bed5be82
commit
908b11c701
1 changed files with 22 additions and 21 deletions
|
|
@ -2,29 +2,30 @@ import numpy as np
|
||||||
|
|
||||||
def conf_matrix(p,labels,names=['1','0'],threshold=.5,show=True):
|
def conf_matrix(p,labels,names=['1','0'],threshold=.5,show=True):
|
||||||
"""
|
"""
|
||||||
Returns true and false positives in a binary classification problem
|
Returns error rate and true/false positives in a binary classification problem
|
||||||
- Column names: true class of the examples
|
- Actual classes are displayed by column.
|
||||||
- Row names: classification assigned by the model
|
- Predicted classes are displayed by row.
|
||||||
|
|
||||||
p: probabilities estimated for observation of belonging to class '1'
|
:param p: array of class '1' probabilities.
|
||||||
labels: observations' class
|
:param labels: array of actual classes.
|
||||||
names: classes' names
|
:param names: list of class names, defaults to ['1','0'].
|
||||||
threshold: probability value at which the model allocate an element to each class
|
:param threshold: probability value used to decide the class.
|
||||||
show: whether the matrix should be shown or not
|
:param show: whether the matrix should be shown or not
|
||||||
|
:type show: False|True
|
||||||
"""
|
"""
|
||||||
p = p.flatten()
|
assert p.size == labels.size, "Arrays p and labels have different dimensions."
|
||||||
labels = labels.flatten()
|
decision = np.ones((labels.size,1))
|
||||||
N = p.size
|
decision[p<threshold] = 0
|
||||||
C = np.ones(N)
|
diff = decision - labels
|
||||||
C[p<threshold] = 0
|
false_0 = diff[diff == -1].size
|
||||||
True_1 = float((labels - C)[labels-C==0].shape[0] )
|
false_1 = diff[diff == 1].size
|
||||||
False_1 = float((labels - C)[labels-C==-2].shape[0] )
|
true_1 = np.sum(decision[diff ==0])
|
||||||
True_0 = float((labels - C)[labels-C==-1].shape[0] )
|
true_0 = labels.size - true_1 - false_0 - false_1
|
||||||
False_0 = float((labels - C)[labels-C==1].shape[0] )
|
error = (false_1 + false_0)/np.float(labels.size)
|
||||||
if show:
|
if show:
|
||||||
print (True_1 + True_0 + 0.)/N * 100,'% instances correctly classified'
|
print 100. - error * 100,'% instances correctly classified'
|
||||||
print '%-10s| %-10s| %-10s| ' % ('',names[0],names[1])
|
print '%-10s| %-10s| %-10s| ' % ('',names[0],names[1])
|
||||||
print '----------|------------|------------|'
|
print '----------|------------|------------|'
|
||||||
print '%-10s| %-10s| %-10s| ' % (names[0],True_1,False_0)
|
print '%-10s| %-10s| %-10s| ' % (names[0],true_1,false_0)
|
||||||
print '%-10s| %-10s| %-10s| ' % (names[1],False_1,True_0)
|
print '%-10s| %-10s| %-10s| ' % (names[1],false_1,true_0)
|
||||||
return True_1, False_1, True_0, False_0
|
return error,true_1, false_1, true_0, false_0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue