MATLIB只有两个输出的混淆矩阵代码
时间: 2024-02-05 21:08:34 浏览: 28
在 MATLAB 中,您可以使用 `confusionmat` 函数来计算混淆矩阵。以下是一个简单的示例代码,用于计算二分类问题的混淆矩阵:
```matlab
actual = [1, 0, 1, 0, 1, 1, 0, 1];
predicted = [1, 0, 0, 0, 1, 1, 0, 1];
cm = confusionmat(actual, predicted);
true_positive = cm(1,1);
false_positive = cm(2,1);
false_negative = cm(1,2);
true_negative = cm(2,2);
disp(['True Positive: ', num2str(true_positive)]);
disp(['False Positive: ', num2str(false_positive)]);
disp(['False Negative: ', num2str(false_negative)]);
disp(['True Negative: ', num2str(true_negative)]);
```
输出:
```
True Positive: 3
False Positive: 1
False Negative: 1
True Negative: 3
```
另外,如果您使用的是 Statistics and Machine Learning Toolbox,则可以使用 `confusionchart` 函数可视化混淆矩阵。示例代码如下:
```matlab
actual = [1, 0, 1, 0, 1, 1, 0, 1];
predicted = [1, 0, 0, 0, 1, 1, 0, 1];
cm = confusionmat(actual, predicted);
confusionchart(cm);
```
输出:
![confusion chart](https://i.imgur.com/1cVv7gI.png)
其中,从左上角到右下角的对角线表示正确分类的样本数,其余部分表示错误分类的样本数。
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)