MATLAB中的混淆矩阵的实现

转自:https://blog.csdn.net/zhaomengszu/article/details/56283832

混淆矩阵的matlab代码实现:

actual:就是我们已知的label。

detected是我们通过模型预测得到的label

结合下面语句实现:

 [pred,acc,preb] = svmpredict(double(testLabel), testData, model, '-b 1');



[html] view plain copy
  1.  function confusion_matrix(actual,detected)  
  2.  [mat,order] = confusionmat(actual,detected);  
  3.   
  4.  %mat = rand(10);           %# A 5-by-5 matrix of random values from 0 to 1  
  5. % mat(3,3) = 0;            %# To illustrate  
  6. % mat(5,2) = 0;            %# To illustrate  
  7. imagesc(mat);            %# Create a colored plot of the matrix values  
  8. colormap(flipud(gray));  %# Change the colormap to gray (so higher values are  
  9.                          %#   black and lower values are white)  
  10.   
  11. textStrings = num2str(mat(:),'%0.02f');  %# Create strings from the matrix values  
  12. textStrings = strtrim(cellstr(textStrings));  %# Remove any space padding  
  13.   
  14. %% ## New code: ###  
  15. %idx = find(strcmp(textStrings(:), '0.00'));  
  16. %textStrings(idx) = {'   '};  
  17. %% ################  
  18.   
  19. [x,y] = meshgrid(1:5);   %# Create x and y coordinates for the strings  
  20. hStrings = text(x(:),y(:),textStrings(:),...      %# Plot the strings  
  21.                 'HorizontalAlignment','center');  
  22. midValue = mean(get(gca,'CLim'));  %# Get the middle value of the color range  
  23. textColors = repmat(mat(:) > midValue,1,3);  %# Choose white or black for the  
  24.                                              %#   text color of the strings so  
  25.                                              %#   they can be easily seen over  
  26.                                              %#   the background color  
  27. set(hStrings,{'Color'},num2cell(textColors,2));  %# Change the text colors  
  28.   
  29. set(gca,'XTick',1:5,...                         %# Change the axes tick marks  
  30.         'XTickLabel',{'Bob','Hyt','Maple','Study','Zm'},...  %#   and tick labels  
  31.         'YTick',1:5,...  
  32.         'YTickLabel',{'Bob','Hyt','Maple','Study','Zm'},...  
  33.         'TickLength',[0 0]);  



结果是:



猜你喜欢

转载自blog.csdn.net/dujiahei/article/details/80789945
今日推荐