An end-to-end machine learning system that predicts financial distress in companies using a hybrid approach combining the Altman Z-Score with key financial ratios. Trained on real Egyptian stock market data, with an interactive Streamlit dashboard for company-level risk analysis and LIME-based explainability.
| Model | Accuracy | ROC-AUC | F1-Score |
|---|---|---|---|
| LightGBM ⭐ | 97.66% | 99.98% | 97.48% |
| Random Forest | 96.09% | 99.80% | 95.87% |
| SVM | 80.47% | 90.25% | 77.88% |
| Logistic Regression | 76.56% | 88.18% | 75.00% |
Best model: LightGBM with ROC-AUC of 99.98% and 5-fold cross-validation stability (std = 0.002)
- Hybrid distress labeling — combines Altman Z-Score (40%), profitability (20%), liquidity (15%), leverage (15%), and ROA (10%) into a single risk score
- Full ML pipeline — preprocessing → feature engineering → feature selection → model training → evaluation
- Multi-model comparison — LightGBM, Random Forest, SVM, Logistic Regression
- LIME explainability — individual prediction explanations showing which financial factors drive the risk score
- Interactive Streamlit app — company-level risk profiling with real-time prediction, visualizations, and sector filtering
- Extensive visualizations — clustering analysis (K-Means, DBSCAN, GMM, Hierarchical), PCA, correlation heatmaps, feature importance plots, ROC curves
AI-based-early-warning-system/
├── app.py # Streamlit dashboard (main entry point)
├── src/
│ ├── data_preprocessing.py # Data cleaning, normalization, PCA
│ ├── feature_engineering.py # Temporal, interaction, statistical & ratio features
│ ├── feature_selection.py # Filter, wrapper, embedded & ensemble selection methods
│ └── model_training.py # Hybrid label creation, model training, evaluation
├── images/ # All generated visualizations
│ ├── model_comparison.png
│ ├── roc_curve.png
│ ├── feature_importance_lightgbm.png
│ ├── feature_importance_random_forest.png
│ ├── pca_visualization.png
│ ├── k-means clustering (k=10).png
│ └── ...
├── data/
│ └── model_results.csv # Saved model performance metrics
└── requirements.txt
Note: The trained model files (
models/) and processed data files (data/*.xlsx) are not included in this repository. See the Setup section to reproduce them from scratch.
git clone https://github.com/YOUR_USERNAME/AI-based-early-warning-system.git
cd AI-based-early-warning-systempip install -r requirements.txtPlace your financial dataset (Excel file with company financial ratios and Altman Z-Score) in the data/ folder. The file should include columns for: sector, firm, years, altman_z_score, liquidity ratios, leverage ratios, and profitability metrics.
# Step 1 — preprocess raw data
python src/data_preprocessing.py
# Step 2 — engineer features
python src/feature_engineering.py
# Step 3 — select best features
python src/feature_selection.py
# Step 4 — train and evaluate models (saves models/ and artifacts/)
python src/model_training.pystreamlit run app.pyRaw Financial Data (.xlsx)
↓
data_preprocessing.py
• Fill missing values (median imputation)
• Outlier removal (Z-score threshold > 3)
• Normalization (StandardScaler)
• PCA for dimensionality reduction
↓
feature_engineering.py
• Temporal features (lag 1, lag 2, rolling mean/std)
• Interaction features (pairwise products, squared terms)
• Statistical features (Z-scores, percentile ranks)
• Financial ratios (current ratio, ROA, debt ratio, asset turnover)
• Hybrid risk features (Altman-based distress labels)
↓
feature_selection.py
• Filter methods: Variance threshold, ANOVA F-test, Mutual Information
• Wrapper methods: Random Forest, Extra Trees, L1 Logistic Regression
• Embedded methods: RFE, SelectFromModel
• Ensemble scoring + stability selection across 10 iterations
↓
model_training.py
• Hybrid distress label creation (5-factor weighted scoring)
• Train/test split with StratifiedKFold cross-validation
• Train: LightGBM, Random Forest, SVM, Logistic Regression
• Save best model + scaler + feature names + optimal threshold
↓
Streamlit App (app.py)
• Company selector by sector
• Risk score + LIME explanation
• Historical trend visualization
ROC Curve |
LightGBM Feature Importance |
K-Means Clustering (k=10) |
PCA Visualization |
| Category | Libraries |
|---|---|
| ML Models | scikit-learn, lightgbm, xgboost |
| Data Processing | pandas, numpy, scipy |
| Explainability | lime |
| Visualization | matplotlib, seaborn, plotly |
| Dashboard | streamlit |
| Serialization | joblib |
streamlit
pandas
numpy
scikit-learn
lightgbm
xgboost
plotly
lime
joblib
openpyxl
scipy
seaborn
matplotlib
Mirna Walid
AI Science Student — Galala University, Faculty of Computer Science & Engineering
LinkedIn · GitHub · Kaggle