{"id":88689,"date":"2025-10-04T15:43:35","date_gmt":"2025-10-04T10:13:35","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=88689"},"modified":"2026-09-19T12:34:39","modified_gmt":"2026-09-19T07:04:39","slug":"sklearn-metrics-in-machine-learning","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/sklearn-metrics-in-machine-learning\/","title":{"rendered":"Sklearn Metrics Explained: The Complete Guide with Code Examples (2026)"},"content":{"rendered":"\n<p>Sklearn Metrics is a module in the scikit-learn library that checks how well a machine learning model is performing. It compares the model&#8217;s predictions with the actual results and gives you a clear score.<\/p>\n\n\n\n<p>Without these metrics, you&#8217;re just guessing whether your model is good. Sklearn Metrics gives you the exact numbers you need, whether you&#8217;re working on classification, regression, or clustering.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>Sklearn Metrics is a scikit-learn module used to check how well your ML model performs.<\/li>\n\n\n\n<li>For classification, common Sklearn Metrics include accuracy, precision, recall, F1 score, and ROC-AUC.<\/li>\n\n\n\n<li>For regression, MSE, RMSE, MAE, and R\u00b2 Score help measure prediction error.<\/li>\n\n\n\n<li>Imbalanced data? Skip plain accuracy \u2014 use F1 Score or ROC-AUC instead.<\/li>\n\n\n\n<li>Choosing the right Sklearn Metrics depends on your problem, not just the model you&#8217;re using.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n  <strong style=\"font-size: 22px; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/strong> <br \/><br \/>\n  <span>\n    scikit-learn\u2019s <strong style=\"color: #110053;\">matthews_corrcoef (MCC)<\/strong> is a highly reliable metric for binary classification, especially with <strong style=\"color: #110053;\">imbalanced datasets<\/strong>, as it considers all four confusion-matrix outcomes: TP, TN, FP, and FN.\n  <\/span>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Sklearn Metrics?<\/strong><\/h2>\n\n\n\n<p><a href=\"http:\/\/Scikit-learn (aka sklearn)\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Sklearn<\/a> Metrics is a module inside the scikit-learn library, used to check how good a <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">machine learning<\/a> model&#8217;s predictions are. It gives you ready-made functions to measure accuracy, error, and overall model performance \u2014 whether you&#8217;re working on classification, regression, or clustering problems. <\/p>\n\n\n\n<p>Instead of writing your own evaluation code from scratch, you simply import the metric you need and apply it to your model&#8217;s results.<\/p>\n\n\n\n<p>You\u2019ll see two general categories:<\/p>\n\n\n\n<ul>\n<li><strong>Classification metrics<\/strong> (for categorical \/ discrete targets)<br><\/li>\n\n\n\n<li><strong>Regression metrics<\/strong> (for continuous numeric targets)<\/li>\n<\/ul>\n\n\n\n<p>Some metrics are \u201cscores\u201d (higher is better), others are \u201closses\u201d (lower is better). In scikit-learn\u2019s design, functions ending with _score generally return values you want to maximize, while ones with _error or _loss are minimization metrics.<\/p>\n\n\n\n<p>As you apply these, always ask:<\/p>\n\n\n\n<ul>\n<li>What errors are more critical in your domain? (false positives vs false negatives)<\/li>\n\n\n\n<li>Is your data imbalanced?<\/li>\n\n\n\n<li>Do you need a single summary metric or multiple perspectives?<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Master model evaluation and 25+ other ML skills with HCL GUVI&#8217;s Intel &amp; IITM Pravartak Certified <\/em><\/strong><em><a href=\"https:\/\/www.guvi.in\/mlp\/artificial-intelligence-and-machine-learning?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=sklearn-metrics-in-machine-learning\" target=\"_blank\" rel=\"noreferrer noopener\">AI\/ML Course<\/a><\/em><strong><em>. Build 20+ real projects and land your AI career with 1000+ hiring partners backing you. Enroll today and transform your career from data curious to AI-ready!<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Classification Metrics in Sklearn Metrics<\/strong><\/h2>\n\n\n\n<p>When your machine learning model predicts categories, like spam vs. not spam, or cat vs. dog vs. rabbit, you\u2019re dealing with <strong><a href=\"https:\/\/www.guvi.in\/blog\/classification-in-data-science\/\" target=\"_blank\" rel=\"noreferrer noopener\">classification<\/a><\/strong>. The burning question then is: how well is my model classifying things?<\/p>\n\n\n\n<p>That\u2019s exactly where <strong>classification metrics<\/strong> come in. They don\u2019t just give you a single score; they provide different \u201clenses\u201d to evaluate your model\u2019s performance. And trust me, choosing the right lens is critical, because a single metric (like accuracy) can easily fool you.<\/p>\n\n\n\n<p>Let\u2019s break down the key classification metrics that Sklearn Metrics provides.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Confusion Matrix: The Big Picture<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-1200x630.webp\" alt=\"\" class=\"wp-image-135228\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/2-6-2.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Think of the <strong>confusion matrix<\/strong> as the \u201ctruth table\u201d of classification. It lays out predictions vs. actual labels so you can literally <em>see<\/em> where the model is getting things right or wrong.<\/p>\n\n\n\n<p>For binary classification (say, positive vs. negative):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Predicted \\ Actual<\/strong><\/td><td><strong>Positive<\/strong><\/td><td><strong>Negative<\/strong><\/td><\/tr><tr><td><strong>Positive<\/strong><\/td><td>TP<\/td><td>FP<\/td><\/tr><tr><td><strong>Negative<\/strong><\/td><td>FN<\/td><td>TN<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Confusion Matrix<\/strong><\/figcaption><\/figure>\n\n\n\n<ul>\n<li><strong>TP (True Positives)<\/strong>: correctly predicted positives<br><\/li>\n\n\n\n<li><strong>FP (False Positives)<\/strong>: incorrectly predicted positives<br><\/li>\n\n\n\n<li><strong>FN (False Negatives)<\/strong>: incorrectly predicted negatives<br><\/li>\n\n\n\n<li><strong>TN (True Negatives)<\/strong>: correctly predicted negatives<\/li>\n<\/ul>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import confusion_matrix\n\ncm = confusion_matrix(y_true, y_pred)\n\nprint(cm)<\/code><\/pre>\n\n\n\n<p>Why it matters: The confusion matrix isn\u2019t a final metric, but it\u2019s the <em>foundation<\/em> for everything else &#8211; precision, recall, F1, MCC, and more all come out of it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Accuracy Score: The First Metric Everyone Checks<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-1200x630.webp\" alt=\"\" class=\"wp-image-135229\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/3-5-1.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><strong>Accuracy<\/strong> is the simplest metric: the fraction of correct predictions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"522\" height=\"107\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/1.png\" alt=\"Accuracy Score\" class=\"wp-image-88750\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/1.png 522w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/1-300x61.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/1-150x31.png 150w\" sizes=\"(max-width: 522px) 100vw, 522px\" title=\"\"><\/figure>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import accuracy_score\n\naccuracy_score(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>It\u2019s quick and intuitive &#8211; \u201cwhat percentage did I get right?\u201d<\/p>\n\n\n\n<p><strong>But here\u2019s the catch<\/strong>: accuracy can be misleading. Imagine a dataset where 95% of emails are \u201cnot spam.\u201d A model that predicts everything as \u201cnot spam\u201d gets 95% accuracy, but it\u2019s actually useless at finding spam. That\u2019s why you need other metrics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Precision: How Many of Your Positives Were Correct?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-1200x630.webp\" alt=\"\" class=\"wp-image-135231\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/4-4.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Precision answers: <em>Of all the items I labeled as positive, how many were truly positive?<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"402\" height=\"93\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/2.png\" alt=\"Precision\" class=\"wp-image-88751\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/2.png 402w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/2-300x69.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/2-150x35.png 150w\" sizes=\"(max-width: 402px) 100vw, 402px\" title=\"\"><\/figure>\n\n\n\n<ul>\n<li>High precision = your positive predictions are trustworthy<br><\/li>\n\n\n\n<li>Low precision = you\u2019re calling too many false alarms<\/li>\n<\/ul>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import precision_score\n\nprecision_score(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>Use precision when the cost of a false positive is high.<br>Example: Predicting whether a tumor is malignant, you don\u2019t want to incorrectly scare patients with false positives.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Recall: How Many Positives Did You Actually Find?<\/strong><\/h3>\n\n\n\n<p>Recall answers: <em>Of all the actual positives, how many did I catch?<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"385\" height=\"102\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/3.png\" alt=\"Recall\" class=\"wp-image-88752\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/3.png 385w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/3-300x79.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/3-150x40.png 150w\" sizes=\"(max-width: 385px) 100vw, 385px\" title=\"\"><\/figure>\n\n\n\n<ul>\n<li>High recall = you\u2019re catching most positives<br><\/li>\n\n\n\n<li>Low recall = you\u2019re missing a lot<\/li>\n<\/ul>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import recall_score\n\nrecall_score(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>Use recall when missing positives is costly.<br>Example: In fraud detection, you\u2019d rather flag too many transactions (false positives) than miss actual fraud (false negatives).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. F1 Score: The Balance Between Precision and Recall<\/strong><\/h3>\n\n\n\n<p>Sometimes you don\u2019t want to maximize just precision or just recall. You want a <strong>balance<\/strong>. That\u2019s where <strong>F1 score<\/strong> comes in.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"518\" height=\"121\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/4.png\" alt=\" F1 Score\" class=\"wp-image-88753\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/4.png 518w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/4-300x70.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/4-150x35.png 150w\" sizes=\"(max-width: 518px) 100vw, 518px\" title=\"\"><\/figure>\n\n\n\n<p>It\u2019s the <strong>harmonic mean<\/strong> of precision and recall, which means it penalizes extreme imbalances.<\/p>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import f1_score\n\nf1_score(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>Why harmonic mean? Because if precision is high but recall is near zero, F1 drops sharply \u2014 telling you your model isn\u2019t balanced.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. ROC Curve and AUC: How Well Can You Rank?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-1200x630.webp\" alt=\"\" class=\"wp-image-135232\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/5-3.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Sometimes you want to know how well your model distinguishes between classes across all possible thresholds. That\u2019s what the <strong>ROC curve<\/strong> and <strong>AUC (Area Under Curve)<\/strong> measure.<\/p>\n\n\n\n<ul>\n<li><strong>ROC curve<\/strong> plots True Positive Rate (Recall) vs. False Positive Rate at different thresholds.<br><\/li>\n\n\n\n<li><strong>AUC<\/strong> summarizes it: 1.0 = perfect, 0.5 = random guessing.<\/li>\n<\/ul>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import roc_auc_score, roc_curve\n\nauc = roc_auc_score(y_true, y_pred_proba)\n\nfpr, tpr, thresholds = roc_curve(y_true, y_pred_proba)<\/code><\/pre>\n\n\n\n<p>Use ROC-AUC when you want to evaluate ranking quality, not just a fixed decision boundary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Matthews Correlation Coefficient (MCC): The Balanced One<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-1200x630.webp\" alt=\"\" class=\"wp-image-135233\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/6-3.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Accuracy is misleading with an imbalance. Precision and recall focus only on positives. F1 helps balance them, but MCC is often considered the <strong>most balanced metric<\/strong> for binary classification.<\/p>\n\n\n\n<p>It uses all four confusion matrix terms and outputs a number between \u20131 and +1:<\/p>\n\n\n\n<ul>\n<li>+1 = perfect prediction<br><\/li>\n\n\n\n<li>0 = random<br><\/li>\n\n\n\n<li>\u20131 = total opposite<\/li>\n<\/ul>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import matthews_corrcoef\n\nmatthews_corrcoef(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>Use MCC when classes are very imbalanced, like rare disease detection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Classification Report: The All-in-One View<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-1200x630.webp\" alt=\"\" class=\"wp-image-135234\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/7-3.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>If you\u2019re working with multiple classes, printing each metric one by one is painful. That\u2019s where the <strong>classification report<\/strong> shines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import classification_report\n\nprint(classification_report(y_true, y_pred))<\/code><\/pre>\n\n\n\n<p>It gives you precision, recall, F1, and support for each class in a single table. Perfect for quick summaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Quick Recap of Classification (Cheat Sheet)<\/strong><\/h3>\n\n\n\n<ul>\n<li><strong>Confusion matrix<\/strong>: the raw truth table<br><\/li>\n\n\n\n<li><strong>Accuracy<\/strong>: overall correctness (be careful with imbalance)<br><\/li>\n\n\n\n<li><strong>Precision<\/strong>: of predicted positives, how many were right?<br><\/li>\n\n\n\n<li><strong>Recall<\/strong>: of actual positives, how many did I catch?<br><\/li>\n\n\n\n<li><strong>F1 score<\/strong>: balance of precision and recall<br><\/li>\n\n\n\n<li><strong>ROC-AUC<\/strong>: how well the model separates classes across thresholds<br><\/li>\n\n\n\n<li><strong>MCC<\/strong>: balanced summary, especially for imbalanced classes<br><\/li>\n\n\n\n<li><strong>Classification report<\/strong>: your one-stop shop summary<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Regression Metrics in Sklearn Metrics<\/strong><\/h2>\n\n\n\n<p>When your model predicts continuous values, house prices, temperatures, sales forecasts, you name it, you\u2019re in <strong><a href=\"https:\/\/www.guvi.in\/blog\/types-of-regression-in-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">regression<\/a> land<\/strong>. Evaluating these models is a bit different from classification: instead of asking <em>\u201cdid I get it right?\u201d<\/em> you ask <em>\u201chow close was I?\u201d<\/em>.<\/p>\n\n\n\n<p>The Sklearn Metrics module has several ways to measure this closeness, and each one emphasizes a slightly different perspective on error. Let\u2019s go through them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Mean Squared Error (MSE) and Root Mean Squared Error (RMSE)<\/strong><\/h3>\n\n\n\n<p><strong>MSE<\/strong> is probably the most commonly reported metric in regression. It calculates the average of squared differences between actual and predicted values:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"417\" height=\"121\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/5.png\" alt=\"Mean Squared Error (MSE) \" class=\"wp-image-88754\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/5.png 417w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/5-300x87.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/5-150x44.png 150w\" sizes=\"(max-width: 417px) 100vw, 417px\" title=\"\"><\/figure>\n\n\n\n<p>Why squared? Because it punishes big errors more than small ones. Predict a house price off by 100,000? The square of that error really hurts your score.<\/p>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import mean_squared_error\n\nmse = mean_squared_error(y_true, y_pred)&nbsp; &nbsp; &nbsp; &nbsp; # MSE\n\nrmse = mean_squared_error(y_true, y_pred, squared=False)&nbsp; # RMSE<\/code><\/pre>\n\n\n\n<p><strong>RMSE<\/strong> just takes the square root of MSE so your errors are in the same units as your target (e.g., \u201cdollars\u201d instead of \u201cdollars squared\u201d).<\/p>\n\n\n\n<p>Use MSE\/RMSE when you want to highlight large mistakes more strongly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Mean Absolute Error (MAE)<\/strong><\/h3>\n\n\n\n<p>Sometimes you don\u2019t want to punish big mistakes disproportionately. <strong>MAE<\/strong> is the average of absolute differences:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"380\" height=\"112\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/6.png\" alt=\"Mean Absolute Error (MAE)\" class=\"wp-image-88755\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/6.png 380w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/6-300x88.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/6-150x44.png 150w\" sizes=\"(max-width: 380px) 100vw, 380px\" title=\"\"><\/figure>\n\n\n\n<p>It\u2019s more \u201cforgiving\u201d of outliers.<\/p>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import mean_absolute_error\n\nmae = mean_absolute_error(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>Think of MAE as answering: <em>on average, how far off am I?<\/em><\/p>\n\n\n\n<p>If you\u2019re building a forecasting tool for delivery times, MAE gives you a sense of the \u201ctypical\u201d miss.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. R\u00b2 Score (Coefficient of Determination)<\/strong><\/h3>\n\n\n\n<p><strong>R\u00b2<\/strong> measures how much of the variation in your target variable your model explains compared to a simple baseline (always predicting the mean).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"387\" height=\"102\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/7.png\" alt=\"R\u00b2 Score\" class=\"wp-image-88756\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/7.png 387w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/7-300x79.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/7-150x40.png 150w\" sizes=\"(max-width: 387px) 100vw, 387px\" title=\"\"><\/figure>\n\n\n\n<p>In sklearn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import r2_score\nr2 = r2_score(y_true, y_pred)<\/code><\/pre>\n\n\n\n<ul>\n<li>R\u00b2 = 1 \u2192 perfect fit<br><\/li>\n\n\n\n<li>R\u00b2 = 0 \u2192 no better than predicting the mean<br><\/li>\n\n\n\n<li>R\u00b2 &lt; 0 \u2192 worse than predicting the mean (ouch!)<br><\/li>\n<\/ul>\n\n\n\n<p>It\u2019s a great \u201cquick diagnostic,\u201d but don\u2019t use it alone, a high R\u00b2 doesn\u2019t always mean your model is <em>good<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Median Absolute Error<\/strong><\/h3>\n\n\n\n<p>Instead of the mean, it uses the <strong>median<\/strong> of absolute errors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import median_absolute_error\n\nmedae = median_absolute_error(y_true, y_pred)<\/code><\/pre>\n\n\n\n<p>This is robust to outliers, one giant error won\u2019t dominate the metric.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Mean Squared Logarithmic Error (MSLE)<\/strong><\/h3>\n\n\n\n<p>MSLE is designed for targets that span several orders of magnitude. Instead of comparing absolute differences, it compares <strong>log-transformed values<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import mean_squared_log_error\n\nmsle = mean_squared_log_error(y_true, y_pred)<\/code><\/pre>\n\n\n\n<ul>\n<li>It penalizes underestimates more than overestimates.<br><\/li>\n\n\n\n<li>Perfect for problems where relative error matters more than absolute error (e.g., predicting population growth where numbers vary widely).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Putting Metrics to Work: Examples &amp; Best Practices<\/strong><\/h2>\n\n\n\n<p>Okay, so you\u2019ve got this toolbox of metrics. How do you actually use them? Here\u2019s how I recommend approaching it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Predicting House Prices<\/strong><\/h3>\n\n\n\n<p>Say you\u2019ve trained a regression model to predict house prices.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n\ny_true = &#91;300000, 150000, 200000, 350000, 500000]\n\ny_pred = &#91;310000, 140000, 220000, 330000, 490000]\n\nmse = mean_squared_error(y_true, y_pred)\n\nmae = mean_absolute_error(y_true, y_pred)\n\nr2 = r2_score(y_true, y_pred)\n\nprint(f\"MSE: {mse:.2f}\")\n\nprint(f\"MAE: {mae:.2f}\")\n\nprint(f\"R\u00b2: {r2:.2f}\")<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>MSE<\/strong> tells you how badly your model punishes those $20,000 mistakes.<br><\/li>\n\n\n\n<li><strong>MAE<\/strong> shows you that, on average, you\u2019re off by about $14,000 (an intuitive number).<br><\/li>\n\n\n\n<li><strong>R\u00b2<\/strong> reveals how much of the price variation your model explains.<\/li>\n<\/ul>\n\n\n\n<p>Together, they give you a complete story.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Forecasting Delivery Times<\/strong><\/h3>\n\n\n\n<p>Suppose you\u2019re predicting delivery times in minutes. Outliers happen (traffic accidents, bad weather).<\/p>\n\n\n\n<ul>\n<li>MAE gives you the average miss \u2192 \u201cWe\u2019re usually about 5 minutes off.\u201d<br><\/li>\n\n\n\n<li>Median AE gives you the typical miss \u2192 \u201cHalf the time we\u2019re within 3 minutes.\u201d<br><\/li>\n\n\n\n<li>Max error warns you about those occasional disasters \u2192 \u201cBut sometimes, we\u2019re off by 45 minutes.\u201d<\/li>\n<\/ul>\n\n\n\n<p>This matters more to customers than a single global metric.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices When Choosing Metrics<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>Don\u2019t rely on a single metric:<\/strong> Each one tells only part of the story. Use a mix (e.g., MSE + MAE + R\u00b2).<br><\/li>\n\n\n\n<li><strong>Think about your domain.<\/strong><strong><br><\/strong>\n<ul>\n<li>If big errors are unacceptable \u2192 prefer MSE\/RMSE.<br><\/li>\n\n\n\n<li>If outliers are common and tolerable \u2192 MAE or Median AE.<br><\/li>\n\n\n\n<li>If relative error matters \u2192 MSLE.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Watch out for scale: <\/strong>Metrics like MSE are scale-dependent (predicting house prices vs. predicting interest rates produce vastly different values). Normalize or compare models on the same dataset only.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Tips for Using Sklearn Metrics<\/strong><\/h2>\n\n\n\n<ul>\n<li>Always look at the confusion matrix as it grounds all derived metrics.<br><\/li>\n\n\n\n<li>Be careful with average settings (micro, macro, weighted) in multiclass.<br><\/li>\n\n\n\n<li>Watch out for metric saturation in trivial classifiers (e.g., accuracy \u2192 0.95 but model is worthless).<br><\/li>\n\n\n\n<li>When using probabilistic classifiers, always work with predicted probabilities for ROC, log loss, etc.<br><\/li>\n\n\n\n<li>Scale and preprocess data consistently, as metrics are meaningful only if input and target align properly.<br><\/li>\n\n\n\n<li>For regression, don\u2019t rely on a single metric. Consider both MSE and MAE, and check residuals.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Sklearn Metric for Each ML Problem<\/strong><\/h2>\n\n\n\n<p>Choosing the right Sklearn Metric depends on whether you&#8217;re solving a classification or regression problem:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Problem Type<\/strong><\/th><th><strong>Best Metric<\/strong><\/th><th><strong>Why It&#8217;s Used<\/strong><\/th><th><strong>Examples<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Balanced Classification<\/strong><\/td><td>Accuracy<\/td><td>Works well when classes are roughly equal in size<\/td><td>Cat vs dog image classification, Handwritten digit recognition, Sentiment (positive\/negative) classification<\/td><\/tr><tr><td><strong>Imbalanced Classification<\/strong><\/td><td>F1 Score<\/td><td>Balances precision and recall when one class is rare<\/td><td>Fraud detection, Disease diagnosis, Spam email detection<\/td><\/tr><tr><td><strong>Classification (ranking confidence)<\/strong><\/td><td>ROC-AUC<\/td><td>Measures how well the model separates classes across thresholds<\/td><td>Credit risk scoring, Customer churn prediction, Loan default prediction<\/td><\/tr><tr><td><strong>Regression (general)<\/strong><\/td><td>RMSE<\/td><td>Penalizes large errors more, useful when big mistakes are costly<\/td><td>House price prediction, Stock price forecasting, Demand forecasting<\/td><\/tr><tr><td><strong>Regression (with outliers)<\/strong><\/td><td>MAE<\/td><td>Less sensitive to outliers than RMSE<\/td><td>Delivery time prediction, Salary prediction, Energy consumption prediction<\/td><\/tr><tr><td><strong>Regression (explaining variance)<\/strong><\/td><td>R\u00b2 Score<\/td><td>Shows how much of the outcome the model explains<\/td><td>Sales forecasting, Marketing spend vs revenue analysis, Crop yield prediction<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, there\u2019s no single \u201cbest\u201d regression metric. MSE and RMSE tell you how painful big errors are, MAE gives you an intuitive sense of typical mistakes, R\u00b2 shows how much variance you\u2019ve explained, and other metrics like max error or MSLE address special cases.&nbsp;<\/p>\n\n\n\n<p>The real skill isn\u2019t in memorizing them, but in knowing which ones matter for your problem. So the next time you evaluate a regression model, don\u2019t just report one score, look at multiple angles, connect them to your domain, and you\u2019ll have a far clearer picture of how your model is really performing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1787292707326\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. What is sklearn.metrics used for?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It&#8217;s used to check how well a machine learning model is performing \u2014 by comparing its predictions to the actual results.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787292711632\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. What is the difference between accuracy and precision in sklearn?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Accuracy shows how many predictions were correct overall. Precision shows how many of the positive predictions were actually correct.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787292714283\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. Which sklearn metric should I use for regression?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use MSE or RMSE if large errors matter most, and MAE if you want a simpler, more forgiving measure of error.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787292715475\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. Is a higher F1 score always better?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, a higher F1 score means a better balance between precision and recall. It&#8217;s most useful when your classes are imbalanced.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787292719633\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. Can sklearn metrics be used for clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, metrics like the silhouette score help you check how well data points are grouped when there are no true labels.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Sklearn Metrics is a module in the scikit-learn library that checks how well a machine learning model is performing. It compares the model&#8217;s predictions with the actual results and gives you a clear score. Without these metrics, you&#8217;re just guessing whether your model is good. Sklearn Metrics gives you the exact numbers you need, whether [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":135227,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[933],"tags":[],"views":"4324","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/Sklearn-Metrics-Explained-The-Complete-Guide-with-Code-Examples-300x116.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/88689"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=88689"}],"version-history":[{"count":17,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/88689\/revisions"}],"predecessor-version":[{"id":139603,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/88689\/revisions\/139603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/135227"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=88689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=88689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=88689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}