2017-05-16 1 views
0

Ich habe 3 separate Klassifikatoren mit 10-facher Kreuzvalidierung. I-Ausgang eine Konfusionsmatrix (für jeden Lauf/fold) jedes Mal, als solche:Pandas: Wie auch immer, um verschiedene Verwirrungsmatrizen zu mitteln?

cm = pd.crosstab(pd.Series(y_pred), pd.Series(y_test), rownames=['Predicted'], colnames=['Actual'], margins=True) 

Meine Frage ist, gibt es eine Möglichkeit, eine durchschnittliche Konfusionsmatrix erstellen kann? Ich würde lieber Panda's als Sklearn's cm verwenden, wenn es nicht nötig ist.

Antwort

0

Sie können concatenate Cross-Tabulation Frames, groupby ihre Indexwerte und nehmen ihre means.

import numpy as np 
import pandas as pd 

# some random data frames 
y_pred = np.random.randint(0, 2, 10) 
y_test = np.random.randint(0, 2, 10) 
cm1 = pd.crosstab(pd.Series(y_pred), pd.Series(y_test), rownames=['Predicted'], colnames=['Actual'], margins=True) 
... 

print(cm1) 
Actual 0 1 All 
Predicted   
0  2 4 6 
1  1 3 4 
All  3 7 10 

print(cm2)  
Actual 0 1 All 
Predicted   
0  6 2 8 
1  1 1 2 
All  7 3 10 

pandas.concat() nimmt eine Sequenz als erstes Argument (Pandas Objekte verknüpft werden), so können Sie so viele verketten, wie Sie wollen, indem Sie eine Liste oder Tupel geben.

cm_concat = pd.concat((cm1, cm2)) 
cm_group = cm_concat.groupby(cm_concat.index) 

cm_group.mean() 

in Resultierende:

Actual 0 1 All 
Predicted   
0   4 3 7 
1   1 2 3 
All   5 5 10 
Verwandte Themen