復習:スクリーニング検査
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オンラインコンパイラ