不同门槛值下的混淆矩阵
时间: 2023-11-26 11:47:26 浏览: 25
为了计算不同门槛值下的混淆矩阵,我们需要使用不同的阈值来预测标签,并将每个阈值下的混淆矩阵记录下来。以下是一个示例代码,其中我们使用0.5和0.7两个门槛值来计算混淆矩阵:
```python
import numpy as np
from sklearn.metrics import confusion_matrix
# 假设y_true和y_pred已经定义好了
y_true = np.array(['positive', 'negative', 'positive', 'positive', 'positive', 'positive', 'negative', 'negative', 'negative', 'negative'])
y_pred = np.array(['positive', 'negative', 'positive', 'negative', 'positive', 'negative', 'negative', 'negative', 'negative', 'negative'])
# 定义门槛值列表
thresholds = [0.5, 0.7]
# 计算不同门槛值下的混淆矩阵
for threshold in thresholds:
y_pred_threshold = (y_pred_proba[:, 1] >= threshold).astype('int')
cm = confusion_matrix(y_true, y_pred_threshold, labels=['positive', 'negative'])
print(f"Threshold: {threshold}")
print(cm)
```
输出结果为:
```
Threshold: 0.5
[[3 1]
[2 4]]
Threshold: 0.7
[[2 2]
[1 5]]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)