{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Evaluating models\n", "\n", "On the [cross validation page](03c_ModelEval), we said you choose the model that produces the \"lowest average validation error\" when you do your cross validation tests.\n", "\n", "But wait! How do you measure accuracy of predictions or the model's fit? \n", "\n", "You need to pick a \"scoring metric\"! \n", "\n", "Below, I list the most common statistics to report and consider. [scikit-learn](https://scikit-learn.org/stable/modules/model_evaluation.html)'s documentation lists the full menu.\n", "\n", "````{margin}\n", "\n", "If you're a visual person, the figures below might help. \n", "\n", "In each figure below, a machine learning classifier calls the data points inside the circle positive, the left half are true positives, and the right half are true negatives.\n", "\n", "\n", "```{image} https://upload.wikimedia.org/wikipedia/commons/e/e7/Sensitivity_and_specificity.svg\n", ":alt: Sensitivity_and_specificity\n", ":width: 300px\n", "```\n", "```{image} https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/Precisionrecall.svg/800px-Precisionrecall.svg.png\n", ":alt: Precisionrecall\n", ":width: 300px\n", "```\n", "\n", "````\n", "\n", "\n", "| Measurement name | Measures | Useful for predicting | \n", "| :--- | :--- | :--- |\n", "| R2 | [The fraction of variation in the y variable the model explains](02c_goodnessOfFit) | Continuous variables | \n", "| Root Mean Squared Error | _Size of average squared error. 0 is impossible._ | Continuous variable | \n", "| Mean Absolute Error | _Square errors penalize big prediction errors more. So MAE based models penalize these errors less. Intuitively, this means outliers influence the model less._ | Continuous variable | \n", "| True Positive Rate (TPR) aka **\"Sensitivity\"** aka **\"Recall\"** = 1-FNR | _What fraction of the true positives do you call positive?_ | Binary variables | \n", "| True Negative Rate (TNR) aka **\"Specificity\"** = 1-FPR | _What fraction of the true negatives do you call negative?_ | Binary variables | \n", "| **\"Precision\"** | _How precise are your positive labels?
What fraction of what you labeled positive are truly positive?_ | Binary variables | \n", "| **\"Accuracy\"** | _What fraction of predictions made are correct?_ | Binary variables | \n", "| False Positive Rate (FPR) = 1-TNR | _What fraction of the true negatives do you call positive?_ | Binary variables | \n", "| False Negative Rate (FNR) = 1-TPR | _What fraction of the true positives do you call negative?_ | Binary variables | \n", "| \"F-score\" or \"F1\" | _The harmonic mean of recall and precision. Intuition: Are you detecting positive cases, and correctly?_ | Binary variables | \n", "\n", "Here is a great trick from [Sean Taylor](https://twitter.com/seanjtaylor/status/1385776334048088065?s=27) for remembering which errors are type 1 and type 2 (II):\n", "\n", "```{image} img/type1and2errors.jpg\n", ":alt: Error_types\n", ":width: 400px\n", "```\n", "\n", "

Which measurement should you use?

\n", "\n", "It depends! If you have a continuous variable, use R2, MAE, or RMSE.\n", "\n", "If you have a classification problem (the \"binary variable\" rows in the table above), you have to think about your problem. [I REALLY like the discussion on this page from DS100.](https://www.textbook.ds100.org/ch/23/classification_sensitivity_specificity.html)\n", "- In medical tests more broadly, false negatives are really bad - if someone has cancer, don't tell them they are cancer free. Thus, medical tests tend to minimize false negatives.\n", "- Minimizing false negatives is the same as maximizing the true positives, aka the \"sensitivity\" of a test, since $FNR=1-TPR$. Better covid tests have higher specificity, because if someone has covid, we want to know that so we can require a quarantine.\n", "- In the legal field, false positives (imprisoning an innocent person) are considered worse than false negatives.\n", "- Identifying terrorists? You might want to maximize the detection rate (\"recall\"). Of course, simply saying \"everyone is a terrorist\" is guaranteed to generate a recall rate of 100%! But this rule would have a precision of about 0%! So you also should think about \"precision\" too. Maybe F1 is the metric for you. \n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }