MACHINE LEARNING – Sigmoid Function Applied to Credit-Risk Classification

By Izairton Vasconcelos
9 min read

Table of Contents

In banking, accurately classifying client credit risk is essential to reduce defaults and make safer lending decisions.

One of the most widely used mathematical tools in Machine Learning for binary classification is the Sigmoid function, which converts numerical outputs into probabilities, enabling strategic quantitative decisions.


Why Use the Sigmoid Function in Credit Risk Classification?

The Sigmoid function is used in Logistic Regression to map predicted values into probabilities between 0 and 1.

Advantages:

  • Converts linear model outputs into interpretable probabilities.
  • Facilitates binary decision thresholds (e.g., 0.5).
  • Has a smooth gradient, ideal for optimization algorithms.

Technical and Mathematical Overview

The Sigmoid (Logistic) function is defined as:

Fórmula Sigmoid
Fórmula Sigmoid

σ(x)=11+e−xσ(x) = \frac{1}{1 + e^{-x}}

Where:

  • x is the input (e.g. credit score).
  • e is Euler’s number (~2.718).

Output: always between 0 and 1, interpreted as probability.

What does it mean in simple words?

Sigmoid is a function that transforms any number into something between 0 and 1.

Imagine you have any value, for example:

  • It could be -100, 0, or +100.
  • After passing through Sigmoid, the result will always be a number between 0 and 1.

Why do we use it?

We use this function in Machine Learning and Neural Networks when we want to convert any number into a probability.

For example:

  • If you are trying to predict whether an email is SPAM (1) or NOT
    SPAM (0),
  • The model calculates a value (it could be 3.2 or -5.1) and passes it
    through the Sigmoid,
  • The result will be something like 0.95 (95% chance of being SPAM) or
    0.01 (1% chance of being SPAM).

How does it work step by step?

1) Calculate -x: Example: If x = 2, then -x = -2

2) Calculate e^(-x): This is Euler’s number raised to -x. In the example: e^(-2) ≈ 0.135

3) Add 1 + e^(-x): Here: 1 + 0.135 = 1.135

4) Divide 1 by this result: 1 / 1.135 ≈ 0.88

So, if x = 2, sigmoid(2) ≈ 0.88, that is, 88%.

Practical analogy (for laypeople)

Imagine a volume knob:

🔈🔉🔊

  • Without Sigmoid, turning the knob a bit increases the sound
    abruptly or even explodes it.
  • With Sigmoid, the knob smoothly controls the sound from 0 to 1,
    never going beyond this limit.
➡️ Sigmoid works like an “adjuster” that keeps everything within the acceptable limit (between 0 and 1), without exploding into infinite values.

Quick summary

Fórmula Sigmoid
Fórmula Sigmoid

Choosing the Right Approach (Manual vs Function)

Fórmula Sigmoid
Fórmula Sigmoid

Two ways to implement it in Python:

  • Manual Calculation: using np.exp() directly.
  • Library Function: using SciPy’s expit() (more efficient).

Both return the same result. In production, the library function is recommended.


Real Example: Classifying Bank Clients by Credit Score

Fórmula Sigmoid
Fórmula Sigmoid

Business Scenario: A bank wants to classify its clients as High Risk (1) or Low Risk (0) based on credit scores, converting them into default
probabilities.


Full Python Script: Manual + Function Together

Here is the consolidated script combining both approaches for beginners to see the full logic flow in one example:

# 1. Importing necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import expit  # Optimized sigmoid from library

# 2. Defining the manual sigmoid function
def sigmoid_manual(x):
    return 1 / (1 + np.exp(-x))

# 3. Simulating an array of credit scores (input data)
credit_score = np.linspace(-10, 10, 100)

# 4. Calculating probabilities using both methods
prob_manual = sigmoid_manual(credit_score)
prob_library = expit(credit_score)

# 5. Plotting comparative graph
plt.figure(figsize=(10, 6))
plt.plot(credit_score, prob_manual, label='Manual Sigmoid', linestyle='--')
plt.plot(credit_score, prob_library, label='Library Sigmoid', linestyle='-')
plt.title('Sigmoid Function Applied to Credit Score')
plt.xlabel('Credit Score')
plt.ylabel('Probability of Default')
plt.legend()
plt.grid(True)
plt.show()

# 6. Practical example: classifying a client with score=2
score_client = 2
prob_default_manual = sigmoid_manual(score_client)
prob_default_library = expit(score_client)

# 7. Displaying manual calculation result
print(f"✅ [Manual] Probability of default (Score=2): {prob_default_manual:.2f}")
if prob_default_manual >= 0.5:
    print("🔴 [Manual] Client classified as HIGH RISK.")
else:
    print("🟢 [Manual] Client classified as LOW RISK.")

# 8. Displaying library function result
print(f"✅ [Library] Probability of default (Score=2): {prob_default_library:.2f}")
if prob_default_library >= 0.5:
    print("🔴 [Library] Client classified as HIGH RISK.")
else:
    print("🟢 [Library] Client classified as LOW RISK.")

How the Full Script Works – Step by Step

Fórmula Sigmoid
Fórmula Sigmoid

Where Do the Data Come From and How Does the Script Use Them?

Data Source: In this example, scores are simulated with np.linspace. In practice, they would come from a CSV, database, or banking API.

How Are They Used? Each score is processed by the sigmoid function, transforming it into a default probability. Based on the threshold (0.5), the client is classified as HIGH RISK or LOW RISK.


Python Script: Manual Calculation (individualized)

import numpy as np
import matplotlib.pyplot as plt

def sigmoid_manual(x):
    return 1 / (1 + np.exp(-x))

credit_score = np.linspace(-10, 10, 100)
prob_manual = sigmoid_manual(credit_score)

plt.figure(figsize=(10, 6))
plt.plot(credit_score, prob_manual, label='Manual Sigmoid', linestyle='--')
plt.title('Sigmoid Function Applied to Credit Score (Manual)')
plt.xlabel('Credit Score')
plt.ylabel('Probability of Default')
plt.legend()
plt.grid(True)
plt.show()

score_client = 2
prob_default = sigmoid_manual(score_client)

print(f"✅ Probability of default (Score=2): {prob_default:.2f}")
if prob_default >= 0.5:
    print("🔴 Client classified as HIGH RISK.")
else:
    print("🟢 Client classified as LOW RISK.")

Python Script: Function Calculation (individualized)

from scipy.special import expit
import numpy as np
import matplotlib.pyplot as plt

credit_score = np.linspace(-10, 10, 100)
prob_library = expit(credit_score)

plt.figure(figsize=(10, 6))
plt.plot(credit_score, prob_library, label='Library Sigmoid', linestyle='-')
plt.title('Sigmoid Function Applied to Credit Score (Library)')
plt.xlabel('Credit Score')
plt.ylabel('Probability of Default')
plt.legend()
plt.grid(True)
plt.show()

score_client = 2
prob_default_library = expit(score_client)

print(f"✅ Probability of default (Score=2): {prob_default_library:.2f}")
if prob_default_library >= 0.5:
    print("🔴 Client classified as HIGH RISK.")
else:
    print("🟢 Client classified as LOW RISK.")

Line-by-Line Explanations

Fórmula Sigmoid
Fórmula Sigmoid

Graphs and VSCode Explanation

Graph Display

A line graph will open in a new window (or in Jupyter notebook if used), showing:

  • The Sigmoid curve calculated manually (dashed line).
  • The Sigmoid curve calculated using Scipy library (solid line).
  • Both curves are practically identical, validating the manual
    implementation.

S-shaped Sigmoid Graph:

  • X-axis: Credit score.
  • Y-axis: Probability of default.

Shows coincidence between manual calculation and library function.

Fórmula Sigmoid
Figure 1: Comparison between Manual and Library Sigmoid Functions

This graph shows both implementations of the sigmoid function applied to
credit scores ranging from -10 to +10.

Blue dashed line: Manual implementation using the formula 1 / (1 +
np.exp(-x)).

Orange solid line: Optimized implementation using SciPy’s expit()
function.

Both curves overlap perfectly, confirming the accuracy of the manual
calculation and the efficiency of the library function.

Fórmula Sigmoid
Figure 2: Manual Sigmoid Function Only

This graph shows the sigmoid curve calculated using the manual formula implementation:

Fórmula Sigmoid

σ(x)=11+e−xσ(x) = \frac{1}{1 + e^{-x}}σ(x)=1+e−x1​

The “S”-shaped curve demonstrates how the manual function correctly maps credit scores into default probabilities, matching the library output in shape and scale.

Fórmula Sigmoid
Figure 3: Library Sigmoid Function Only

This graph displays the sigmoid curve calculated using the SciPy expit function.

The curve maps credit scores into probabilities between 0 (low risk) and 1 (high risk) with a smooth “S” shape typical of logistic regression outputs.

Graph Interpretation (Matplotlib)

Visual Description:

Title: Sigmoid Function Applied to Credit Score
Plotted curves:

    • Blue Dashed Line: Manual Sigmoid (sigmoid_manual)
    • Orange Solid Line: Library Sigmoid (scipy.special.expit)

Technical Analysis:

  • The graph displays the classic sigmoid function, with the characteristic “S”-shaped curve that:
    • Approaches 0 for large negative values (very low credit
      scores).
    • Rises rapidly near 0, going from ~0.1 to ~0.9 in a few
      points.
    • Tends towards 1 for large positive values (high credit
      scores).
  • The almost perfect coincidence of both curves validates that:
    • The manual implementation (1 / (1 + np.exp(-x))) is correct.
    • The optimized function expit(x) from SciPy performs the same operation but more efficiently.

Connection to Script:

credit_score = np.linspace(-10, 10, 100)
prob_manual = sigmoid_manual(credit_score)
prob_library = expit(credit_score)

Explanation:

  • np.linspace(-10, 10, 100) creates 100 credit score values between -10 and +10.
  • sigmoid_manual(credit_score) manually calculates the probability for each score.
  • expit(credit_score) does the same using SciPy.

Plotting:

plt.plot(credit_score, prob_manual, label='Manual Sigmoid',
linestyle='--')

plt.plot(credit_score, prob_library, label='Library Sigmoid',
linestyle='-')

Both functions are plotted with different styles for comparison.

VSCode Terminal Interpretation

Output

Fórmula Sigmoid
Fórmula Sigmoid

Technical Analysis

Line 2: Classifies the client based on the threshold of 0.5.

Script snippet:

if prob_default >= 0.5:
  print("🔴 Client classified as HIGH RISK.")
else:
  print("🟢 Client classified as LOW RISK.")

Since 0.88 ≥ 0.5, the client is classified as HIGH RISK.

Line 1: Calculates the probability of default for a client with credit score = 2.

Result: 0.88 (88% chance of default).

Script snippet:

score_client = 2
prob_default = sigmoid_manual(score_client)
print(f"✅ Probability of default (Score=2): {prob_default:.2f}")

Here, the script applies the sigmoid_manual function to the client's score.


Probability and Score Explanation

How was the ~88% probability obtained?

Sigmoid Mathematical Concept: The sigmoid function transforms any real number into a value between 0 and 1, applying the formula:

Fórmula Sigmoid

Substituting the value score_client = 2

Fórmula Sigmoid

Step-by-step calculation

Calculate the negative exponent:

Fórmula Sigmoid
Fórmula Sigmoid

Add 1 to the result:

Fórmula Sigmoid
Fórmula Sigmoid

Divide 1 by this result:

Fórmula Sigmoid
Fórmula Sigmoid

Conclusion: 0.8808 → 88% (when formatted to two decimal places)

Code snippet performing this calculation

score_client = 2

prob_default_manual = sigmoid_manual(score_client)

print(f"✅ \[Manual\] Probability of default (Score=2): {prob_default_manual:.2f}")

Line-by-Line Explanation

score_client = 2: Defines the client’s credit score as 2

prob_default_manual = sigmoid_manual(score_client): Calls the sigmoid_manual function, passing score_client as parameter.

What happens inside the function:

def sigmoid_manual(x):
  return 1 / (1 + np.exp(-x))
  • Calculates np.exp(-2) = e^{-2} ≈ 0.1353
  • Adds 1 + 0.1353 = 1.1353
  • Calculates 1 / 1.1353 ≈ 0.8808

print(f"✅ [Manual] Probability of default (Score=2): prob_default_manual:.2f}"): Prints the result and Formats the number to two decimals, resulting in 0.88 (88%).

Why was score = 2 used in the example?

The number 2 was purposely chosen because:

  • It is a small positive value, resulting in a probability close to but not exactly 1, showing that even moderate scores can yield high default probability.
  • It facilitates manual calculation and explanation for beginner readers, as e−2e^{-2} has a known value (~0.1353), allowing a clear step-by-step without complex numbers.
  • It simulates a realistic scenario, where clients may have low positive scores requiring credit risk analysis.

The example with score=2 reinforces the understanding of the sigmoid
function, binary classification, and its practical application in
banking and finance.

Final Didactic Summary

Fórmula Sigmoid
Fórmula Sigmoid

Practical Applications in Business and AI

Banking: Credit scoring models.

Insurance: Risk-based pricing.

Medical AI: Probabilities in binary diagnosis.

AI Models: Output activation layer in binary classification neural
networks.


Advanced Insights and Strategic Suggestions

Combine sigmoid with Logistic Regression for interpretable models.

Adjust thresholds according to business risk appetite.

Use Softmax for multiclass problems instead of sigmoid.


Conclusion

The sigmoid function is a simple yet powerful mathematical tool that transforms linear outputs into interpretable probabilities, essential in binary classification tasks, especially for credit and financial risk management.


References

  1. NG, Andrew. Machine Learning. Coursera, Stanford University.
    Available at: https://www.coursera.org/learn/machine-learning.
    Accessed on: July 4, 2025.
  2. GOODFELLOW, Ian; BENGIO, Yoshua; COURVILLE, Aaron. Deep Learning.
    MIT Press, 2016. Available at: https://www.deeplearningbook.org.
    Accessed on: July 4, 2025.
  3. SCIPY. SciPy v1.11.3 Reference Guide – scipy.special.expit.
    Available at:
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.expit.html.
    Accessed on: July 4, 2025.
  4. GÉRON, Aurélien. Hands-On Machine Learning with Scikit-Learn, Keras
    & TensorFlow: Concepts, Tools, and Techniques to Build Intelligent
    Systems
    . 2nd ed. Sebastopol: O’Reilly Media, 2019.

Follow & Connect

Izairton Vasconcelos is a technical content creator with degrees in Software Engineering, Business Administration, and Statistics, as well as several specializations in Technology.

He is a Computer Science student and Python specialist focused on productivity, automation, finance, and data science. He develops scripts, dashboards, predictive models, and applications, and also provides consulting services for companies and professionals seeking to implement smart digital solutions in their businesses.

Currently, he publishes bilingual articles and tips in his LinkedIn Newsletter, helping people from different fields apply Python in a practical, fast, and scalable way to their daily tasks.

💼 LinkedIn & Newsletters:
👉Linkedin
👉Portuguese Newsletter
👉English Newsletter

💼 Company Page:
👉Company Page

💻 GitHub:
👉 GitHub

Tagged in:

Python, ML, Sigmoid, Credit-Risk

Last Update: July 15, 2025

About the Author

Izairton Vasconcelos Brazil

Python specialist creating digital solutions that transform productivity into real business results.

View All Posts