Clear Filters
Clear Filters

how can i plot Confusion matrix by using the predicted and actual data ?

2 views (last 30 days)
Hi all,
I would obtain the confusion matrix table and use the code in graphs section the classification based on KNN classifier but it gave me error and i do not know what is the issue.
Hopfully,I can have help and i will attach the workspace here Google drive and the code that has been used.below.
Traindata = readtable('phase b.csv');
Testdata = readtable('testing_b.csv');
Actualdata = readtable('testing_b.csv');
%% -------------- Feature Scalling -------------------------------
% -------------- Method 1: Standardization ----------------------
% ---------------------------- Code -----------------------------
%training
stand_I = (Traindata.I - mean(Traindata.I))/std(Traindata.I);
Traindata.I = stand_I;
stand_H3 = (Traindata.H3 - mean(Traindata.H3))/std(Traindata.H3);
Traindata.H3 = stand_H3;
stand_H5 = (Traindata.H5 - mean(Traindata.H5))/std(Traindata.H5);
Traindata.H5 = stand_H5;
stand_H7 = (Traindata.H7 - mean(Traindata.H7))/std(Traindata.H7);
Traindata.H7 = stand_H7;
stand_H9 = (Traindata.H9 - mean(Traindata.H9))/std(Traindata.H9);
Traindata.H9 = stand_H9;
%testing
stand_I = (Testdata.I - mean(Testdata.I))/std(Testdata.I);
Testdata.I = stand_I;
stand_H3 = (Testdata.H3 - mean(Testdata.H3))/std(Testdata.H3);
Testdata.H3 = stand_H3;
stand_H5 = (Testdata.H5 - mean(Testdata.H5))/std(Testdata.H5);
Testdata.H5 = stand_H5;
stand_H7 = (Testdata.H7 - mean(Testdata.H7))/std(Testdata.H7);
Testdata.H7 = stand_H7;
stand_H9 = (Testdata.H9 - mean(Testdata.H9))/std(Testdata.H9);
Testdata.H9 = stand_H9;
%testing
stand_I = (Actualdata.I - mean(Actualdata.I))/std(Actualdata.I);
Actualdata.I = stand_I;
stand_H3 = (Actualdata.H3 - mean(Actualdata.H3))/std(Actualdata.H3);
Actualdata.H3 = stand_H3;
stand_H5 = (Actualdata.H5 - mean(Actualdata.H5))/std(Actualdata.H5);
Actualdata.H5 = stand_H5;
stand_H7 = (Actualdata.H7 - mean(Actualdata.H7))/std(Actualdata.H7);
Actualdata.H7 = stand_H7;
stand_H9 = (Actualdata.H9 - mean(Actualdata.H9))/std(Actualdata.H9);
Actualdata.H9 = stand_H9;
X = [Traindata.I Traindata.THD Traindata.H3 Traindata.H5 Traindata.H7 Traindata.H9];
Y = ordinal(Traindata.Loads);
isCategoricalPredictor = [false, false, false, false, false, false];
%% BUILD THE CLASSIFIER KNN
classificationKNN = fitcknn(...
X, ...
Y, ...
'Distance', 'Euclidean', ...
'Exponent', [], ...
'NumNeighbors', 50, ...
'DistanceWeight', 'Equal', ...
'Standardize', true, ...
'ClassNames', [1; 2; 3; 4]);
classificationKNN = compact(classificationKNN);
[predClass,classifScore] = predict(classificationKNN,[Testdata.I Testdata.THD Testdata.H3 Testdata.H5 Testdata.H7 Testdata.H9]);
classnames = classificationKNN.ClassNames;
Preddata = [table(Testdata.Loads,predClass),array2table(classifScore)];
Preddata.Properties.VariableNames = [{'Actual'},{'Predicted'},classnames'];
%% Graphs
figure
C = confusionchart(Actualdata.Loads,[Preddata.Predicted{:}]-'0');
sortClasses(C,{'1' '2' '3' '4'})
C.Normalization = 'row-normalized';

Accepted Answer

Hiro Yoshino
Hiro Yoshino on 12 Sep 2022
Preddata = [table(Testdata.Loads,predClass),array2table(classifScore)];
It seems the statement above is not right. For confusionchart, you should use "Labels". classifScore is not the one that confusionchart takes (see: confusionchart ).
C = confusionchart(Actualdata.Loads,[Preddata.Predicted{:}]-'0')
This seems to be comparing the "label" with the corresponding "score (value)". I believe this is where the issue arises.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!