[null,null,["最后更新时间 (UTC):2025-05-23。"],[],[],null,["# Classification: ROC and AUC\n\nThe previous section presented a set of model metrics, all calculated at a\nsingle classification threshold value. But if you want to evaluate a\nmodel's quality across all possible thresholds, you need different tools.\n\nReceiver-operating characteristic curve (ROC)\n---------------------------------------------\n\nThe [**ROC curve**](/machine-learning/glossary#roc-receiver-operating-characteristic-curve)\nis a visual representation of model performance across all thresholds.\nThe long version of the name, receiver operating characteristic, is a holdover\nfrom WWII radar detection.\n\nThe ROC curve is drawn by calculating the true positive rate (TPR)\nand false positive rate (FPR) at every possible threshold (in practice, at\nselected intervals), then graphing TPR over FPR. A perfect model,\nwhich at some threshold has a TPR of 1.0 and a FPR of 0.0, can\nbe represented by either a point at\n(0, 1) if all other thresholds are ignored, or by the following:\n**Figure 1.** ROC and AUC of a hypothetical perfect model.\n\nArea under the curve (AUC)\n--------------------------\n\nThe [**area under the ROC curve (AUC)**](/machine-learning/glossary#AUC)\nrepresents the probability that the model,\nif given a randomly chosen positive and negative example, will rank the\npositive higher than the negative.\n\nThe perfect model above, containing a square with sides of length 1, has an\narea under the curve (AUC) of 1.0. This means there is a 100% probability that\nthe model will correctly rank a randomly chosen positive example higher than a\nrandomly chosen negative example. In other words, looking at the spread of\ndata points below, AUC gives the probability that the model will place a\nrandomly chosen square to the right of a randomly chosen circle, independent of\nwhere the threshold is set.\n**Figure 2.** A spread of predictions for a binary classification model. AUC is the chance a randomly chosen square is positioned to the right of a randomly chosen circle.\n\nIn more concrete terms, a spam classifier with AUC\nof 1.0 always assigns a random spam email a higher probability of being\nspam than a random legitimate email. The actual classification of each\nemail depends on the threshold that you choose.\n\nFor a binary classifier, a model that does exactly as well as random guesses or\ncoin flips has a ROC that is a diagonal line from (0,0) to (1,1). The AUC is\n0.5, representing a 50% probability of correctly ranking a random positive and\nnegative example.\n\nIn the spam classifier example, a spam classifier with AUC of 0.5 assigns\na random spam email a higher probability of being spam than a random\nlegitimate email only half the time.\n**Figure 3.** ROC and AUC of completely random guesses. \n\n#### (Optional, advanced) Precision-recall curve\n\nAUC and ROC work well for comparing models when the dataset is roughly\nbalanced between classes. When the dataset is imbalanced, precision-recall\ncurves (PRCs) and the area under those curves may offer a better comparative\nvisualization of model performance. Precision-recall curves are created by\nplotting precision on the y-axis and recall on the x-axis across all\nthresholds.\n\nAUC and ROC for choosing model and threshold\n--------------------------------------------\n\nAUC is a useful measure for comparing the performance of two different models,\nas long as the dataset is roughly balanced. The model with greater area under\nthe curve is generally the better one.\n**Figure 4.** ROC and AUC of two hypothetical models. The curve on the right, with a greater AUC, represents the better of the two models.\n\nThe points on a ROC curve closest to (0,1) represent a range of the\nbest-performing thresholds for the given model. As discussed in the\n[Thresholds](/machine-learning/crash-course/classification/thresholding),\n[Confusion matrix](/machine-learning/crash-course/classification/thresholding#confusion_matrix)\nand\n[Choice of metric and tradeoffs](/machine-learning/crash-course/classification/accuracy-precision-recall#choice_of_metric_and_tradeoffs)\nsections, the threshold you choose depends on which metric is most important to\nthe specific use case. Consider the points A, B, and C in the following\ndiagram, each representing a threshold:\n**Figure 5.** Three labeled points representing thresholds.\n\nIf false positives (false alarms) are highly costly, it may make sense to\nchoose a threshold that gives a lower FPR, like the one at point A, even if TPR\nis reduced. Conversely, if false positives are cheap and false negatives\n(missed true positives) highly costly, the threshold for point C, which\nmaximizes TPR, may be preferable. If the costs are roughly equivalent, point B\nmay offer the best balance between TPR and FPR.\n\nHere is the ROC curve for the data we have seen before:\n\nExercise: Check your understanding\n----------------------------------\n\nIn practice, ROC curves are much less regular than the illustrations given above. Which of the following models, represented by their ROC curve and AUC, has the best performance? \nThis model has the highest AUC, which corresponds with the best performance. \nWhich of the following models performs worse than chance? \nThis model has an AUC lower than 0.5, which means it performs worse than chance. \nThis model performs slightly better than chance. \nThis model performs the same as chance. \nThis is a hypothetical perfect classifier. \n\n#### (Optional, advanced) Bonus question\n\nWhich of the following changes can be made to the worse-than-chance model in the previous question to cause it to perform better than chance? \nReverse the predictions, so predictions of **1** become **0** , and predictions of **0** become **1**. \nIf a binary classifier reliably puts examples in the wrong classes more often than chance, switching the class label immediately makes its predictions better than chance without having to retrain the model. \nHave it always predict the negative class. \nThis may or may not improve performance above chance. Also, as discussed in the [Accuracy](/machine-learning/crash-course/classification/accuracy-precision-recall#accuracy) section, this isn't a useful model. \nHave it always predict the positive class. \nThis may or may not improve performance above chance. Also, as discussed in the [Accuracy](/machine-learning/crash-course/classification/accuracy-precision-recall#accuracy) section, this isn't a useful model.\n\n\u003cbr /\u003e\n\nImagine a situation where it's better to allow some spam to reach the\ninbox than to send a business-critical email to the spam folder. You've\ntrained a spam classifier for this situation where the positive class is\nspam and the negative class is not-spam.\nWhich of the following points\non the ROC curve for your classifier is preferable? \nPoint A \nIn this use case, it's better to minimize false positives, even if true positives also decrease. \nPoint B \nThis threshold balances true and false positives. \nPoint C \nThis threshold maximizes true positives (flags more spam) at a cost of more false positives (more legitimate emails flagged as spam).\n| **Key terms:**\n|\n| - [Area under the ROC curve (AUC)](/machine-learning/glossary#AUC)\n- [ROC curve](/machine-learning/glossary#roc-receiver-operating-characteristic-curve) \n[Help Center](https://support.google.com/machinelearningeducation)"]]