การวัดความพอดีของ Logistic Regression

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Maarten Van den Broeck

Content Developer at DataCamp

ผลลัพธ์ทั้งสี่แบบ

ทำนายว่าเป็นเท็จ ทำนายว่าเป็นจริง
ค่าจริงเป็นเท็จ ถูกต้อง false positive
ค่าจริงเป็นจริง false negative ถูกต้อง
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Confusion Matrix: จำนวนของผลลัพธ์

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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

การแสดงภาพ Confusion Matrix

conf_matrix = mdl_recency.pred_table()

print(conf_matrix)
[[141.              59.]
 [111.              89.]] 
true negative false positive
false negative true positive
from statsmodels.graphics.mosaicplot
import mosaic

mosaic(conf_matrix)

กราฟ Mosaic แสดงผลลัพธ์ของโมเดล churn กับ recency มีข้อมูล 200 รายการทั้งกลุ่มที่ churn จริงและไม่ churn จริง ทำให้แต่ละคอลัมน์มีความกว้างเท่ากัน

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Sensitivity

Sensitivity คือสัดส่วนของ true positive

$$ \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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Specificity

Specificity คือสัดส่วนของ true negative

$$ \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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

มาฝึกกันเถอะ!

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Preparing Video For Download...