Li‘s 影像组学视频学习笔记(25)-查看准确度、灵敏度、特异度及混淆矩阵

本笔记来源于B站Up主: 有Li 的影像组学系列教学视频
本节(25)主要讲解: 通过sklearn包输出准确度、灵敏度、特异度及混淆矩阵

  1. 基本概念

在这里插入图片描述

在这里插入图片描述

  1. 代码实现
from sklearn.metrics import classification_report
y_pred = [0,1,0,1,0,0,1]
y_true = [0,0,0,1,1,0,1]
print(classification_report(y_true,y_pred))

# 结果如下:
#               precision    recall  f1-score   support
#            0       0.75      0.75      0.75         4
#            1       0.67      0.67      0.67         3

#     accuracy                           0.71         7
#    macro avg       0.71      0.71      0.71         7
# weighted avg       0.71      0.71      0.71         7
# 显示 confusion matrix
# method 1
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_true, y_pred))

# [[3 1]
#  [1 2]]
# method 2 (图形化输出)
import matplotlib.pyplot as plt
cm = confusion_matrix(y_true, y_pred)

fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(cm)
ax.grid(False)
ax.xaxis.set(ticks=(0, 1), ticklabels=('Predicted 0s', 'Predicted 1s'))
ax.yaxis.set(ticks=(0, 1), ticklabels=('Actual 0s', 'Actual 1s'))
ax.set_ylim(1.5, -0.5)
for i in range(2):
    for j in range(2):
        ax.text(j, i, cm[i, j], ha='center', va='center', color='red')
plt.show()

结果如图:

在这里插入图片描述

作者:北欧森林
链接:https://www.jianshu.com/p/347323b74d91
来源:简书,已获授权转载

RadiomicsWorld.com “影像组学世界”论坛:
影像组学世界/RadiomicsWorld

在R中,通过随机森林实例化混淆矩阵、计算准确率(accuracy)、灵敏度(sensitivity)、特异度(specificity)可以使用以下步骤: 1. 导入数据并将其分成训练集和测试集。 ```r library(randomForest) # 导入数据 data(iris) # 将数据分成训练集和测试集 set.seed(123) train_index <- sample(nrow(iris), 0.7 * nrow(iris)) train_data <- iris[train_index, ] test_data <- iris[-train_index, ] ``` 2. 使用随机森林模型训练数据。 ```r # 使用随机森林训练数据 rf_model <- randomForest(Species ~ ., data = train_data) ``` 3. 使用测试集来预测结果,并生成混淆矩阵。 ```r # 使用测试集预测结果 rf_pred <- predict(rf_model, test_data) # 生成混淆矩阵 table(test_data$Species, rf_pred) ``` 4. 计算准确率、灵敏度特异度。 ```r # 计算准确率 accuracy <- sum(diag(table(test_data$Species, rf_pred))) / sum(table(test_data$Species, rf_pred)) cat("Accuracy:", round(accuracy, 3), "\n") # 计算灵敏度特异度 sensitivity <- diag(table(test_data$Species, rf_pred)) / rowSums(table(test_data$Species, rf_pred)) specificity <- colSums(table(test_data$Species, rf_pred)) - diag(table(test_data$Species, rf_pred)) / colSums(table(test_data$Species, rf_pred) - rowSums(table(test_data$Species, rf_pred))) cat("Sensitivity:", round(sensitivity, 3), "\n") cat("Specificity:", round(specificity, 3), "\n") ``` 以上代码能够在iris数据集上进行测试,您可以用自己的数据代替。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值