python 用confusion_matrix 输出tn fp fn tp
时间: 2023-10-01 08:03:40 浏览: 101
可以使用sklearn.metrics模块中的confusion_matrix函数来输出tn, fp, fn, tp。
示例代码如下:
```python
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 1, 0, 0, 1]
y_pred = [0, 1, 0, 1, 0, 1, 0, 1]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print('True Negative (TN):', tn)
print('False Positive (FP):', fp)
print('False Negative (FN):', fn)
print('True Positive (TP):', tp)
```
输出结果:
```
True Negative (TN): 2
False Positive (FP): 1
False Negative (FN): 2
True Positive (TP): 3
```
其中,y_true和y_pred分别是真实标签和预测标签,ravel()函数将输出的混淆矩阵展平为一维数组,然后用四个变量分别接收tn, fp, fn, tp。最后输出这四个变量即可。
相关问题
怎样用confusion_matrix输出FP样本个数
可以使用sklearn.metrics库中的confusion_matrix()函数来计算混淆矩阵,并从中获取FP样本个数。具体代码如下:
```python
from sklearn.metrics import confusion_matrix
# 假设y_true和y_pred是真实标签和预测标签
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
# 输出FP样本个数
print("FP样本个数:", fp)
```
其中,confusion_matrix()函数返回一个4x4的混淆矩阵,分别代表True Negative(TN)、False Positive(FP)、False Negative(FN)和True Positive(TP)的个数。使用ravel()函数将混淆矩阵展开成一维数组,然后通过索引取出FP的个数即可。
python 两个图像的tp tn fp fn
在Python中,可以使用sklearn.metrics库中的confusion_matrix函数来计算TP、TN、FP、FN。示例代码如下:
```python
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 1] # 实际标签
y_pred = [1, 1, 0, 1, 0] # 预测标签
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(f"TP = {tp}, TN = {tn}, FP = {fp}, FN = {fn}")
```
输出结果为:TP = 2, TN = 1, FP = 1, FN = 1。