Confusion Matrix 3D with Overall PCC and Group Statistics
function [confusion_matrix overall_pcc group_stats groups_list] = confusionMatrix3d(predicted_groups,actual_groups)
% DESCRIPTION:
% Confusion matrix-based model performance summary tool.
% Works with character and numeric data, for any number of groups.
%
% Displays your confusion matrix as a 3D bar chart of your observations,
% broken down by their actual and predicted groups.
%
% Assumes that union(predicted and actual_groups) contains all
% possibilities for Groups.
%
% Returns the overall PCC and the following stats per group:
% True Positives, False Positives, True Negatives, False Negatives,
% Sensitivity, Specificity, PCC.
%
%
% OUTPUT:
% 1) a 3D Bar Chart of the number of observations per group predicted as
% each group (helps you visualize the performance of your model in
% predicting each of several groups). X and Y tick labels are the
% names (char or numeric) of your predicted and actual groups in ascending
% alphanumeric order (the same order in the groups_list variable).
%
% 2) confusion_matrix (matrix of doubles): the counts underlying the 3D Bar
% Chart confusion matrix, where columns are different predicted groups, in
% ascending alphanumeric order, and rows are different actual groups, in
% ascending alphanumeric order (the same order in the groups_list variable)
%
% 3) overall_pcc (double): the overall Percent Correctly Classified in your data
%
% 4) group_stats (cell array of structs), where each struct contains:
% group -- the name of the group for the current stat struct
% TP, FP, FN,TN -- True&False Positives&Negatives for the group
% sensitivity -- TP/(TP+FN) for the group
% specificity -- TN/(TN+FP) for the group
% PCC -- (TP+TN)/(TP+TN+FP+FN) for the group
% the cell array's structs are arranged in alphanumeric order of
% group names.
%
% 5) groups_list (cell array of chars or vector): the names of groups in
% alphanumeric order, the same order as they appear on the Confusion Matrix
% 3D Bar Chart and in the group_stats cell array.
%
% INPUTS
% parameter_name (datatype)-- description
% 1) predicted_groups (vector of numeric/logicals, or cell array of chars)--
% The group for each observation, as predicted by your model. If you are
% using a logistic regression model, you need to translate the predicted
% logit scores/ probabilities into groups, based on your own cutoff
% value(s), and then feed those groups into this function.
%
% 2) actual_groups (vector of numeric/logicals, or cell array of chars)--
% The group for each observation, based on your actual data.
%
% Note: if one of these two inputs is a cell array of chars, both need to
% be cell arrays of chars.
Cite As
Brian Weidenbaum (2025). Confusion Matrix 3D with Overall PCC and Group Statistics (https://www.mathworks.com/matlabcentral/fileexchange/36053-confusion-matrix-3d-with-overall-pcc-and-group-statistics), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- AI and Statistics > Statistics and Machine Learning Toolbox > Descriptive Statistics and Visualization >
- MATLAB > Graphics > 2-D and 3-D Plots > Data Distribution Plots > Bar Plots >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.1.0.0 | % Changes between versions 1.1 and 1.2
|
||
1.0.0.0 |