πŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
πŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Metrics in Python

Learn about Metrics in this comprehensive Python tutorial. Learn why Accuracy is dangerously flawed on imbalanced datasets, and exactly how to use Precision, Recall, and AUC.

⚑ Total XP: 0|πŸ’» tensorflow XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

Why can 99% accuracy be meaningless for detecting a rare disease?


πŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
πŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Listen up. If you're building deep learning models, understanding Metrics in Python is non-negotiable. This is where graphs get compiled, gradients get computed, and raw data turns into intelligence.

1Tf metrics Part 1

Every Keras model juggles two different kinds of numbers during training: a loss and one or more metrics. The loss β€” binary_crossentropy, mse, categorical_crossentropy β€” is the value the optimizer actually differentiates and minimizes through backpropagation. It is chosen because it is mathematically well-behaved (smooth, differentiable), not because a human can look at 0.1423 and understand what it means.

Metrics exist purely for the human on the other side of the screen. "Accuracy: 95%" or "Precision: 0.87" is something a product manager, a stakeholder, or you at 2 a.m. can interpret at a glance. Keras computes metrics after every batch or epoch, but β€” critically β€” never uses them to update a single weight.

This separation matters because the two numbers can tell very different stories. A model's loss can keep decreasing smoothly while a metric like accuracy stalls or even gets worse on validation data, which is usually the first sign of overfitting.

βœ•
β€”
+
# Humans understand 95% Accuracy.
# Humans do not understand a Loss of 0.1423.
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

2Tf metrics Part 2

You declare which metrics to track directly inside model.compile(), alongside the optimizer and loss β€” for example model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]).

From that point on, Keras computes every listed metric automatically at the end of each batch during model.fit(), and reports it in the progress bar and the returned History object β€” no extra code required. You can pass metric names as strings ("accuracy", "precision", "recall") or import the metric classes directly (tf.keras.metrics.Precision()) when you need to configure thresholds or per-class behavior.

Because metrics are decoupled from the loss, you can track as many as you want β€” accuracy, precision, recall, AUC β€” simultaneously, without any of them affecting how the model actually learns.

βœ•
β€”
+
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

3Tf metrics Part 3

The core distinction to internalize: the loss function is mathematical fuel for backpropagation, while metrics are a read-only dashboard. During training, TensorFlow computes gradients of the loss with respect to every trainable weight and nudges those weights in the direction that reduces the loss β€” metrics never enter that gradient calculation.

This is why you can pick a loss that's technically 'ugly' to a human (like binary_crossentropy) but perfect for gradient descent (smooth, differentiable everywhere), while simultaneously tracking a metric like accuracy that's intuitive for humans but mathematically unsuitable for optimization (it's a step function β€” not differentiable, so gradients through it are meaningless).

Getting this backwards is a common beginner mistake: trying to directly optimize accuracy instead of a proper loss function almost never works, because there's no useful gradient signal to follow.

βœ•
β€”
+
# Human vs Machine
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

4Tf metrics Part 4

Accuracy β€” the percentage of predictions that are correct β€” feels like the obvious metric to optimize for. It becomes dangerously misleading the moment your classes are imbalanced. Imagine a model screening for a disease that affects only 1% of the population. A model that outputs "Healthy" for every single patient, without looking at a single input feature, scores 99% accuracy.

That number looks like a triumph on a dashboard, but the model is medically useless β€” it has a 0% detection rate for the one class that actually matters. This is the "accuracy paradox": on skewed datasets, accuracy rewards a model for mirroring the majority class rather than for learning anything meaningful about the minority class.

The same trap appears constantly in production ML: fraud detection (fraud is rare), spam filtering, defect detection on a manufacturing line β€” anywhere the interesting class is a small minority of the data.

βœ•
β€”
+
# 99% Accuracy is meaningless if the AI misses 100% of the sick patients.
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

5Tf metrics Part 5

The accuracy paradox generalizes to any highly imbalanced classification task, not just medical screening. In fraud detection, where roughly 99.9% of transactions are legitimate, a model that never flags anything as fraud still reports 99.9% accuracy β€” while catching zero fraud, which is the entire point of building the model in the first place.

The root cause is that accuracy treats every misclassification as equally costly, but in imbalanced problems the errors are wildly asymmetric: missing a fraudulent transaction (a false negative) is far more expensive than flagging a legitimate one for review (a false positive). Accuracy has no way to express that asymmetry β€” it just counts total correct predictions over total predictions, letting the massive majority class dominate the score.

This is exactly why practitioners reach for Precision, Recall, F1-score, or AUC on imbalanced datasets instead of accuracy alone β€” each of those metrics is sensitive to how the model performs specifically on the minority class.

βœ•
β€”
+
# The Accuracy Trap
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

6Tf metrics Part 6

Precision answers a very specific question: of everything the model flagged as positive, how much of it was actually positive? Formally, Precision = True Positives / (True Positives + False Positives).

In code, this looks like importing the metric class directly: from tensorflow.keras.metrics import Precision, then passing metrics=[Precision()] inside model.compile().

High precision means very few false alarms β€” when the model says "fraud", it's almost always right. That makes precision the metric to prioritize whenever a false positive is costly or annoying: falsely flagging a legitimate credit card transaction irritates a customer and burns a human reviewer's time, so a fraud team optimizing for precision wants to be confident every flagged case is worth investigating.

Precision says nothing, however, about how much fraud the model *missed* β€” that's a completely separate question, answered by recall.

βœ•
β€”
+
# High Precision = Very few False Alarms
from tensorflow.keras.metrics import Precision
metrics=[Precision()]
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

7Tf metrics Part 7

A model with high precision but low recall on cancer detection has a very specific β€” and dangerous β€” failure profile. High precision means that whenever it does predict "cancer", it's almost always correct: very few healthy patients get a false alarm. But low recall means it's only catching a small fraction of the actual cancer cases in the dataset β€” it's being extremely conservative, only flagging the most obvious, unambiguous cases and staying silent on everything else.

In plain terms: the model rarely cries wolf, but it also lets a lot of real wolves through. For a cancer screening tool, that's close to the worst possible trade-off, because the cost of a missed diagnosis (a false negative) is far higher than the cost of an unnecessary follow-up test (a false positive).

This is why precision and recall have to be read together, never in isolation β€” a single high number can hide a serious weakness in the other.

βœ•
β€”
+
# Precision Analysis
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

8Tf metrics Part 8

Before the next check, make sure precision and recall are cleanly separated in your head, because they answer opposite questions. Precision looks at the model's positive predictions and asks how many were correct. Recall is about to flip that lens entirely: it looks at all the real positive cases in the dataset and asks how many the model actually found.

The two metrics trade off against each other. A model can trivially get 100% recall by flagging every single input as positive β€” it will never miss a real case, but its precision will collapse because of all the false alarms. Likewise, a model can get near-perfect precision by only flagging the single most obvious case β€” but its recall will be terrible.

Understanding where your application sits on that trade-off is the whole game of choosing metrics for imbalanced problems.

βœ•
β€”
+
# SYSTEM WARNING:
# ADA Protocol initiating...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

9Tf metrics Part 9

Recall β€” also called sensitivity or the true positive rate β€” answers the mirror-image question to precision: of all the actual positive cases that exist in the dataset, how many did the model successfully find? Formally, Recall = True Positives / (True Positives + False Negatives).

High recall means the model misses very few real positives β€” it casts a wide net and catches almost everything, even if that means occasionally flagging something that turns out to be negative. That makes recall the metric to prioritize whenever a false negative (a missed case) is the expensive, dangerous, or catastrophic outcome: missed fraud, an undetected tumor, an undetected structural defect.

Like precision, recall says nothing on its own about false positives β€” a model can achieve perfect recall by flagging everything, which is why recall is almost always evaluated alongside precision, never alone.

βœ•
β€”
+
# ADA initializing Recall constraints...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

10Tf metrics Part 10

Self-driving pedestrian detection is the textbook case for choosing recall over precision. A false positive here β€” the car braking hard because it mistook a shadow or a mailbox for a person β€” is uncomfortable and annoying for the passengers, but ultimately harmless. A false negative β€” failing to detect an actual pedestrian in the road β€” is catastrophic and potentially fatal.

Because the cost of the two error types is wildly asymmetric, the engineering decision is clear: tune the model (and its decision threshold) to maximize recall, accepting a higher rate of false alarms as the price of near-zero missed detections. This is the general pattern for any safety-critical system: identify which error type is unacceptable, and optimize the metric that punishes that error type the hardest.

Contrast this with something like a spam filter, where a false positive (a real email marked as spam) is often worse than a false negative (a spam email reaching the inbox) β€” the right metric genuinely depends on the cost structure of the application, not a fixed rule.

βœ•
β€”
+
# DEFEND THE SYSTEM
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

11Tf metrics Part 11

With loss versus metrics, and precision versus recall, sorted out, the next lesson moves to a closely related topic: Callbacks, the mechanism Keras uses to react to those metrics *during* training β€” stopping early when validation loss stops improving, saving the best checkpoint automatically, or reducing the learning rate when a metric plateaus.

As a quick recap: never let "Accuracy" be the only number you look at on an imbalanced dataset. Check precision when false positives are costly, check recall when false negatives are costly, and remember that both are metrics for humans β€” the model itself is still being optimized against the loss function the whole time.

βœ•
β€”
+
print("System secured.\
Metrics optimal.")
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

12Step-by-Step Breakdown

Loss Functions are for the computer. "Metrics" are for humans. The computer optimizes binary_crossentropy, but humans want to see "Accuracy".

You define metrics inside model.compile(). Keras will calculate them automatically during model.fit().

What is the primary difference between a "Loss Function" and a "Metric" in Keras?

  • β†’There is no difference; they are identical.
  • β†’The network uses the Loss Function to mathematically update its weights via backpropagation; Metrics are solely calculated for humans to monitor performance.
  • β†’Metrics update the weights; Loss is for humans.

Accuracy is dangerous. Imagine an AI detecting a rare disease that only affects 1% of the population. If the AI simply guesses "Healthy" every single time, it will have 99% Accuracy.

In highly imbalanced datasets (e.g., fraud detection where 99.9% of transactions are legitimate), why is "Accuracy" a terrible metric?

  • β†’Because accuracy is too hard for the GPU to calculate.
  • β†’Because a model can achieve near-perfect accuracy simply by ignoring the rare class and guessing the majority class every single time.
  • β†’Because accuracy cannot go above 50%.

To solve this, we use specialized metrics. "Precision" asks: Of all the transactions the AI FLAGGED as fraud, how many were ACTUALLY fraud?

If your AI has High Precision but Low Recall in predicting Cancer, what does that mean?

  • β†’It means the AI is perfectly balanced.
  • β†’It means that when the AI does predict Cancer, it is almost always right (few false alarms), but it is probably missing a lot of actual cancer patients.
  • β†’It means the model is broken.

Now, prepare yourself. We are about to enter the ADA Defense Protocol. Ensure you understand Recall.

Recall asks: Of ALL the actual fraud in the dataset, how much did the AI successfully find? (High Recall = Few Missed Targets).

ADA DEFENSE: You are programming the AI for a self-driving car to detect pedestrians. Is it more important to have High Precision (never braking for shadows) or High Recall (never missing a real pedestrian)?

  • β†’High Precision. False alarms are the worst.
  • β†’High Recall is absolutely critical. A False Positive (braking for a shadow) is annoying, but a False Negative (missing a real pedestrian) is catastrophic.
  • β†’Accuracy is the only thing that matters.

Threat neutralized. Precision/Recall trade-off acknowledged. Proceeding to Callbacks.

Compute Real Accuracy. Finish compute_accuracy(): accuracy is just the fraction of exact matches.

Level Up πŸš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Semantic Usage

Using the proper structure for Metrics in Python ensures that screen readers can correctly interpret the content hierarchy and purpose.

<!-- Apply semantic elements appropriately -->

SEO Implications

  • 1

    Contextual Relevance

    Proper implementation of Metrics in Python provides search engine crawlers with better context, improving the indexing accuracy of your page.

Best Practices

Clean Code

Always validate your structure when using Metrics in Python to prevent layout shifts and DOM inconsistencies.

Separation of Concerns

Keep styling and behavior separate from the structural markup of Metrics in Python.

Frequent Bugs

THE BUG

Unexpected layout shifts or styling failures.

THE FIX

Ensure all implementations related to Metrics in Python are properly structured according to strict specifications.

Real-World Examples

Production Usage

Here is how Metrics in Python is typically implemented in a professional, robust application.

<!-- Best practice implementation of Metrics in Python -->
<div class="production-ready">
  <!-- Content -->
</div>

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Using mutable default arguments

# Wrong def append_item(item, lst=[]): lst.append(item) return lst # Correct def append_item(item, lst=None): if lst is None: lst = [] lst.append(item) return lst

The Solution //

Default arguments are evaluated once when the function is defined. If you use a list or dict, the same instance is shared across all calls. Use None instead.

The Error //

Forgetting 'self' in class methods

# Wrong class Dog: def bark(): print('Woof!') # Correct class Dog: def bark(self): print('Woof!')

The Solution //

Instance methods in Python must have 'self' as their first parameter. Without it, you will get a TypeError when calling the method.

Lesson Glossary

[01]Precision

True Positives / (True Positives + False Positives). Answers: Of all the positive predictions made, how many were actually positive?

Code Preview
// Precision context

[02]Recall

True Positives / (True Positives + False Negatives). Answers: Of all the actual positive items in the dataset, how many did we find?

Code Preview
// Recall context

Continue Learning