• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab计算混淆矩阵

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
function [confmatrix] = cfmatrix(actual, predict, classlist, per)
% CFMATRIX calculates the confusion matrix for any prediction 
% algorithm that generates a list of classes to which the test 
% feature vectors are assigned
%
% Outputs: confusion matrix
%
%                 Actual Classes
%                   p       n
%              ___|_____|______| 
%    Predicted  p'|     |      |
%      Classes  n'|     |      |
% 
% Inputs: 
% 1. actual / 2. predict
% The inputs provided are the 'actual' classes vector
% and the 'predict'ed classes vector. The actual classes are the classes
% to which the input feature vectors belong. The predicted classes are the 
% class to which the input feature vectors are predicted to belong to, 
% based on a prediction algorithm. 
% The length of actual class vector and the predicted class vector need to 
% be the same. If they are not the same, an error message is displayed. 
% 3. classlist
% The third input provides the list of all the classes {p,n,...} for which 
% the classification is being done. All classes are numbers.
% 4. per = 1/0 (default = 0)
% This parameter when set to 1 provides the values in the confusion matrix 
% as percentages. The default provides the values in numbers.
%
% Example:
% >> a = [ 1 2 3 1 2 3 1 1 2 3 2 1 1 2 3];
% >> b = [ 1 2 3 1 2 3 1 1 1 2 2 1 2 1 3];
% >> Cf = cfmatrix(a, b);
%
% [Avinash Uppuluri: [email protected]: Last modified: 08/21/08]

% If classlist not entered: make classlist equal to all 
% unique elements of actual
if (nargin < 2)
   error('Not enough input arguments.');
elseif (nargin == 2)
    classlist = unique(actual); % default values from actual
    per = 0; % default is numbers and input 1 for percentage
elseif (nargin == 3)
    per = 0; % default is numbers and input 1 for percentage
end

if (length(actual) ~= length(predict))
    error('First two inputs need to be vectors with equal size.');
elseif ((size(actual,1) ~= 1) && (size(actual,2) ~= 1))
    error('First input needs to be a vector and not a matrix');
elseif ((size(predict,1) ~= 1) && (size(predict,2) ~= 1))
    error('Second input needs to be a vector and not a matrix');
end
format short g;
n_class = length(classlist);
line_two = '----------';
line_three = '_________|';
for i = 1:n_class
    obind_class_i = find(actual == classlist(i));
    prind_class_i = find(predict == classlist(i));
    confmatrix(i,i) = length(intersect(obind_class_i,prind_class_i));
    for j = 1:n_class
        %if (j ~= i)
        if (j < i)
        % observed j predicted i
        confmatrix(i,j) = length(find(actual(prind_class_i) == classlist(j))); 
        % observed i predicted j
        confmatrix(j,i) = length(find(predict(obind_class_i) == classlist(j)));
        end
    end
    line_two = strcat(line_two,'---',num2str(classlist(i)),'-----');
    line_three = strcat(line_three,'__________');
end

if (per == 1)
    confmatrix = (confmatrix ./ length(actual)).*100;
end

% output to screen
disp('------------------------------------------');
disp('             Actual Classes');
disp(line_two);
disp('Predicted|                     ');
disp('  Classes|                     ');
disp(line_three);

for i = 1:n_class
    temps = sprintf('       %d             ',i);
    for j = 1:n_class
    temps = strcat(temps,sprintf(' |    %2.1f    ',confmatrix(i,j)));
    end
    disp(temps);
    clear temps
end
disp('------------------------------------------');
        

 调用:

a=importdata('E:\\actual_label.txt');
b=importdata('E:\\predict_label.txt');
Cf = cfmatrix(a, b)

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB2012blicensecheckoutfailed发布时间:2022-07-18
下一篇:
OpenCV学习笔记(14)利用Matlab查看双目视觉景深效果发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap