📊 PYTHON TIP + MACHINE LEARNING – Sigmoid Function Applied to Credit Risk Classification
📰 Edição #55 — PYTHON TIP + MACHINE LEARNING – Sigmoid Function Applied to Credit Risk Classification
📑 Table of Contents
1. Introduction
2. Why Use the Sigmoid Function in Credit Risk Classification?
3. Technical and Mathematical Overview
4. Choosing the Right Approach (Manual vs Function)
5. Real Example: Classifying Bank Clients by Credit Score
6. Full Python Script: Manual + Function Together
7. How the Full Script Works – Step by Step
8. Python Script: Manual Calculation
9. Python Script: Function Calculation
10. Line-by-Line Explanations
11. Graphs and VSCode Explanation
12. Probability and Score Explanation
13. Practical Applications in Business and AI
14. Advanced Insights and Strategic Suggestions
15. Conclusion
16. References
17. CTA – Follow & Connect
1. Introduction
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.
2. 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.
3. Technical and Mathematical Overview
The Sigmoid (Logistic) function is defined as:
σ(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.
3.1. 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.
3.2. 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:
o If you are trying to predict whether an email is SPAM (1) or NOT SPAM (0),
o The model calculates a value (it could be 3.2 or -5.1) and passes it through the Sigmoid,
o The result will be something like 0.95 (95% chance of being SPAM) or 0.01 (1% chance of being SPAM).
3.3. How does it work step by step?
Calculate -x Example: If x = 2, then -x = -2
Calculate e^(-x) This is Euler’s number raised to -x. In the example: e^(-2) ≈ 0.135
Add 1 + e^(-x) Here: 1 + 0.135 = 1.135
Divide 1 by this result 1 / 1.135 ≈ 0.88
✔️ So, if x = 2, sigmoid(2) ≈ 0.88, that is, 88%.
3.4. 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.
3.5. Quick summary
4. Choosing the Right Approach (Manual vs Function)
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.
5. Real Example: Classifying Bank Clients by Credit Score
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.
6. 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:
7. How the Full Script Works – Step by Step
✅ 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.
8. Python Script: Manual Calculation (individualized)
9. Python Script: Function Calculation (individualized)
10. Line-by-Line Explanations
11. 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.
✅ 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.
✅ Figure 2: Manual Sigmoid Function Only
This graph shows the sigmoid curve calculated using the manual formula implementation:
• The “S”-shaped curve demonstrates how the manual function correctly maps credit scores into default probabilities, matching the library output in shape and scale.
✅ 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.
11.1. Graph Interpretation (Matplotlib)
🔍 Visual Description
• Title: Sigmoid Function Applied to Credit Score
• Plotted curves:
o Blue Dashed Line: Manual Sigmoid (sigmoid_manual)
o Orange Solid Line: Library Sigmoid (scipy.special.expit)
✅ Technical Analysis
The graph displays the classic sigmoid function, with the characteristic “S”-shaped curve that: o Approaches 0 for large negative values (very low credit scores). o Rises rapidly near 0, going from ~0.1 to ~0.9 in a few points. o Tends towards 1 for large positive values (high credit scores).
The almost perfect coincidence of both curves validates that: o The manual implementation (1 / (1 + np.exp(-x))) is correct. o The optimized function expit(x) from SciPy performs the same operation but more efficiently.
📌 Connection to Script
✔️ 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
• Both functions are plotted with different styles for comparison.
11.2. VSCode Terminal Interpretation
🔍 Output
✅ Technical Analysis
1. Line 1: o Calculates the probability of default for a client with credit score = 2. o Result: 0.88 (88% chance of default).
🔗 Script snippet
✔️ Here, the script applies the sigmoid_manual function to the client's score.
2. Line 2: o Classifies the client based on the threshold of 0.5.
🔗 Script snippet
✔️ Since 0.88 ≥ 0.5, the client is classified as HIGH RISK.
12. Probability and Score Explanation
12.1. 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:
🔢 Substituting the value score_client = 2
✔️ Step-by-step calculation:
1.Calculate the negative exponent:
2. Add 1 to the result:
3. Divide 1 by this result:
✅ Conclusion: 0.8808 → 88% (when formatted to two decimal places)
12.2. Code snippet performing this calculation
📝 Line-by-Line Explanation
Linha1 >>> score_client = 2
Defines the client’s credit score as 2.
Linha2 >>> prob_default_manual = sigmoid_manual(score_client)
Calls the sigmoid_manual function, passing score_client as parameter.
What happens inside the function:
✔️ Internal steps:
• Calculates np.exp(-2) = e^{-2} ≈ 0.1353
• Adds 1 + 0.1353 = 1.1353
• Calculates 1 / 1.1353 ≈ 0.8808
Linha3 >>> print(f"✅ [Manual] Probability of default (Score=2): {prob_default_manual:.2f}")
Prints the result: • {prob_default_manual:.2f} Formats the number to two decimals, resulting in 0.88 (88%).
12.3. Why was score = 2 used in the example?
✔️ Didactic explanation:
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.
12.4. Final Didactic Summary
13. 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.
14. Advanced Insights and Strategic Suggestions
🔬 Suggestions:
Combine sigmoid with Logistic Regression for interpretable models.
Adjust thresholds according to business risk appetite.
Use Softmax for multiclass problems instead of sigmoid.
15. 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.
16. References
NG, Andrew. Machine Learning. Coursera, Stanford University. Available at: https://www.coursera.org/learn/machine-learning. Accessed on: July 4, 2025.
GOODFELLOW, Ian; BENGIO, Yoshua; COURVILLE, Aaron. Deep Learning. MIT Press, 2016. Available at: https://www.deeplearningbook.org. Accessed on: July 4, 2025.
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.
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.
17. CTA – Follow & Connect
💼 LinkedIn & Newsletters: 👉 https://www.linkedin.com/in/izairton-oliveira-de-vasconcelos-a1916351/ 👉 https://www.linkedin.com/newsletters/scripts-em-python-produtividad-7287106727202742273 👉 https://www.linkedin.com/build-relation/newsletter-follow?entityUrn=7319069038595268608
💼 Company Page: 👉 https://www.linkedin.com/company/106356348/
💻 GitHub: 👉 https://github.com/IOVASCON
AI, Cloud Computing, Virtualization, Containerization & Orchestration, Infrastructure-as-Code, Configuration Management, Continuous Integration & Deployment, Observability, Security & Compliance.
1moIZAIRTON OLIVEIRA DE VASCONCELOS, the probabilistic approach really makes sense here. Converting those credit scores into clear probabilities gives you something the risk teams can actually work with when making decisions.
Performance Marketing | Analytics | E-commerce Expert
1moIZAIRTON OLIVEIRA DE VASCONCELOS, transforming complex data into clear decisions is truly fascinating. Thanks for sharing. 📊
AI & Automation Consultant
1moHow fascinating that data can transform credit assessment. This approach indeed demystifies complex algorithms. What a valuable resource for learners.
Fractional CFO & Accounting Partner | Helping You Scale and Build a Profitable Financial Foundation | Girl Dad | Youth Soccer Coach
1moIZAIRTON OLIVEIRA DE VASCONCELOS, what a fascinating approach to risk classification. Understanding sigmoid functions can significantly enhance our decision-making in finance. How do you see this evolving with future data trends? 📊 #MachineLearning