量化羅吉斯回歸的擬合度

使用 Python 中的 statsmodels 進行回歸入門

Maarten Van den Broeck

Content Developer at DataCamp

四種結果

預測為 false 預測為 true
實際為 false 正確 偽陽性
實際為 true 偽陰性 正確
使用 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)

流失對最近互動模型結果的馬賽克圖。實際流失與未流失各有 200 筆觀測,因此每一欄寬度相同。

使用 Python 中的 statsmodels 進行回歸入門

Accuracy(正確率)

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(靈敏度)

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(特異度)

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 進行回歸入門

一起來練習吧!

使用 Python 中的 statsmodels 進行回歸入門

Preparing Video For Download...