wordsforthewise
wordsforthewise

Reputation: 15847

How to use pandas_ml to make a confusion matrix with 2 classes?

I am trying to make a confusion matrix with plotting, pandas_ml seems to have a function but it doesn't work with 2 classes. Is there some secret option to get it working?

from pandas_ml import ConfusionMatrix
ytrue = ['ham', 'ham', 'spam']
ypred = ['ham', 'spam', 'spam']
cm = ConfusionMatrix(ytrue, ypred)
cm

results in

Predicted  False  True  __all__
Actual                         
False          0     0        0
True           0     0        0
__all__        0     0        0

This:

from pandas_ml import ConfusionMatrix
ytrue = ['ham', 'ham', 'spam', 'third']
ypred = ['ham', 'spam', 'spam', 'third']
cm = ConfusionMatrix(ytrue, ypred)
cm

results in

Predicted  ham  spam  third  __all__
Actual                              
ham          1     1      0        2
spam         0     1      0        1
third        0     0      1        1
__all__      1     2      1        4

Upvotes: 1

Views: 2309

Answers (3)

wordsforthewise
wordsforthewise

Reputation: 15847

The way to solve this is to create two named pandas Series, and use pandas.crosstab(). Don't even use pandas_ml:

import pandas as pd

ytrue = pd.Series(['ham', 'ham', 'spam'], name='actual')
ypred = pd.Series(['ham', 'spam', 'spam'], name='predictions')
pd.crosstab(ytrue, ypred, margins=True)

The output will be

predictions  ham  spam  All
actual                     
ham            1     1    2
spam           0     1    1
All            1     2    3

Upvotes: 1

mendo
mendo

Reputation: 86

Maybe it is too late but I had the same problem. It seems that when you have 2 classes ConfusionMatrix from pandas_ml requires for your inputs to be boolean. Just convert 'spam'/'ham' to True/False and it should work.

from pandas_ml import ConfusionMatrix
ytrue = np.array(['ham', 'ham', 'spam'])
ytrue = np.array(['ham', 'ham', 'spam'])
ypred = np.array(['ham', 'spam', 'spam'])
cm = ConfusionMatrix(np.where(ytrue == 'spam', True, False), np.where(ypred == 'spam', True, False))
cm

Upvotes: 2

Chase Calkins
Chase Calkins

Reputation: 103

No, when I run it in my python3.6 through spyder3 I get this,

from pandas_ml import ConfusionMatrix 
ytrue = ['ham', 'ham', 'spam']
ypred = ['ham', 'spam', 'spam']
cm = ConfusionMatrix(ytrue, ypred)
cm

Out[1]: 
Predicted  ham  spam  __all__
Actual                       
ham          1     1        2
spam         0     1        1
__all__      1     2        3

IN[2]: cm.print_stats()
OUT[2]:
population: 3
P: 1
N: 2
PositiveTest: 2
NegativeTest: 1
TP: 1
TN: 1
FP: 1
FN: 0
TPR: 1.0
TNR: 0.5
PPV: 0.5
NPV: 1.0
FPR: 0.5
FDR: 0.5
FNR: 0.0
ACC: 0.666666666667
F1_score: 0.666666666667
MCC: 0.5
informedness: 0.5
markedness: 0.5
prevalence: 0.333333333333
LRP: 2.0
LRN: 0.0
DOR: inf
FOR: 0.0

cm.TP
Out[3]: 1

cm.TN
Out[4]: 1

cm.FP
Out[5]: 1

cm.FN
Out[6]: 0

Upvotes: -1

Related Questions