MATLAB Answers

How to plot confusion matrix for 2 classes (genuine or fraud)

2 views (last 30 days)
Tam Swee Chin
Tam Swee Chin on 1 Apr 2021
Answered: Athul Prakash on 6 Apr 2021
clc
clear
trainingSetup = load("C:\Users\User\Desktop\Master Application\02 Dissertion\Signature\Kaggle\Real_A1\trainingSetup_2021_04_01__20_23_12.mat");
imdsTrain = imageDatastore("C:\Users\User\Desktop\Master Application\02 Dissertion\Signature\Testing","IncludeSubfolders",true,"LabelSource","foldernames");
[imdsTrain, imdsValidation] = splitEachLabel(imdsTrain,0.7,"randomized");
% Resize the images to match the network input layer.
augimdsTrain = augmentedImageDatastore([224 224 3],imdsTrain);
augimdsValidation = augmentedImageDatastore([224 224 3],imdsValidation);
opts = trainingOptions("rmsprop",...
"ExecutionEnvironment","auto",...
"InitialLearnRate",0.001,...
"Shuffle","every-epoch",...
"Plots","training-progress",...
"ValidationData",augimdsValidation);
layers = [
imageInputLayer([224 224 3],"Name","imageinput")
convolution2dLayer([3 3],32,"Name","conv","Padding","same")
reluLayer("Name","relu")
maxPooling2dLayer([5 5],"Name","maxpool","Padding","same")
fullyConnectedLayer(2,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
[net, traininfo] = trainNetwork(augimdsTrain,layers,opts);

Answers (1)

Athul Prakash
Athul Prakash on 6 Apr 2021
Hi Tam,
First, you may set aside some of your data as test data.
With this test dataset, obtain Y_Actual as the labels and X as the values in the test data.
After that,
YPred = predict(net, X);
cmat = confusionmat(YActual, YPred);
1) Check out the doc on confusionmat and the examples found there.
2) You may also refer to confusionchart(), which creates a plot of the confusion matrix as well.
Hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!