回顾:筛查测试
本课属于 Coddy Machine Learning 学习旅程的“监督学习”单元。第 26 课,共 58 课。
挑战
中等还记得本章开头提到的漏诊案例吗?诊所要筛查组织样本中的癌症,而漏掉恶性样本是最严重的错误。程序会读取诊所需要的召回率;起始代码会加载并缩放样本(1 表示恶性),并按严格程度从高到低列出 thresholds。
- 训练一个逻辑回归模型,并输出它的 AUC。
- 找出召回率达到要求的第一个阈值。
- 在该阈值下,按下方格式输出数量和精确率。
对于另一家诊所和 0.9:
AUC: 0.981
Threshold: 0.4
Caught 47 of 51 malignant samples
False alarms: 2
Precision: 0.959亲手试试
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score, recall_score, precision_score, confusion_matrix
# 诊所要求的召回率,例如 0.95
needed = float(input())
# 组织样本:y 中的 1 表示恶性,这是筛查必须检出的情况
cancer = load_breast_cancer()
X = cancer.data
y = 1 - cancer.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 要尝试的阈值,从最严格的开始
thresholds = [0.5, 0.4, 0.3, 0.2, 0.1, 0.05]
# 在下方编写你的代码
监督学习中的所有课
自己练习: Python 在线编译器