Model Evaluation

Python · Machine Learning (Scikit-Learn)

Theory

Classification metrics start from the confusion matrix, which counts true positives (TP), true negatives (TN), false positives (FP, false alarms) and false negatives (FN, misses). Accuracy = correct / total. Precision = TP / (TP + FP): of the rows flagged positive, how many really were. Recall = TP / (TP + FN): of the real positives, how many were caught. F1 is the harmonic mean of precision and recall, high only when both are.

These all depend on the 0.5 threshold. ROC AUC uses the predicted probabilities instead and measures how well the model ranks positives above negatives across every threshold: 1 is perfect ranking, 0.5 is random guessing. In the diagram, one positive scored 0.4 (a miss) and one negative scored 0.6 (a false alarm), so accuracy, precision and recall are each 0.67, while AUC is 0.89 because most pairs are still ranked correctly.

Why we use it

A single accuracy number hides which mistakes a model makes. On imbalanced data, a model that always predicts 'not fraud' can be 99% accurate and useless; precision, recall, F1 and AUC reveal that.

When to use it

Use precision when false alarms are expensive (spam filters), recall when misses are expensive (fraud, disease), F1 to balance both, and ROC AUC to compare models independent of threshold. Accuracy is fine only for balanced classes.

How it works

  1. Fitted classifier and test set
  2. Get predictions and probabilities for X_test
  3. Build the confusion matrix: TN, FP, FN, TP
  4. Are false alarms or misses more costly? Yes: Focus on precision (alarms) or recall (misses). No: Use F1 or accuracy.
  5. Compare models threshold-free with ROC AUC
  6. Choose the model and threshold

Syntax

from sklearn.metrics import (accuracy_score, precision_score, recall_score,
    f1_score, confusion_matrix, roc_auc_score, classification_report)
confusion_matrix(y_test, y_pred)
roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])

Example

print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
print(roc_auc_score(y_test, prob))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic