Package: conf-mat
conf-matThis library was created to address the confusion in confusion matrices and classification reports generated by libraries like scikit-learn. It displays confusion matrices and their heatmaps in a more aesthetically pleasing, intuitive way. It also presents classification reports and accuracy rates in a manner that clarifies the calculation method and how those results were obtained, which can help alleviate confusion for beginners in machine learningInstallationYou can installconf-matvia pip:pipinstallconf-matUsageFor Binary Classificationfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,2,1000)y_pred=randint(0,2,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)print_conf_mat(y_true,y_pred,classes_names=[False,True])# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(y_true,y_pred,classes_names=[False,True])show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(y_true,y_pred,classes_names=[False,True])OutputConfusionMatrix:________________

╒═════════════════════╤═══════════════════════════╤═══════════════════════════╕
│Classes│PredictedPositive(PP)│PredictedNegative(PN)│
╞═════════════════════╪═══════════════════════════╪═══════════════════════════╡
│ActualPositive(P)│TruePositive(TP):273│FalseNegative(FN):244│
│││TypeIIError(Missed)│
├─────────────────────┼───────────────────────────┼───────────────────────────┤
│ActualNegative(N)│FalsePositive(FP):229│TrueNegative(TN):254│
││TypeIError(Wrong)││
╘═════════════════════╧═══════════════════════════╧═══════════════════════════╛

╒══════════╤══════════════════════════════════════════════════════╕
││Rate(Score)│
╞══════════╪══════════════════════════════════════════════════════╡
│Accuracy│CorrectTP+TN│
││_______:_________________OR1-Error=0.53│
│││
││TotalTP+FP+FN+TN│
├──────────┼──────────────────────────────────────────────────────┤
│Error│WrongFP+FN│
││_____:_________________OR1-Accuracy=0.47│
│││
││TotalTP+FP+FN+TN│
╘══════════╧══════════════════════════════════════════════════════╛


ClassificationReport:_____________________

╒══════════════════╤═════════════════╤════════════════════╤═════════════════════╤════════════════╕
││Precision(P)│Recall(R)│F1-Score(F)│Support(S)│
╞══════════════════╪═════════════════╪════════════════════╪═════════════════════╪════════════════╡
│Positive(True)│P1(PPV):│R1(Sensitivity):│F1:│S1:│
││││││
││TP│TP│2xP1xR1││
││_______=0.54│_______=0.53│___________=0.54│TP+FN=517│
││││││
││TP+FP│TP+FN│P1+R1││
├──────────────────┼─────────────────┼────────────────────┼─────────────────────┼────────────────┤
│Negative(False)│P0(NPV):│R0(Specificity):│F0:│S0:│
││││││
││TN│TN│2xP0xR0││
││_______=0.51│_______=0.53│___________=0.52│FP+TN=483│
││││││
││TN+FN│TN+FP│P0+R0││
├──────────────────┼─────────────────┼────────────────────┼─────────────────────┼────────────────┤
│MacroAvg│P1+P0│R1+R0│F1+F0│TS=1000│
││_______=0.53│_______=0.53│_______=0.53││
││││││
││2│2│2││
├──────────────────┼─────────────────┼────────────────────┼─────────────────────┼────────────────┤
│WeightedAvg│W1│W2│W3│TS=1000│
││__=0.53│__=0.53│__=0.53││
││││││
││TS│TS│TS││
╘══════════════════╧═════════════════╧════════════════════╧═════════════════════╧════════════════╛

PPV:PositivePredictiveValue

NPV:NegativePredictiveValueW1=(P1xS1)+(P0xS0)W2=(R1xS1)+(R0xS0)W3=(F1xS1)+(F0xS0)TS:TotalSupport=S1+S0

Note:AllRealNumbersAreRoundedWithTwoDigitsAfterTheCommaHTMLFileGeneratedSuccesfuly:)You Can Plot, Generate HTML Page For Confusion Matrix And Classification Report Directly From One Calculationfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,2,1000)y_pred=randint(0,2,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=print_conf_mat(y_true,y_pred,classes_names=[False,True])# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=[False,True])show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=[False,True])You Can Hide Detail Information For Confusion Matrix, Classification Report And Confusion Matrix Heatmapfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,2,1000)y_pred=randint(0,2,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=print_conf_mat(y_true,y_pred,classes_names=[False,True],detail=False)# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=[False,True],detail=False)show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=[False,True],detail=False)OutputConfusionMatrix:________________

╒═════════════════════╤═══════════════════════════╤═══════════════════════════╕
│Classes│PredictedPositive(PP)│PredictedNegative(PN)│
╞═════════════════════╪═══════════════════════════╪═══════════════════════════╡
│ActualPositive(P)│261│271│
├─────────────────────┼───────────────────────────┼───────────────────────────┤
│ActualNegative(N)│240│228│
╘═════════════════════╧═══════════════════════════╧═══════════════════════════╛

╒══════════╤════════════════╕
││Rate(Score)│
╞══════════╪════════════════╡
│Accuracy│0.49│
├──────────┼────────────────┤
│Error│0.51│
╘══════════╧════════════════╛


ClassificationReport:_____________________

╒══════════════════╤═════════════════╤══════════════╤════════════════╤═══════════════╕
││Precision(P)│Recall(R)│F1-Score(F)│Support(S)│
╞══════════════════╪═════════════════╪══════════════╪════════════════╪═══════════════╡
│Positive(True)│0.52│0.49│0.51│532│
├──────────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│Negative(False)│0.46│0.49│0.47│468│
├──────────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│MacroAvg│0.49│0.49│0.49│1000│
├──────────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│WeightedAvg│0.49│0.49│0.49│1000│
╘══════════════════╧═════════════════╧══════════════╧════════════════╧═══════════════╛HTMLFileGeneratedSuccesfuly:)You Can Plot And Generate HTML Page For Confusion Matrix Directly From One Calculation And Without Print Confusion Matrix And Classification Reportfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportcalc_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,2,1000)y_pred=randint(0,2,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=calc_conf_mat(y_true,y_pred)print(cm)# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=[False,True],detail=False)show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=[False,True],detail=False)Output[[236,253],[255,256]]HTMLFileGeneratedSuccesfuly:)For Multi Classificationfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,10,1000)y_pred=randint(0,10,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)print_conf_mat(y_true,y_pred,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(y_true,y_pred,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(y_true,y_pred,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])OutputConfusionMatrix:________________

╒═══════════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╤═══════╕
│Classes│C1│C2│C3│C4│C5│C6│C7│C8│C9│C10│
╞═══════════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╪═══════╡
│C1│5│17│12│11│12│13│6│13│10│7│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C2│10│6│10│11│13│11│13│8│11│11│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C3│6│6│12│8│10│5│11│8│7│11│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C4│14│13│10│8│9│5│12│14│9│7│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C5│12│9│15│5│8│9│6│8│10│10│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C6│12│16│15│9│10│8│10│11│11│12│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C7│12│9│12│14│12│6│13│13│7│14│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C8│6│11│7│10│10│12│8│10│8│11│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C9│15│7│8│6│8│6│5│4│14│12│
├───────────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼───────┤
│C10│10│11│11│13│8│11│12│12│6│15│
╘═══════════╧══════╧══════╧══════╧══════╧══════╧══════╧══════╧══════╧══════╧═══════╛

Yellow:NotNoneCorrectValues/TruePositive(TP)ORTrueNegative(TN)Red:NotNoneWrongValues/FalsePositive(FP)ORFalseNegative(FN)Green:NoneCorrectValues
Blue:NoneWrongValues

╒══════════╤════════════════════════════════════════════════════════════════╕
││Rate(Score)│
╞══════════╪════════════════════════════════════════════════════════════════╡
│Accuracy│CorrectSumOfYellowValues│
││_______:____________________________OR1-Error=0.1│
│││
││TotalSumOfYellowAndRedValues│
├──────────┼────────────────────────────────────────────────────────────────┤
│Error│WrongSumOfRedValues│
││_____:____________________________OR1-Accuracy=0.9│
│││
││TotalSumOfYellowAndRedValues│
╘══════════╧════════════════════════════════════════════════════════════════╛


ClassificationReport:_____________________

╒══════════════╤═════════════════╤══════════════╤════════════════╤═══════════════╕
││Precision(P)│Recall(R)│F1-Score(F)│Support(S)│
╞══════════════╪═════════════════╪══════════════╪════════════════╪═══════════════╡
│C1│0.05│0.05│0.05│106│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C2│0.06│0.06│0.06│104│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C3│0.11│0.14│0.12│84│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C4│0.08│0.08│0.08│101│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C5│0.08│0.09│0.08│92│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C6│0.09│0.07│0.08│114│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C7│0.14│0.12│0.12│112│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C8│0.1│0.11│0.1│93│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C9│0.15│0.16│0.16│85│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│C10│0.14│0.14│0.14│109│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│MacroAvg│0.1│0.1│0.1│1000│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│WeightedAvg│0.1│0.1│0.1│1000│
╘══════════════╧═════════════════╧══════════════╧════════════════╧═══════════════╛

Precision:YellowValue/SumOfYellowValueColumn

Recall:YellowValue/SumOfYellowValueRow

F1-Score:(2xPrecisionxRecall)/(Precision+Recall)Support:SumOfEachRow

MacroAvg:Precision:(SumOfPrecisionColumn)/ClassesCountRecall:(SumOfRecallColumn)/ClassesCountF1-Score:(SumOfF1-ScoreColumn)/ClassesCountSupport:Total(SumOfAllMatrix)WeightedAvg:Precision:(SumOf(Precisionxsupport))/Total(SumOfAllMatrix)Recall:(SumOf(RecallxSupport))/Total(SumOfAllMatrix)F1-Score:(SumOf(F1-ScorexSupport))/Total(SumOfAllMatrix)Support:Total(SumOfAllMatrix)Note:AllRealNumbersAreRoundedWithTwoDigitsAfterTheCommaHTMLFileGeneratedSuccesfuly:)You Can Plot, Generate HTML Page For Confusion Matrix And Classification Report Directly From One Calculationfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,10,1000)y_pred=randint(0,10,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=print_conf_mat(y_true,y_pred,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'])You Can Hide Detail Information For Confusion Matrix, Classification Report And Confusion Matrix Heatmapfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportprint_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,10,1000)y_pred=randint(0,10,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=print_conf_mat(y_true,y_pred,detail=False)# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'],detail=False)show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'],detail=False)OutputConfusionMatrix:________________

╒═══════════╤═════╤═════╤═════╤═════╤═════╤═════╤═════╤═════╤═════╤═════╕
│Classes│0│1│2│3│4│5│6│7│8│9│
╞═══════════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╡
│0│12│7│13│8│12│11│13│9│13│16│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│1│10│9│16│2│12│8│5│13│15│7│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│2│13│9│7│6│12│4│14│13│14│12│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│3│10│6│9│4│6│6│13│11│11│11│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│4│12│5│8│8│5│13│13│7│16│12│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│5│6│14│10│10│12│15│8│11│10│14│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│6│10│10│7│8│12│12│9│11│12│11│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│7│13│6│10│12│9│9│6│12│9│14│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│8│7│12│8│7│11│9│10│10│13│14│
├───────────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│9│6│9│4│10│14│13│7│10│9│4│
╘═══════════╧═════╧═════╧═════╧═════╧═════╧═════╧═════╧═════╧═════╧═════╛

╒══════════╤════════════════╕
││Rate(Score)│
╞══════════╪════════════════╡
│Accuracy│0.09│
├──────────┼────────────────┤
│Error│0.91│
╘══════════╧════════════════╛


ClassificationReport:_____________________

╒══════════════╤═════════════════╤══════════════╤════════════════╤═══════════════╕
││Precision(P)│Recall(R)│F1-Score(F)│Support(S)│
╞══════════════╪═════════════════╪══════════════╪════════════════╪═══════════════╡
│0│0.12│0.11│0.11│114│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│1│0.1│0.09│0.1│97│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│2│0.08│0.07│0.07│104│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│3│0.05│0.05│0.05│87│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│4│0.05│0.05│0.05│99│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│5│0.15│0.14│0.14│110│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│6│0.09│0.09│0.09│102│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│7│0.11│0.12│0.12│100│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│8│0.11│0.13│0.12│101│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│9│0.03│0.05│0.04│86│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│MacroAvg│0.09│0.09│0.09│1000│
├──────────────┼─────────────────┼──────────────┼────────────────┼───────────────┤
│WeightedAvg│0.09│0.09│0.09│1000│
╘══════════════╧═════════════════╧══════════════╧════════════════╧═══════════════╛HTMLFileGeneratedSuccesfuly:)You Can Plot And Generate HTML Page For Confusion Matrix Directly From One Calculation And Without Print Confusion Matrix And Classification Reportfromnumpy.randomimportrandintfrommatplotlib.pyplotimportshowfromconf_matimportcalc_conf_mat,plot_conf_mat,conf_mat_to_htmly_true=randint(0,10,1000)y_pred=randint(0,10,1000)# Show The Confusion Matrix On The Console (Classes Names Are Not Mandatory)cm=calc_conf_mat(y_true,y_pred)print(cm)# Show The Confusion Matrix As A Heat Map (Classes Names Are Not Mandatory)plot_conf_mat(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'],detail=False)show()# If You Are Using VSCode And You Are Executing The Code Direcctely# Show The Confusion Matrix As It Appears On The Console And As A Heat Map On The HTML Page (Classes Names Are Not Mandatory)conf_mat_to_html(conf_mat=cm,classes_names=['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'],detail=False)Output[[9,12,11,8,10,10,7,6,11,9],[12,9,12,9,7,3,16,7,7,11],[7,10,25,14,5,14,9,11,6,12],[7,15,9,11,13,13,4,7,10,10],[12,10,11,3,14,6,8,19,6,15],[5,14,13,17,9,8,8,7,11,17],[7,10,10,10,6,9,10,4,9,11],[8,9,11,7,11,11,15,9,13,12],[12,12,9,6,11,7,10,13,7,10],[11,5,9,13,8,14,3,9,15,13]]HTMLFileGeneratedSuccesfuly:)NoteThese outputs are specific to the print_conf_mat, plot_conf_mat and calc_conf_mat function only, in both cases. I'll leave it to you to discover the last function conf_mat_to_html on your own.LicenseThis project is licensed under theMIT License- see theLICENSEfile for details.