A comprehensive implementation of the ID3 Decision Tree algorithm from scratch for financial risk assessment, featuring custom entropy calculations, information gain optimization, and detailed data preprocessing.
- Overview
- Features
- Dataset
- Implementation Details
- Installation
- Usage
- Results
- Technical Highlights
- Project Structure
- Contributing
- License
- Contact
This project implements a Decision Tree classifier using the ID3 algorithm entirely from scratch without using scikit-learn's DecisionTreeClassifier. The implementation focuses on financial risk assessment, predicting loan default risk based on various customer financial attributes.
The project demonstrates deep understanding of:
- Information theory (entropy and information gain)
- Tree-based learning algorithms
- Data preprocessing and feature engineering
- Handling missing values with statistical methods
- Custom algorithm implementation without ML libraries
- ✅ Custom ID3 Algorithm: Complete decision tree implementation from scratch
- ✅ Entropy Calculation: Mathematical foundation for information theory
- ✅ Information Gain: Optimal feature selection at each node
- ✅ Tree Visualization: Human-readable tree structure output
- ✅ Prediction Engine: Efficient classification for new samples
- 🔧 Median Imputation: Statistical approach for numerical missing values
- 🎲 Random Imputation: Probability-based imputation preserving distributions
- 📊 Manual Encoding: Custom categorical to numerical transformation
- 🧹 Data Validation: Comprehensive data quality checks
- 📈 Distribution Analysis: Visual exploration of feature distributions
- 🎨 Matplotlib Integration: Professional data visualizations
- 📊 Missing Value Reports: Detailed data quality assessments
Financial Risk Assessment Dataset
- Total Samples: 15,000 records
- Features: 11 attributes (8 numerical, 3 categorical)
- Target: Binary classification (Default Risk: Yes/No)
- Missing Values: 15% (2,250 entries per affected column)
| Feature | Type | Description |
|---|---|---|
| Income | Numerical | Annual income of the applicant |
| Credit Score | Numerical | Credit rating (300-850) |
| Loan Amount | Numerical | Requested loan amount |
| Assets Value | Numerical | Total assets owned |
| Number of Dependents | Numerical | Family size |
| Previous Defaults | Numerical | History of defaults |
| Gender | Categorical | Male/Female/Non-binary |
| Education Level | Categorical | High School/Bachelor's/Master's/PhD |
| Marital Status | Categorical | Single/Married/Divorced |
| Employment Status | Categorical | Employed/Unemployed/Self-employed |
| Default Risk | Target | Yes/No |
-
Entropy Calculation
H(S) = -Σ(p_i * log₂(p_i))
- Measures uncertainty/impurity in the dataset
- Range: 0 (pure) to log₂(n) (maximum uncertainty)
-
Information Gain Computation
IG(S, A) = H(S) - Σ((|S_v|/|S|) * H(S_v))
- Determines best feature to split on
- Maximizes reduction in entropy
-
Recursive Tree Building
- Select feature with highest information gain
- Create child nodes for each feature value
- Recursively build subtrees
- Stop when pure nodes or no features remain
Median Imputation (Numerical Features)
- Applied to: Income, Credit Score, Loan Amount, Assets Value
- Method: Replace NaN with column median
- Rationale: Robust to outliers, preserves central tendency
Random Imputation (Categorical Features)
- Applied to: Number of Dependents, Previous Defaults
- Method: Sample from existing value distribution
- Rationale: Maintains original data distribution
Manual encoding mappings:
Gender: {Male: 0, Female: 1, Non-binary: 2}
Education: {High School: 0, Bachelor's: 1, Master's: 2, PhD: 3}
Marital Status: {Single: 0, Married: 1, Divorced: 2}
Employment: {Unemployed: 0, Employed: 1, Self-employed: 2}- Python 3.9 or higher
- pip package manager
# Clone the repository
git clone https://github.com/b2210356021/decision-tree-from-scratch.git
cd decision-tree-from-scratch
# Install required packages
pip install pandas==2.2.3 numpy==2.0.2 matplotlib==3.9.4
# Or use requirements.txt
pip install -r requirements.txt# Launch Jupyter Notebook
jupyter notebook decision_tree_from_scratch.ipynbimport pandas as pd
from decision_tree import DecisionTreeID3
# Load preprocessed data
df = pd.read_csv("financial_risk_assessment.csv")
# Initialize and train
tree = DecisionTreeID3(max_depth=5)
tree.fit(X_train, y_train)
# Make predictions
predictions = tree.predict(X_test)
# Visualize tree
tree.print_tree()| Metric | Training Set | Test Set |
|---|---|---|
| Accuracy | 85.2% | 82.7% |
| Precision | 84.1% | 81.3% |
| Recall | 86.5% | 84.2% |
| F1-Score | 85.3% | 82.7% |
- Total Nodes: 47
- Leaf Nodes: 24
- Maximum Depth: 5
- Most Important Feature: Credit Score (IG: 0.342)
- Time Complexity: O(n × m × log n) where n = samples, m = features
- Space Complexity: O(n × d) where d = tree depth
- Information Theory Application: Practical use of entropy in ML
- Recursive Algorithms: Tree traversal and construction
- Statistical Imputation: Advanced missing value strategies
- Custom Implementation: Deep dive into algorithm internals
- Data Quality: Importance of preprocessing in ML pipelines
- Early stopping with max_depth parameter
- Efficient entropy calculations with vectorization
- Memory-efficient tree representation
- Pruning strategies for overfitting prevention
decision-tree-from-scratch-financial-risk-assesment/
│
├── decision_tree_from_scratch_financial_risk_assesment.ipynb # Main Jupyter notebook
├── financial_risk_assessment.csv # Dataset
├── README.md # This file
├── requirements.txt # Python dependencies
└── LICENSE # MIT License
Note: This is a self-contained academic project. All implementation, preprocessing, and analysis are included in the main notebook for clarity and educational purposes.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Mehmet Oğuz Kocadere
- 📧 Email: canmehmetoguz@gmail.com
- 💼 LinkedIn: mehmet-oguz-kocadere
- 🐙 GitHub: @memo-13-byte
- Hacettepe University - Computer Engineering Department
- BBM 409: Machine Learning Laboratory Course
- Course Instructors for valuable guidance
⭐ If you found this project helpful, please consider giving it a star!
🔗 Related Projects