A machine-learning-powered web application that predicts a user's health risk level (Low / Medium / High) based on lifestyle inputs such as age, BMI, sleep, exercise, smoking, and profession. Generates a detailed visual report with charts.
| Component | Technology |
|---|---|
| Backend | Python, Flask |
| ML Model | scikit-learn — Random Forest Classifier |
| Data | Synthetic lifestyle/health dataset (CSV, ~270K rows) |
| Preprocessing | StandardScaler, LabelEncoder, One-Hot Encoding |
| Charts | Matplotlib (base64-encoded PNGs served inline) |
| Frontend | Jinja2 HTML templates, custom CSS |
| Persistence | Flask session, joblib model serialisation |
- Dataset —
Lifestyle_and_Health_Risk_Prediction_Synthetic_Dataset.csvwith features: age, weight, height, BMI, sleep, exercise type, smoking, alcohol, marital status, profession. - Preprocessing — missing value imputation, label encoding for binary columns, one-hot encoding for categorical columns (exercise, profession).
- Model —
RandomForestClassifiertrained and saved tomodel/rf_model.pklviajoblib. - Scaler —
StandardScalersaved tomodel/scaler.pklto normalise inference inputs. - Label Encoder —
le_risk.pklmaps numeric predictions back to Low / Medium / High labels. - Mappings —
mappings.pklstores encoding dictionaries for categorical inputs.
- After prediction, Matplotlib generates multiple inline charts (risk gauge, feature breakdown, comparison bars).
- Charts are converted to base64 PNG strings and passed to the Jinja2 template — no file writes, no temp files.
- Results page provides a full health report with personalised insights.
- Flask
sessionstores the user's form inputs and prediction result across the multi-page form flow. - No database required — fully stateless between sessions.
health-level-prediction-system/
│
├── app.py # Flask app — routes, prediction logic, chart generation
├── train_model.py # Training script — run once to generate model files
├── main.py # App entry point / runner
│
├── model/ # Serialised ML artefacts (generated by train_model.py)
│ ├── rf_model.pkl # Trained Random Forest model
│ ├── scaler.pkl # StandardScaler
│ ├── feature_columns.pkl # Ordered feature list for inference
│ ├── le_risk.pkl # LabelEncoder for risk level
│ └── mappings.pkl # Encoding maps for categorical inputs
│
├── Lifestyle_and_Health_Risk_Prediction_Synthetic_Dataset.csv
│
├── project_final.ipynb # Jupyter notebook — EDA + model experiments
│
├── templates/
│ ├── base.html # Base layout
│ ├── welcome.html # Landing / intro page
│ ├── form.html # Multi-section input form
│ └── results.html # Prediction report with embedded charts
│
├── static/
│ └── css/
│ └── style.css # Custom styles (green colour palette)
│
└── screenshots/
| Method | Route | Description |
|---|---|---|
| GET | / |
Welcome / landing page |
| GET | /form |
Render lifestyle input form |
| POST | /predict |
Process form, run model, redirect to results |
| GET | /results |
Show prediction report with charts |
pip install flask scikit-learn pandas numpy matplotlib joblibpython train_model.pyThis generates all files in the model/ directory.
python app.py
# or
python main.pyVisit http://localhost:5000
PALETTE = {
"lightest": "#EEFAF6",
"light": "#C8EFE1",
"mid": "#5CC9A8",
"dark": "#12352C",
}




