MATLAB Examples

# Confusion matrix

## Contents

```clc clear all close all warning off all rng('default') ```

## Proof

```disp('Running Proof....') n=100;m=4; actual=round(rand(1,n)*m); [c_matrixp,Result]= confusion.getMatrix(actual,actual); disp('Getting Values') Accuracy=Result.Accuracy Error=Result.Error Sensitivity=Result.Sensitivity Specificity=Result.Specificity Precision=Result.Precision FalsePositiveRate=Result.FalsePositiveRate F1_score=Result.F1_score MatthewsCorrelationCoefficient=Result.MatthewsCorrelationCoefficient Kappa=Result.Kappa ```
```Running Proof.... Class List in given sample 0 1 2 3 4 Total Instance = 100 class1==>0 class2==>1 class3==>2 class4==>3 class5==>4 Confusion Matrix predict_class1 predict_class2 predict_class3 ______________ ______________ ______________ Actual_class1 10 0 0 Actual_class2 0 25 0 Actual_class3 0 0 21 Actual_class4 0 0 0 Actual_class5 0 0 0 predict_class4 predict_class5 ______________ ______________ Actual_class1 0 0 Actual_class2 0 0 Actual_class3 0 0 Actual_class4 28 0 Actual_class5 0 16 Multi-Class Confusion Matrix Output TruePositive FalsePositive FalseNegative ____________ _____________ _____________ Actual_class1 10 0 0 Actual_class2 25 0 0 Actual_class3 21 0 0 Actual_class4 28 0 0 Actual_class5 16 0 0 TrueNegative ____________ Actual_class1 90 Actual_class2 75 Actual_class3 79 Actual_class4 72 Actual_class5 84 AccuracyOfSingle ErrorOfSingle AccuracyInTotal ErrorInTotal ________________ _____________ _______________ ____________ 1 0 0.1 0 1 0 0.25 0 1 0 0.21 0 1 0 0.28 0 1 0 0.16 0 Sensitivity Specificity Precision FalsePositiveRate F1_score ___________ ___________ _________ _________________ ________ 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 MatthewsCorrelationCoefficient Kappa TruePositive FalsePositive ______________________________ _____ ____________ _____________ 1 0.8 10 0 1 0.5 25 0 1 0.58 21 0 1 0.44 28 0 1 0.68 16 0 FalseNegative TrueNegative Class _____________ ____________ ____________ 0 90 'class1==>0' 0 75 'class2==>1' 0 79 'class3==>2' 0 72 'class4==>3' 0 84 'class5==>4' Over all valuses Accuracy: 1 Error: 0 Sensitivity: 1 Specificity: 1 Precision: 1 FalsePositiveRate: 0 F1_score: 1 MatthewsCorrelationCoefficient: 1 Kappa: 1 Getting Values Accuracy = 1 Error = 0 Sensitivity = 1 Specificity = 1 Precision = 1 FalsePositiveRate = 0 F1_score = 1 MatthewsCorrelationCoefficient = 1 Kappa = 1 ```

## Multiclass demo

```disp('_____________Multiclass demo_______________') disp('Runing Multiclass confusionmat') n=100;m=2; actual=round(rand(1,n)*m); predict=round(rand(1,n)*m); [c_matrix,Result,RefereceResult]= confusion.getMatrix(actual,predict); % % %DIsplay off % % [c_matrix,Result,RefereceResult]= confusionmat(actual,predict,0) ```
```_____________Multiclass demo_______________ Runing Multiclass confusionmat Class List in given sample 0 1 2 Total Instance = 100 class1==>0 class2==>1 class3==>2 Confusion Matrix predict_class1 predict_class2 predict_class3 ______________ ______________ ______________ Actual_class1 6 20 5 Actual_class2 9 23 13 Actual_class3 9 11 4 Multi-Class Confusion Matrix Output TruePositive FalsePositive FalseNegative ____________ _____________ _____________ Actual_class1 6 18 25 Actual_class2 23 31 22 Actual_class3 4 18 20 TrueNegative ____________ Actual_class1 51 Actual_class2 24 Actual_class3 58 AccuracyOfSingle ErrorOfSingle AccuracyInTotal ErrorInTotal ________________ _____________ _______________ ____________ 0.19355 0.80645 0.06 0.18 0.51111 0.48889 0.23 0.31 0.16667 0.83333 0.04 0.18 Sensitivity Specificity Precision FalsePositiveRate F1_score ___________ ___________ _________ _________________ ________ 0.19355 0.73913 0.25 0.26087 0.21818 0.51111 0.43636 0.42593 0.56364 0.46465 0.16667 0.76316 0.18182 0.23684 0.17391 MatthewsCorrelationCoefficient Kappa TruePositive FalsePositive ______________________________ _______ ____________ _____________ 0.072903 0.57319 6 18 0.05243 0.34545 23 31 0.07235 0.63083 4 18 FalseNegative TrueNegative Class _____________ ____________ ____________ 25 51 'class1==>0' 22 24 'class2==>1' 20 58 'class3==>2' Over all valuses Accuracy: 0.3300 Error: 0.6700 Sensitivity: 0.2904 Specificity: 0.6462 Precision: 0.2859 FalsePositiveRate: 0.3538 F1_score: 0.2856 MatthewsCorrelationCoefficient: 0.0659 Kappa: 0.3367 ```

## Two Class demo

```disp('____________Two Class demo________________') disp('Running Simple Confusionmat...') n=100;m=1; actual=round(rand(1,n)*m); predict=round(rand(1,n)*m); % [c_matrix,Result]= confusionmat(actual,predict) [c_matrix,Result]= confusion.getMatrix(actual,predict); ```
```____________Two Class demo________________ Running Simple Confusionmat... Class List in given sample 0 1 Total Instance = 100 class1==>0 class2==>1 Confusion Matrix predict_class1 predict_class2 ______________ ______________ Actual_class1 26 28 Actual_class2 24 22 Two-Class Confution Matrix '' 'TruePositive' 'FalsePositive' 'FalseNegative' [ 26] [ 28] 'TrueNegative=TN' [ 24] [ 22] Over all valuses Accuracy: 0.4800 Error: 0.5200 Sensitivity: 0.4815 Specificity: 0.4783 Precision: 0.5200 FalsePositiveRate: 0.5217 F1_score: 0.5000 MatthewsCorrelationCoefficient: 0.0401 Kappa: 0.0385 ```

## Get Calculation using confusion matrix

```disp('____________Get Calculation using confusion matrix________________') n=5; c_matrix=randi([20,40],[n,n]); disp('confusion matrix generated') disp(c_matrix) disp('Running Calcualtion...') [Result,RefereceResult]=confusion.getValues(c_matrix); disp(Result) disp(RefereceResult) ```
```____________Get Calculation using confusion matrix________________ confusion matrix generated 32 22 31 40 28 31 39 33 24 29 38 33 31 22 27 25 30 35 22 36 26 33 30 21 33 Running Calcualtion... Accuracy: 0.2091 Error: 0.7909 Sensitivity: 0.2088 Specificity: 0.8022 Precision: 0.2078 FalsePositiveRate: 0.1978 F1_score: 0.2080 MatthewsCorrelationCoefficient: 0.0265 Kappa: 0.5954 AccuracyOfSingle: [5x1 double] ErrorOfSingle: [5x1 double] AccuracyInTotal: [5x1 double] ErrorInTotal: [5x1 double] Sensitivity: [5x1 double] Specificity: [5x1 double] Precision: [5x1 double] FalsePositiveRate: [5x1 double] F1_score: [5x1 double] MatthewsCorrelationCoefficient: [5x1 double] Kappa: [5x1 double] TruePositive: [5x1 double] FalsePositive: [5x1 double] FalseNegative: [5x1 double] TrueNegative: [5x1 double] ```