เกณฑ์และเมทริกซ์ความสับสน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
สมมติว่าคุณมีโมเดลการถดถอยเชิงลอจิสติกสําหรับการตรวจจับอีเมลสแปมซึ่งคาดการณ์ค่าระหว่าง 0 ถึง 1 ซึ่งแสดงถึงแนวโน้มที่อีเมลหนึ่งๆ จะเป็นสแปม การคาดการณ์ 0.50 หมายถึงความน่าจะเป็น 50% ที่อีเมลจะเป็นจดหมายขยะ การคาดการณ์ 0.75 หมายถึงความน่าจะเป็น 75% ที่อีเมลจะเป็นจดหมายขยะ และอื่นๆ
คุณต้องการติดตั้งใช้งานรูปแบบนี้ในแอปพลิเคชันอีเมลเพื่อกรองจดหมายขยะไปยังโฟลเดอร์อีเมลแยกต่างหาก แต่คุณจะต้องแปลงเอาต์พุตตัวเลขดิบของโมเดล (เช่น 0.75
) ออกเป็น 2 หมวดหมู่ ได้แก่ "สแปม" หรือ "ไม่ใช่สแปม"
หากต้องการทําการเปลี่ยนรูปแบบนี้ ให้เลือกความน่าจะเป็นเกณฑ์ที่เรียกว่าเกณฑ์การจัดประเภท
จากนั้นระบบจะกําหนดตัวอย่างที่มีความน่าจะเป็นสูงกว่าเกณฑ์เป็นคลาสที่เป็นบวก ซึ่งเป็นคลาสที่คุณกําลังทดสอบ (ในที่นี้คือ spam
) และกำหนดตัวอย่างที่มีความน่าจะเป็นต่ำกว่าเป็นคลาสที่เป็นลบ ซึ่งเป็นคลาสทางเลือก (ในที่นี้คือ not spam
)
คลิกที่นี่เพื่อดูรายละเอียดเพิ่มเติมเกี่ยวกับเกณฑ์การจัดประเภท
คุณอาจสงสัยว่าจะเกิดอะไรขึ้นหากคะแนนที่คาดการณ์เท่ากับเกณฑ์การจัดประเภท (เช่น คะแนน 0.5 เมื่อเกณฑ์การจัดประเภทคือ 0.5 ด้วย) การจัดการสำหรับกรณีนี้ขึ้นอยู่กับการใช้งานที่เลือกสำหรับรูปแบบการจัดประเภท ไลบรารี Keras จะคาดการณ์คลาสเชิงลบหากคะแนนและเกณฑ์เท่ากัน แต่เครื่องมือ/เฟรมเวิร์กอื่นๆ อาจจัดการเคสนี้แตกต่างกัน
สมมติว่าโมเดลให้คะแนนอีเมลฉบับหนึ่งเป็น 0.99 ซึ่งคาดการณ์ว่าอีเมลดังกล่าวมีโอกาสเป็นจดหมายขยะ 99% และอีกฉบับหนึ่งเป็น 0.51 ซึ่งคาดการณ์ว่าอีเมลดังกล่าวมีโอกาสเป็นจดหมายขยะ 51% หากคุณตั้งค่าเกณฑ์การจัดประเภทเป็น 0.5 โมเดลจะจัดประเภททั้ง 2 อีเมลว่าเป็นจดหมายขยะ หากคุณตั้งค่าเกณฑ์เป็น 0.95 จะมีเพียงอีเมลที่มีคะแนน 0.99 เท่านั้นที่จัดว่าเป็นจดหมายขยะ
แม้ว่า 0.5 อาจดูเหมือนเกณฑ์ที่เข้าใจง่าย แต่ก็ไม่เหมาะในกรณีที่ต้นทุนของการแยกประเภทที่ไม่ถูกต้องประเภทหนึ่งสูงกว่าอีกประเภทหนึ่ง หรือในกรณีที่คลาสไม่สมดุล หากมีอีเมลเพียง 0.01% ที่เป็นจดหมายขยะ หรือหากการแยกอีเมลที่ถูกต้องไปยังโฟลเดอร์ที่ไม่ถูกต้องส่งผลเสียมากกว่าการปล่อยให้จดหมายขยะเข้ามาในกล่องจดหมาย การติดป้ายกำกับอีเมลที่โมเดลพิจารณาว่ามีแนวโน้มเป็นจดหมายขยะอย่างน้อย 50% เป็นจดหมายขยะจะทำให้เกิดผลลัพธ์ที่ไม่พึงประสงค์
เมตริกความสับสน
คะแนนความน่าจะเป็นไม่ใช่ความจริงหรือข้อมูลพื้นฐาน
ผลลัพธ์ที่เป็นไปได้ของเอาต์พุตแต่ละรายการจากตัวแยกประเภทแบบไบนารีมี 4 รายการ
สําหรับตัวอย่างตัวแยกประเภทสแปม หากคุณจัดวางข้อมูลที่เป็นความจริงเป็นคอลัมน์และการคาดการณ์ของโมเดลเป็นแถว ตารางต่อไปนี้ซึ่งเรียกว่าเมทริกซ์ความสับสนจะแสดงผลลัพธ์
|
ผลบวกจริง |
ลบจริง |
ผลบวกที่คาดการณ์ |
True Positive (TP): อีเมลที่เป็นสแปมที่จัดประเภทเป็นอีเมลสแปมอย่างถูกต้อง ข้อความเหล่านี้คือข้อความสแปมที่ระบบส่งไปยังโฟลเดอร์จดหมายขยะโดยอัตโนมัติ |
การตรวจพบที่ผิดพลาด (FP): อีเมลที่ไม่ใช่จดหมายขยะแต่ได้รับการจัดประเภทเป็นจดหมายขยะ อีเมลเหล่านี้เป็นอีเมลที่ถูกต้องซึ่งปรากฏในโฟลเดอร์จดหมายขยะ |
เชิงลบที่คาดการณ์ |
ผลลบลวง (FN): อีเมลสแปมที่จัดประเภทไม่ถูกต้องว่าเป็นไม่ใช่สแปม อีเมลเหล่านี้เป็นอีเมลขยะที่ไม่ได้ถูกตัวกรองจดหมายขยะจับได้และส่งไปยังกล่องจดหมาย |
True Negative (TN): อีเมลที่ไม่ใช่จดหมายขยะซึ่งจัดประเภทอย่างถูกต้องว่าไม่ใช่จดหมายขยะ
อีเมลเหล่านี้เป็นอีเมลที่ถูกต้องซึ่งส่งไปยังกล่องจดหมายโดยตรง |
โปรดทราบว่าผลรวมในแต่ละแถวแสดงผลบวกที่คาดการณ์ทั้งหมด (TP + FP) และผลลบที่คาดการณ์ทั้งหมด (FN + TN) โดยไม่คำนึงถึงความถูกต้อง ส่วนผลรวมในแต่ละคอลัมน์จะแสดงผลบวกจริงทั้งหมด (TP + FN) และผลลบจริงทั้งหมด (FP + TN) โดยไม่คำนึงถึงการจัดประเภทโมเดล
เมื่อผลรวมของรายการเชิงบวกจริงไม่ได้ใกล้เคียงกับผลรวมของรายการเชิงลบจริง แสดงว่าชุดข้อมูลไม่สมดุล อินสแตนซ์ของชุดข้อมูลที่ไม่สมดุลอาจเป็นชุดรูปภาพเมฆหลายพันรูป โดยเมฆประเภทที่หายากซึ่งคุณสนใจ เช่น เมฆรูปเกลียว ปรากฏเพียงไม่กี่ครั้ง
ผลของเกณฑ์ที่มีต่อผลบวกจริงและผลบวกลวง รวมถึงผลลบ
โดยปกติแล้ว เกณฑ์ที่แตกต่างกันจะส่งผลให้ผลบวกจริงและผลบวกลวง รวมถึงผลลบจริงและผลลบลวงมีจำนวนไม่เท่ากัน วิดีโอต่อไปนี้อธิบายสาเหตุของปัญหานี้
ลองเปลี่ยนเกณฑ์ด้วยตนเอง
วิดเจ็ตนี้มีชุดข้อมูลของของเล่น 3 ชุด ได้แก่
- แยก ซึ่งโดยทั่วไปแล้วตัวอย่างเชิงบวกและตัวอย่างเชิงลบจะแยกแยะกันได้ดี โดยตัวอย่างเชิงบวกส่วนใหญ่จะมีคะแนนสูงกว่าตัวอย่างเชิงลบ
- ไม่แยก ซึ่งตัวอย่างเชิงบวกจำนวนมากมีคะแนนต่ำกว่าตัวอย่างเชิงลบ และตัวอย่างเชิงลบจำนวนมากมีคะแนนสูงกว่าตัวอย่างเชิงบวก
- ไม่สมดุล มีตัวอย่างของคลาสเชิงบวกเพียงไม่กี่รายการ
ทดสอบความเข้าใจ
1. ลองจินตนาการถึงโมเดลการจัดประเภทฟิชชิงหรือมัลแวร์ซึ่งเว็บไซต์ฟิชชิงและมัลแวร์อยู่ในคลาสที่มีป้ายกำกับ 1 (จริง) และเว็บไซต์ที่ไม่เป็นอันตรายอยู่ในคลาสที่มีป้ายกำกับ 0 (เท็จ) โมเดลนี้จัดประเภทเว็บไซต์ที่ถูกต้องเป็นมัลแวร์อย่างไม่ถูกต้อง ฟีเจอร์นี้เรียกว่าอะไร
ผลบวกลวง
ตัวอย่างเชิงลบ (เว็บไซต์ที่ถูกต้อง) ได้รับการจัดประเภทอย่างไม่ถูกต้องเป็นตัวอย่างเชิงบวก (เว็บไซต์มัลแวร์)
ผลบวกจริง
ผลบวกแท้จริงคือเว็บไซต์ที่เป็นมัลแวร์ซึ่งจัดว่าเป็นมัลแวร์อย่างถูกต้อง
ผลลบลวง
ผลลัพธ์ลบที่ไม่ถูกต้องคือเว็บไซต์มัลแวร์ที่จัดประเภทอย่างไม่ถูกต้องว่าเป็นเว็บไซต์ที่ถูกต้อง
ผลลบจริง
ผลลัพธ์เชิงลบที่แท้จริงคือเว็บไซต์ที่ถูกกฎหมายซึ่งจัดประเภทอย่างถูกต้องว่าเป็นเว็บไซต์ที่ถูกกฎหมาย
2. โดยทั่วไป จะเกิดอะไรขึ้นกับจํานวนผลบวกลวงเมื่อเกณฑ์การจัดประเภทเพิ่มขึ้น แล้วผลบวกจริงล่ะ ลองใช้แถบเลื่อนด้านบน
ทั้งผลบวกจริงและผลบวกลวงจะลดลง
เมื่อเกณฑ์สูงขึ้น โมเดลมีแนวโน้มที่จะคาดการณ์ผลบวกโดยรวมน้อยลง ทั้งผลบวกจริงและผลบวกลวง ตัวแยกประเภทสแปมที่มีเกณฑ์ .9999 จะติดป้ายกำกับอีเมลว่าเป็นจดหมายขยะก็ต่อเมื่อพิจารณาแล้วว่าการแยกประเภทมีแนวโน้มอย่างน้อย 99.99% ซึ่งหมายความว่ามีแนวโน้มต่ำมากที่จะติดป้ายกำกับอีเมลที่ถูกต้องว่าเป็นจดหมายขยะ แต่ก็อาจพลาดจดหมายขยะจริงด้วย
ทั้งผลบวกจริงและผลบวกลวงจะเพิ่มขึ้น
ใช้แถบเลื่อนด้านบนเพื่อลองตั้งค่าเกณฑ์เป็น 0.1 แล้วลากเป็น 0.9 จะเกิดอะไรขึ้นกับจํานวนผลบวกลวงและผลบวกจริง
ผลบวกจริงเพิ่มขึ้น ผลบวกลวงลดลง
ใช้แถบเลื่อนด้านบนเพื่อลองตั้งค่าเกณฑ์เป็น 0.1 แล้วลากเป็น 0.9 จะเกิดอะไรขึ้นกับจํานวนผลบวกลวงและผลบวกจริง
3. โดยทั่วไป จะเกิดอะไรขึ้นกับจํานวนผลลบที่ผิดพลาดเมื่อเกณฑ์การจัดประเภทเพิ่มขึ้น แล้วผลลบจริงล่ะ ลองใช้แถบเลื่อนด้านบน
ทั้งผลลบจริงและผลลบเท็จจะเพิ่มขึ้น
เมื่อเกณฑ์เพิ่มขึ้น โมเดลมีแนวโน้มที่จะคาดการณ์ผลลัพธ์เชิงลบโดยรวมมากขึ้น ทั้งผลลัพธ์เชิงลบจริงและเท็จ เมื่อใช้เกณฑ์ที่สูงมาก อีเมลเกือบทั้งหมดทั้งที่เป็นจดหมายขยะและไม่ใช่จดหมายขยะจะจัดประเภทเป็นไม่ใช่จดหมายขยะ
ทั้งผลบวกลวงและผลลบลวงจะลดลง
ใช้แถบเลื่อนด้านบนเพื่อลองตั้งค่าเกณฑ์เป็น 0.1 แล้วลากเป็น 0.9 จะเกิดอะไรขึ้นกับจํานวนผลลบเท็จและผลลบจริง
ผลลบจริงเพิ่มขึ้น ผลลบลวงลดลง
ใช้แถบเลื่อนด้านบนเพื่อลองตั้งค่าเกณฑ์เป็น 0.1 แล้วลากเป็น 0.9 จะเกิดอะไรขึ้นกับจํานวนผลลบเท็จและผลลบจริง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-05-22 UTC
[null,null,["อัปเดตล่าสุด 2025-05-22 UTC"],[],[],null,["# Thresholds and the confusion matrix\n\nLet's say you have a logistic regression model for spam-email detection that\npredicts a value between 0 and 1, representing the probability that a given\nemail is spam. A prediction of 0.50 signifies a 50% likelihood that the email is\nspam, a prediction of 0.75 signifies a 75% likelihood that the email is spam,\nand so on.\n\nYou'd like to deploy this model in an email application to filter spam into\na separate mail folder. But to do so, you need to convert the model's raw\nnumerical output (e.g., `0.75`) into one of two categories: \"spam\" or \"not\nspam.\"\n\nTo make this conversion, you choose a threshold probability, called a\n[**classification threshold**](/machine-learning/glossary#classification-threshold).\nExamples with a probability above the threshold value are then assigned\nto the [**positive class**](/machine-learning/glossary#positive_class),\nthe class you are testing for (here, `spam`). Examples with a lower\nprobability are assigned to the [**negative class**](/machine-learning/glossary#negative_class),\nthe alternative class (here, `not spam`). \n\n**Click here for more details on the classification threshold**\n\nYou may be wondering: what happens if the predicted score is equal to\nthe classification threshold (for instance, a score of 0.5 where\nthe classification threshold is also 0.5)? Handling for this case\ndepends on the particular implementation chosen for the classification\nmodel. The [Keras](https://keras.io/)\nlibrary predicts the negative class if the score and threshold\nare equal, but other tools/frameworks may handle this case\ndifferently.\n\nSuppose the model scores one email as 0.99, predicting\nthat email has a 99% chance of being spam, and another email as\n0.51, predicting it has a 51% chance of being spam. If you set the\nclassification threshold to 0.5, the model will classify both emails as\nspam. If you set the threshold to 0.95, only the email scoring 0.99 will\nbe classified as spam.\n\nWhile 0.5 might seem like an intuitive threshold, it's not a good idea if the\ncost of one type of wrong classification is greater than the other, or if the\nclasses are imbalanced. If only 0.01% of emails are spam, or if misfiling\nlegitimate emails is worse than letting spam into the inbox,\nlabeling anything the model considers at least 50% likely to be spam\nas spam produces undesirable results.\n\nConfusion matrix\n----------------\n\nThe probability score is not reality, or\n[**ground truth**](/machine-learning/glossary#ground_truth).\nThere are four possible outcomes for each output from a binary classifier.\nFor the spam classifier example, if you lay out the ground truth as columns\nand the model's prediction as rows, the following table, called a\n[**confusion matrix**](/machine-learning/glossary#confusion_matrix), is the\nresult:\n\n| | Actual positive | Actual negative |\n| Predicted positive | **True positive (TP)**: A spam email correctly classified as a spam email. These are the spam messages automatically sent to the spam folder. | **False positive (FP)**: A not-spam email misclassified as spam. These are the legitimate emails that wind up in the spam folder. |\n| Predicted negative | **False negative (FN)**: A spam email misclassified as not-spam. These are spam emails that aren't caught by the spam filter and make their way into the inbox. | **True negative (TN)**: A not-spam email correctly classified as not-spam. These are the legitimate emails that are sent directly to the inbox. |\n|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|\n\nNotice that the total in each row gives all predicted positives (TP + FP) and\nall predicted negatives (FN + TN), regardless of validity. The total in each\ncolumn, meanwhile, gives all real positives (TP + FN) and all real negatives\n(FP + TN) regardless of model classification.\n\nWhen the total of actual positives is not close to the total of actual\nnegatives, the dataset is\n[**imbalanced**](/machine-learning/glossary#class_imbalanced_data_set). An instance\nof an imbalanced dataset might be a set of thousands of photos of clouds, where\nthe rare cloud type you are interested in, say, volutus clouds, only appears\na few times.\n\nEffect of threshold on true and false positives and negatives\n-------------------------------------------------------------\n\nDifferent thresholds usually result in different numbers of true and false\npositives and true and false negatives. The following video explains why this is\nthe case. \n\nTry changing the threshold yourself.\n\nThis widget includes three toy datasets:\n\n- **Separated**, where positive examples and negative examples are generally well differentiated, with most positive examples having higher scores than negative examples.\n- **Unseparated**, where many positive examples have lower scores than negative examples, and many negative examples have higher scores than positive examples.\n- **Imbalanced**, containing only a few examples of the positive class.\n\n### Check your understanding\n\n1. Imagine a phishing or malware classification model where phishing and malware websites are in the class labeled **1** (true) and harmless websites are in the class labeled **0** (false). This model mistakenly classifies a legitimate website as malware. What is this called? \nA false positive \nA negative example (legitimate site) has been wrongly classified as a positive example (malware site). \nA true positive \nA true positive would be a malware site correctly classified as malware. \nA false negative \nA false negative would be a malware site incorrectly classified as a legitimate site. \nA true negative \nA true negative would be a legitimate site correctly classified as a legitimate site. \n2. In general, what happens to the number of false positives when the classification threshold increases? What about true positives? Experiment with the slider above. \nBoth true and false positives decrease. \nAs the threshold increases, the model will likely predict fewer positives overall, both true and false. A spam classifier with a threshold of .9999 will only label an email as spam if it considers the classification to be at least 99.99% likely, which means it is highly unlikely to mislabel a legitimate email, but also likely to miss actual spam email. \nBoth true and false positives increase. \nUsing the slider above, try setting the threshold to 0.1, then dragging it to 0.9. What happens to the number of false positives and true positives? \nTrue positives increase. False positives decrease. \nUsing the slider above, try setting the threshold to 0.1, then dragging it to 0.9. What happens to the number of false positives and true positives? \n3. In general, what happens to the number of false negatives when the classification threshold increases? What about true negatives? Experiment with the slider above. \nBoth true and false negatives increase. \nAs the threshold increases, the model will likely predict more negatives overall, both true and false. At a very high threshold, almost all emails, both spam and not-spam, will be classified as not-spam. \nBoth true and false negatives decrease. \nUsing the slider above, try setting the threshold to 0.1, then dragging it to 0.9. What happens to the number of false negatives and true negatives? \nTrue negatives increase. False negatives decrease. \nUsing the slider above, try setting the threshold to 0.1, then dragging it to 0.9. What happens to the number of false negatives and true negatives?\n| **Key terms:**\n|\n| - [Binary classification](/machine-learning/glossary#binary-classification)\n| - [Class-imbalanced dataset](/machine-learning/glossary#class_imbalanced_data_set)\n| - [Classification threshold](/machine-learning/glossary#classification-threshold)\n| - [Confusion matrix](/machine-learning/glossary#confusion_matrix)\n| - [Ground truth](/machine-learning/glossary#ground_truth)\n| - [Negative class](/machine-learning/glossary#negative_class)\n- [Positive class](/machine-learning/glossary#positive_class) \n[Help Center](https://support.google.com/machinelearningeducation)"]]