matlab利用热力图绘制混淆矩阵
时间: 2023-10-02 19:09:20 浏览: 120
以下是利用热力图绘制混淆矩阵的matlab代码:
```matlab
% 模拟数据
actual = [1 1 0 0 1 0 1 0 1 0];
predicted = [1 1 1 0 1 0 0 1 1 0];
% 计算混淆矩阵
confusion_matrix = confusionmat(actual, predicted);
% 绘制热力图
heatmap(confusion_matrix, 'Colormap', 'gray', 'Colorbar', true, 'XLabel', 'Predicted', 'YLabel', 'Actual');
```
运行以上代码,可以得到如下的热力图结果:
![confusion_matrix_heatmap](https://i.loli.net/2020/10/16/FmZAMuR7VUSvW9G.png)
其中,混淆矩阵的行表示实际类别,列表示预测类别,数值表示预测为该类别的样本在实际中的数量。对角线上的数值表示预测正确的样本数,其他位置的数值表示预测错误的样本数。热力图的颜色越深表示样本数越多,颜色越浅表示样本数越少。可以通过观察热力图来了解模型的分类性能。
相关问题
matlab混淆矩阵热力图
Matlab混淆矩阵热力图是一种用于可视化分类模型性能的工具。混淆矩阵是一个二维表格,用于展示分类模型在不同类别上的预测结果与真实标签之间的对应关系。热力图则通过颜色的变化来表示不同类别之间的预测结果的数量或比例。
在Matlab中,你可以使用confusionchart函数来创建混淆矩阵热力图。以下是一个简单的示例代码:
```matlab
% 创建一个混淆矩阵
labels = {'Cat', 'Dog', 'Bird'};
trueLabels = {'Cat', 'Dog', 'Bird', 'Bird', 'Dog', 'Cat'};
predictedLabels = {'Cat', 'Dog', 'Bird', 'Bird', 'Cat', 'Dog'};
% 绘制混淆矩阵热力图
confusionMatrix = confusionmat(trueLabels, predictedLabels);
confusionChart = confusionchart(confusionMatrix, labels);
```
运行以上代码,你将得到一个包含了混淆矩阵热力图的图形窗口。该热力图将根据混淆矩阵中每个元素的值来显示不同颜色,从而反映出分类模型在各个类别上的预测结果。
matlab混淆矩阵可视化图代码
以下是一个基本的 MATLAB 混淆矩阵可视化示例:
```matlab
% 创建混淆矩阵
cm = [98 2 0; 10 85 5; 3 10 87];
% 计算每个类别的总数
total = sum(cm, 2);
% 计算每个类别的正确预测数
correct = diag(cm);
% 计算每个类别的错误预测数
incorrect = total - correct;
% 计算每个类别的召回率
recall = correct ./ total;
% 创建绘图对象
figure;
% 绘制热力图
heatmap(cm);
% 添加标签和标题
xlabel('预测标签');
ylabel('真实标签');
title('混淆矩阵');
% 添加文本标签
textStrings = num2str(cm(:),'%d');
textStrings = strtrim(cellstr(textStrings));
[x,y] = meshgrid(1:3);
hStrings = text(x(:),y(:),textStrings(:),...
'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));
textColors = repmat(cm(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
```
这段代码将创建一个简单的混淆矩阵并将其可视化为一个热力图。它还将添加文本标签以显示每个单元格中的值。你可以根据需要更改矩阵的大小和内容,以及标签和标题的样式。
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)