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 Accuracy Error Sensitivity Specificity Precision ________ _____ ___________ ___________ _________ 0.1 0.9 1 1 1 0.25 0.75 1 1 1 0.21 0.79 1 1 1 0.28 0.72 1 1 1 0.16 0.84 1 1 1 FalsePositiveRate F1_score MatthewsCorrelationCoefficient Kappa _________________ ________ ______________________________ _____ 0 1 1 0.8 0 1 1 0.5 0 1 1 0.58 0 1 1 0.44 0 1 1 0.68 TruePositive FalsePositive FalseNegative TrueNegative ____________ _____________ _____________ ____________ 10 0 0 90 25 0 0 75 21 0 0 79 28 0 0 72 16 0 0 84 Class ____________ 'class1==>0' 'class2==>1' 'class3==>2' 'class4==>3' '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 Accuracy Error Sensitivity Specificity Precision ________ _____ ___________ ___________ _________ 0.06 0.94 0.19355 0.73913 0.25 0.23 0.77 0.51111 0.43636 0.42593 0.04 0.96 0.16667 0.76316 0.18182 FalsePositiveRate F1_score MatthewsCorrelationCoefficient Kappa _________________ ________ ______________________________ _______ 0.26087 0.21818 0.072903 0.57319 0.56364 0.46465 0.05243 0.34545 0.23684 0.17391 0.07235 0.63083 TruePositive FalsePositive FalseNegative TrueNegative ____________ _____________ _____________ ____________ 6 18 25 51 23 31 22 24 4 18 20 58 Class ____________ 'class1==>0' 'class2==>1' 'class3==>2' Over all valuses Accuracy: 0.33 Error: 0.67 Sensitivity: 0.29044 Specificity: 0.64622 Precision: 0.28591 FalsePositiveRate: 0.35378 F1_score: 0.28558 MatthewsCorrelationCoefficient: 0.065894 Kappa: 0.33665 ```

## 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.48 Error: 0.52 Sensitivity: 0.48148 Specificity: 0.47826 Precision: 0.52 FalsePositiveRate: 0.52174 F1_score: 0.5 MatthewsCorrelationCoefficient: 0.040129 Kappa: 0.038462 ```

## 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.20905 Error: 0.79095 Sensitivity: 0.20877 Specificity: 0.80224 Precision: 0.20778 FalsePositiveRate: 0.19776 F1_score: 0.20804 MatthewsCorrelationCoefficient: 0.026498 Kappa: 0.59542 Accuracy: [5x1 double] Error: [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] ```