mtlab 에서 confusion matrix 구현

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

답변 (1개)

Angelo Yeo
Angelo Yeo 2024년 1월 28일
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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!