mtlab 에서 confusion matrix 구현

2 views (last 30 days)
jimin jung
jimin jung on 29 Apr 2020
Answered: Angelo Yeo on 28 Jan 2024
매트랩으로 빌트인 함수인 confusionmat 또는 plotconfusionmat를 사용하지 않고 기본함수로 multi-class confusion matrix를 구현하려면 코드를 어떻게 짜야 할까요? Pred는 [n,1] , groundtruth는 [n,1]배열일 때의 상황입니다.

Answers (1)

Angelo Yeo
Angelo Yeo on 28 Jan 2024
n = 100;
groundtruth = randi(2 ,n, 1);
pred = randi(2, n, 1);
% Initialize the confusion matrix
numClasses = max(groundtruth);
confusionMatrix = zeros(numClasses);
% Calculate the confusion matrix
for i = 1:length(groundtruth)
trueClass = groundtruth(i);
predictedClass = pred(i);
confusionMatrix(predictedClass, trueClass) = confusionMatrix(predictedClass, trueClass) + 1;
end
actual_1 = confusionMatrix(:,1);
actual_2 = confusionMatrix(:,2);
confusionMatrixTable = table(actual_1, actual_2, 'rownames', ["pred_1", "pred_2"])
confusionMatrixTable = 2×2 table
actual_1 actual_2 ________ ________ pred_1 24 23 pred_2 25 28

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!