ロジスティック回帰の当てはまりを定量化

Pythonで学ぶstatsmodelsによる回帰入門

Maarten Van den Broeck

Content Developer at DataCamp

4つの結果

予測: 偽 予測: 真
実際: 偽 正解 偽陽性
実際: 真 偽陰性 正解
Pythonで学ぶstatsmodelsによる回帰入門

混同行列:結果の件数

actual_response = churn["has_churned"]
predicted_response = np.round(mdl_recency.predict())
outcomes = pd.DataFrame({"actual_response": actual_response,
                         "predicted_response": predicted_response})
print(outcomes.value_counts(sort=False))
actual_response  predicted_response
0                0.0                   141
                 1.0                    59
1                0.0                   111
                 1.0                    89
Pythonで学ぶstatsmodelsによる回帰入門

混同行列の可視化

conf_matrix = mdl_recency.pred_table()

print(conf_matrix)
[[141.              59.]
 [111.              89.]] 
真陰性 偽陽性
偽陰性 真陽性
from statsmodels.graphics.mosaicplot
import mosaic

mosaic(conf_matrix)

離反 vs. 最新利用モデルの結果を示すモザイク図。真の離反と真の非離反が各200件のため、各列の幅は同じです。

Pythonで学ぶstatsmodelsによる回帰入門

正解率(Accuracy)

正解率は正しく予測した割合です。

$$ \text{accuracy} = \frac{TN + TP}{TN + FN + FP + TP} $$

 [[141.,  59.],
  [111.,  89.]]
TN = conf_matrix[0,0]
TP = conf_matrix[1,1]
FN = conf_matrix[1,0]
FP = conf_matrix[0,1]
acc = (TN + TP) / (TN + TP + FN + FP)
print(acc)
0.575
Pythonで学ぶstatsmodelsによる回帰入門

感度(Sensitivity)

感度は真陽性の割合です。

$$ \text{sensitivity} = \frac{TP}{FN + TP} $$

 [[141.,  59.],
  [111.,  89.]]
TN = conf_matrix[0,0]
TP = conf_matrix[1,1]
FN = conf_matrix[1,0]
FP = conf_matrix[0,1]
sens = TP / (FN + TP)
print(sens)
0.445
Pythonで学ぶstatsmodelsによる回帰入門

特異度(Specificity)

特異度は真陰性の割合です。

$$ \text{specificity} = \frac{TN}{TN + FP} $$

 [[141.,  59.],
  [111.,  89.]]
TN = conf_matrix[0,0]
TP = conf_matrix[1,1]
FN = conf_matrix[1,0]
FP = conf_matrix[0,1]
spec = TN / (TN + FP)
print(spec)
0.705
Pythonで学ぶstatsmodelsによる回帰入門

Ayo berlatih!

Pythonで学ぶstatsmodelsによる回帰入門

Preparing Video For Download...