1. LEARNING OBJECTIVES

By the end of this lesson, you will be able to:

  • Understand why predicting a category (Classification) is completely different from predicting a number (Regression).

  • Explain the “Sigmoid Function” – how we turn a math equation into a probability (0% to 100%).

  • Build a Logistic Regression model to predict whether a customer will default on a loan.

  • Decode a “Confusion Matrix” and perfectly understand True Positives, True Negatives, False Positives, and False Negatives.

  • Explain why a 99% accurate model can be completely worthless for Fraud Detection.

  • Understand the dangerous “Bias-Variance Tradeoff” (Overfitting vs. Underfitting) using relatable business examples.

  • Write a beginner-friendly Python script for binary classification using scikit-learn.


2. WHAT IS CLASSIFICATION IN FINTECH?

2.1 The “Yes/No” Problem
In Lesson 1, we tried to predict how much a customer would spend. That was Regression.
In Lesson 2, we are predicting Binary Outcomes:

  • Will this customer Default on their loan? (Yes=1, No=0).

  • Is this credit card transaction Fraudulent? (Yes=1, No=0).

  • Will this customer Churn (close their account and leave our bank) next month? (Yes=1, No=0).

2.2 Why we can’t use Linear Regression for Yes/No
If we draw a straight line through our data to predict a “Yes” or “No”, the line will inevitably produce outputs like -0.51.8, or 0.2. A bank cannot accept a loan default probability of 1.8 or -0.5.
We need to force the output to be between 0 and 1, representing a probability (0% to 100%). To do this, we wrap our linear equation inside a Sigmoid Function.


3. THE SIGMOID FUNCTION: TURNING MATH INTO PROBABILITY

3.1 The Visual of the S-Curve
The Sigmoid Function creates a beautiful “S” shape.

  • On the far left (if the equation gives a huge negative number), the sigmoid approaches 0.

  • On the far right (if the equation gives a huge positive number), the sigmoid approaches 1.

  • Right in the middle (if the equation gives 0), the sigmoid is exactly 0.5.

3.2 The Math Formula (Do not be scared!)
The formula for the sigmoid is:

σ(z)=11+e−z

Where e is Euler’s constant (approx 2.718), and z is your linear equation (w0+w1x1+w2x2).
How we interpret it: If the model outputs a probability of 0.85 for a customer, we say: “According to the model, this customer has an 85% chance of defaulting.”
To make a final decision, we set a Decision Threshold. Usually, this is 0.5:

  • Probability >= 0.5 ➡️ Predict “Default” (1).

  • Probability < 0.5 ➡️ Predict “Paid” (0).


4. THE MATHEMATICAL EVALUATION MATRIX (THE CONFUSION MATRIX)

As a beginner, the “Confusion Matrix” is the most critical tool you will ever use to judge your model. It is a 2×2 grid that breaks down exactly how the model performed.

 
 
  Model Predicted: PAID Model Predicted: DEFAULT
Actual Truth: PAID True Negative (TN)
We got it right. Good!
False Positive (FP)
(The “Cry Wolf” error)
Model flagged a good customer as a defaulter.
Actual Truth: DEFAULT False Negative (FN)
(The “Missed Crook” error)
The model missed a customer who went bankrupt.
True Positive (TP)
We caught the defaulter. Good!

4.1 Why “Accuracy” is a Liar in Finance
Let’s say you run a credit card company with 100,000 customers. Only 100 of them are fraudsters (0.1% fraud rate). You build an ML model that predicts “Not Fraud” for every single person in the universe.

  • The model catches 0 fraudsters.

  • The model incorrectly flags 0 innocent people.

  • Accuracy = 99.9% (99,900/100,000).
    A business manager would look at this and say: “You bankrupted us, we lost all our money because the criminals got away!” This is why we never use Accuracy for fraud or default detection.

4.2 The Real Metrics: Precision and Recall
To replace Accuracy, financial institutions focus on two specific metrics derived from the confusion matrix:

  • Recall (Sensitivity): Out of all the actual fraudsters (True Positives + False Negatives), how many did we catch?

    • Formula: TP/(TP+FN)

    • Business Translation: “How good are we at catching the bad guys?” Banks will set a very high Recall threshold for fraud.

  • Precision: Out of all the people the model flagged as fraudsters, how many actually were fraudsters?

    • Formula: TP/(TP+FP)

    • Business Translation: “If we freeze a customer’s card, how often are we annoying an innocent VIP?” High Precision means you do not harass your good customers.

4.3 The F1-Score (The Harmonious Blend)
If a model achieves 100% Recall (catches every single criminal) but 0.01% Precision (it flags absolutely everyone as a criminal), it will bankrupt the bank via customer complaints and manual reviews.
The F1-Score takes the harmonic mean of Precision and Recall. It balances them out. If either number is zero, the F1-Score is zero. It forces the model to balance catching criminals with not annoying customers.


5. THE BIGGEST DANGER IN AI: THE BIAS-VARIANCE TRADEOFF

Imagine you are teaching a new loan officer how to approve loans.

5.1 High Bias (Underfitting – The “Lazy” Employee)

  • This employee is incredibly lazy. They don’t look at the data. They just create a single, rigid rule: “Approve anyone making over $30,000.”

  • Result: They fail to capture the subtle patterns (e.g., a person earning $50k with massive credit card debt is actually very risky). The model is too simple. It performs badly on both the training data and the test data. This is Underfitting.

5.2 High Variance (Overfitting – The “Memorizing” Employee)

  • This employee is a savant, but they have a flaw. They examine the past year’s loan data with microscopic precision. They notice that “Customer 5512 defaulted because it rained on a Tuesday in February.”

  • They memorize all these absurd, purely random coincidences (which are actually just random noise).

  • Result: The employee passes the final exam (Training Data) with 100% perfect scores. But when you send them out to the real world (Testing Data) to approve loans for new customers, they fail miserably because they are trying to apply “Tuesday rain” rules to new people. This is Overfitting.

5.3 The Goldilocks Zone
Our goal in ML is to find the perfect middle ground: complex enough to spot the important financial patterns (Income, Debt), but simple enough to ignore the random noise of life (rainy Tuesdays, specific timestamps). We achieve this via Regularization (a tiny mathematical penalty applied to the weights to keep them from getting too extreme).


6. BEGINNER HANDS-ON LAB: PREDICTING LOAN DEFAULT (CLASSIFICATION)

Now, we will write a beginner-friendly Python script to predict Loan Default. We will use StandardScalertrain_test_split, and LogisticRegression. Read the comments closely!

python
# --- 1. IMPORT OUR TOOLS ---
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
# This library gives us the Confusion Matrix and the detailed report (Precision/Recall).
from sklearn.metrics import confusion_matrix, classification_report

# --- 2. GENERATE MOCK DATA ---
# In real life, you would load a CSV. 
# We are making up 500 customers.
np.random.seed(42) # Ensures the random numbers are the same every time we run.
num_customers = 500
data = {
    'Age': np.random.randint(20, 65, num_customers),
    'Annual_Income': np.random.normal(55000, 20000, num_customers),
    'Current_Debt': np.random.exponential(scale=5000, size=num_customers),
    'Credit_Limit': np.random.randint(1000, 30000, num_customers)
}
df = pd.DataFrame(data)

# Feature Engineering: Let's calculate the "Debt to Income Ratio" - this is highly predictive for default.
# We add 1e-9 (a tiny number) to the denominator to avoid division by zero errors.
df['Debt_to_Income'] = df['Current_Debt'] / (df['Annual_Income'] + 1e-9)

# Let's simulate the Target (Default = 1, Paid = 0).
# We set the rule: Higher Debt_to_Income = higher chance of defaulting.
default_prob = (df['Debt_to_Income'] * 4) + np.random.normal(0, 0.5, num_customers)
# If the calculated probability is above 0.5, we mark them as a defaulter.
df['Default'] = (default_prob > default_prob.median()).astype(int)

# --- 3. SEPARATE FEATURES (X) AND TARGET (y) ---
X = df.drop('Default', axis=1)
y = df['Default']

# --- 4. TRAIN / TEST SPLIT ---
# 80% of customers to teach the model, 20% to test it.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# --- 5. SCALE THE DATA ---
# Always scale numbers! Large numbers confuse the optimizer.
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# --- 6. TRAIN THE LOGISTIC REGRESSION MODEL ---
model = LogisticRegression()
# This line triggers the Gradient Descent algorithm to find the perfect sigmoid curve weights.
model.fit(X_train_scaled, y_train)

# --- 7. PREDICT ON THE HIDDEN TEST DATA ---
# Generate the 0 or 1 predictions.
y_pred = model.predict(X_test_scaled)
# Generate the actual probabilities (e.g., "This customer has 0.82 chance of default").
y_pred_prob = model.predict_proba(X_test_scaled)[:, 1]

# --- 8. EVALUATE WITH THE CONFUSION MATRIX ---
print("----- Confusion Matrix -----")
# The output will be a 2x2 table: [[TN, FP], [FN, TP]]
print(confusion_matrix(y_test, y_pred))

print("\n----- Classification Report (Precision, Recall, F1) -----")
print(classification_report(y_test, y_pred))

# --- 9. SEE THE MATH WEIGHTS (EXPLAINABLE AI) ---
features = X.columns
weights = model.coef_[0]
print("\n----- Model Weights (How the Model Thinks) -----")
for feature, weight in zip(features, weights):
    print(f"{feature}: {weight:.4f}")
    if weight > 0:
        print(f"  -> Positive weight: Higher {feature} increases the probability of default.")
    else:
        print(f"  -> Negative weight: Higher {feature} decreases the probability of default.")

Interpreting the Output: When you run this, look at the Classification Report.

  • Look at the F1-score for the 1 (Default) class. If it’s around 0.80 to 0.90, your model is doing a great job balancing precision and recall.

  • Look at the Weights at the bottom. If Debt_to_Income has a large positive number (e.g., 1.5), your model is saying: “As debt rises, the risk goes up significantly.” That makes perfect logical sense to you as a banker, and that is why Logistic Regression is trusted by regulators—it explains why it makes decisions.


7. SUMMARY FOR THE BEGINNER FINANCE PRACTITIONER

If you take nothing else away from this lesson, remember these three rules:

  1. Do not trust Accuracy. In finance, we care about Precision (not annoying good customers) and Recall (catching the criminals).

  2. The Confusion Matrix is your scoreboard. It tells you exactly where your model is making mistakes (False Positives or False Negatives).

  3. Beware of Overfitting. If your model scores 100% on your training data but fails in the real world, it memorized, it didn’t learn. Always use a Test Set to keep it honest.