File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 6767crossed = cross_val_score (classifier , features , labels )
6868print ('Result with prescaling: {}' .format (crossed ))
6969
70+
71+ # Now, generate & print a cross-validated confusion matrix for the same result
72+ from sklearn .metrics import confusion_matrix
73+ names = list (set (labels ))
74+ labels = np .array ([names .index (ell ) for ell in labels ])
75+ preds = labels .copy ()
76+ preds [:] = - 1
77+ for train , test in kf :
78+ classifier .fit (features [train ], labels [train ])
79+ preds [test ] = classifier .predict (features [test ])
80+
81+ cmat = confusion_matrix (labels , preds )
82+ print ()
83+ print ('Confusion matrix: [rows represent true outcome, columns predicted outcome]' )
84+ print (cmat )
85+
86+ # The explicit float() conversion is necessary in Python 2
87+ # (Otherwise, result is rounded to 0)
88+ acc = cmat .trace ()/ float (cmat .sum ())
89+ print ('Accuracy: {0:.1%}' .format (acc ))
90+
You can’t perform that action at this time.
0 commit comments