# dhanuk8778-netizen/predictive_models architecture

> Architecture diagram of the GitHub repository dhanuk8778-netizen/predictive_models, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhanuk8778-netizen/predictive_models
- Repository: https://github.com/dhanuk8778-netizen/predictive_models

## Overview

#### Purpose and Entry Points
This repository is a batch ML workflow for predicting industrial machine failure from AI4I 2020 sensor data. It is executed as ordered Python scripts from `src/`, beginning with preparation and ending with evaluation and SHAP interpretation. Source data is local at `data/ai4i2020.csv`; generated visual artifacts are written under `output/figures`.

#### 1. Data Ingestion and Label Validation
`src/data_prep.py` loads and cleans the dataset, validates the binary machine-failure target, and establishes the dataset consumed by later stages. A critical boundary is between the target and failure-subtype flags: subtype labels are post-failure information and must not enter predictive training.
- `src/data_prep.py`
- `data/ai4i2020.csv`

#### 2. Exploratory Validation
`src/eda.py` examines class prevalence, feature distributions, and correlations. This stage may use the subtype flags to validate target semantics, but it is analytical only and must not feed leakage-prone columns into modeling.
- `src/eda.py`
- `output/figures`

#### 3. Leakage-Safe Feature Construction
`src/features.py` removes post-outcome subtype flags, creates physically motivated predictors, and creates stratified train/test partitions. Core engineered signals include temperature difference, torque × rotational-speed power proxy, and torque/wear interaction. The untouched test partition is the boundary for final generalization measurement.
- `src/features.py`
- `src/data_prep.py`

#### 4. Imbalance-Aware Model Training and Selection
`src/train.py` trains Random Forest and XGBoost candidates using stratified 5-fold cross-validation and randomized hyperparameter search. Selection centers on PR-AUC because failure is rare (~3.39%), making accuracy misleading. Imbalance is handled in-model through Random Forest class weights and XGBoost positive-class weighting rather than resampling.
- `src/train.py`
- `requirements.txt`

#### 5. Held-Out Evaluation and Operating Thresholds
`src/evaluate.py` evaluates selected models against the held-out test set using confusion matrices, ROC/PR curves, ROC-AUC, PR-AUC, precision, and recall. It also derives XGBoost operating points at target recall levels; threshold choice is a business-risk boundary between missed failures and false maintenance alarms.
- `src/evaluate.py`
- `output/figures`

#### 6. Model Explainability
`src/shap_analysis.py` produces global and per-prediction SHAP explanations for the trained model. These outputs validate whether learned behavior aligns with known physical failure mechanisms, particularly tool wear, speed, and power-related features.
- `src/shap_analysis.py`
- `output/figures/shap_summary.png`
- `output/figures/shap_waterfall_example.png`

#### Runtime and Dependencies
The system is a local Python, file-oriented pipeline with no service, database, or cloud infrastructure. Material external inputs are the UCI AI4I dataset stored locally; material ML/runtime dependencies are defined in `requirements.txt`, including the Random Forest/XGBoost and SHAP ecosystem implied by the workflow.
- `requirements.txt`
- `data/ai4i2020.csv`

Main entry points: [`src/train.py`](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/train.py) (Candidate training & selection), [`src/shap_analysis.py`](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/shap_analysis.py) (SHAP analysis), [`src/features.py`](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/features.py) (Feature construction), [`src/evaluate.py`](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/evaluate.py) (Held-out evaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data &amp; validation"]
  node_dataset[("AI4I 2020 sensor data<br/>CSV input<br/>[ai4i2020.csv]")]
  node_prep["Data preparation<br/>batch script<br/>[data_prep.py]"]
  node_validated_data[("Validated dataset<br/>pipeline dataset")]
  node_eda["Exploratory validation<br/>batch script<br/>[eda.py]"]
  node_feature_figures["EDA figures<br/>visual artifacts"]
end

subgraph group_features["Feature boundary"]
  node_features["Feature construction<br/>batch script<br/>[features.py]"]
  node_train_partition[("Stratified training partition<br/>model dataset")]
  node_heldout_test[("Untouched test partition<br/>evaluation dataset")]
end

subgraph group_modeling["Modeling &amp; evaluation"]
  node_train["Candidate training &amp; selection<br/>batch script<br/>[train.py]"]
  node_rf_model{{"Random Forest candidate<br/>classifier"}}
  node_xgb_model{{"XGBoost candidate<br/>classifier"}}
  node_selected_model{{"Selected trained model<br/>model artifact"}}
  node_evaluate["Held-out evaluation<br/>batch script<br/>[evaluate.py]"]
  node_evaluation_figures["Evaluation figures<br/>visual artifacts"]
end

subgraph group_outputs["Interpretation outputs"]
  node_shap["SHAP analysis<br/>batch script<br/>[shap_analysis.py]"]
  node_shap_summary["SHAP summary<br/>explanation figure"]
  node_shap_waterfall["SHAP waterfall example<br/>explanation figure"]
end

node_requirements["ML dependencies<br/>Python requirements<br/>[requirements.txt]"]

node_dataset -->|"load"| node_prep
node_prep -->|"clean and validate"| node_validated_data
node_validated_data -->|"analyze"| node_eda
node_eda -->|"write"| node_feature_figures
node_validated_data -->|"construct leakage-safe features"| node_features
node_features -->|"stratified split"| node_train_partition
node_features -->|"reserve"| node_heldout_test
node_requirements -.->|"provides ML stack"| node_train
node_train_partition -->|"cross-validate and tune"| node_train
node_train -->|"train"| node_rf_model
node_train -->|"train"| node_xgb_model
node_rf_model -->|"compare by PR-AUC"| node_selected_model
node_xgb_model -->|"compare by PR-AUC"| node_selected_model
node_selected_model -->|"score"| node_evaluate
node_heldout_test -->|"held-out measurement"| node_evaluate
node_evaluate -->|"write curves and matrices"| node_evaluation_figures
node_selected_model -->|"explain"| node_shap
node_heldout_test -->|"explain predictions"| node_shap
node_shap -->|"write"| node_shap_summary
node_shap -->|"write"| node_shap_waterfall

click node_dataset "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/data/ai4i2020.csv"
click node_requirements "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/requirements.txt"
click node_prep "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/data_prep.py"
click node_eda "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/eda.py"
click node_feature_figures "https://github.com/dhanuk8778-netizen/predictive_models/tree/main/output/figures"
click node_features "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/features.py"
click node_train "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/train.py"
click node_evaluate "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/evaluate.py"
click node_evaluation_figures "https://github.com/dhanuk8778-netizen/predictive_models/tree/main/output/figures"
click node_shap "https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/shap_analysis.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dataset,node_prep,node_validated_data,node_eda,node_feature_figures toneBlue
class node_features,node_train_partition,node_heldout_test toneAmber
class node_train,node_rf_model,node_xgb_model,node_selected_model,node_evaluate,node_evaluation_figures toneMint
class node_shap,node_shap_summary,node_shap_waterfall toneRose
class node_requirements toneNeutral
```

## Components

### Data & validation

- [AI4I 2020 sensor data](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/data/ai4i2020.csv): CSV input · `data/ai4i2020.csv`
- [Data preparation](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/data_prep.py): batch script · Cleans data and validates the binary failure target. · `src/data_prep.py`
- **Validated dataset**: pipeline dataset · Keeps failure subtype flags distinct from the predictive target.
- [Exploratory validation](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/eda.py): batch script · Uses subtype flags only for analytical target validation. · `src/eda.py`
- [EDA figures](https://github.com/dhanuk8778-netizen/predictive_models/tree/main/output/figures): visual artifacts · `output/figures`

### Feature boundary

- [Feature construction](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/features.py): batch script · Removes post-outcome flags and engineers physical predictors. · `src/features.py`
- **Stratified training partition**: model dataset
- **Untouched test partition**: evaluation dataset · Reserved for final generalization measurement.

### Modeling & evaluation

- [Candidate training & selection](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/train.py): batch script · Runs stratified 5-fold randomized search using PR-AUC. · `src/train.py`
- **Random Forest candidate**: classifier · Uses class weighting for imbalance.
- **XGBoost candidate**: classifier · Uses positive-class weighting for imbalance.
- **Selected trained model**: model artifact · Selected by cross-validated PR-AUC.
- [Held-out evaluation](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/evaluate.py): batch script · Computes metrics and recall-targeted operating thresholds. · `src/evaluate.py`
- [Evaluation figures](https://github.com/dhanuk8778-netizen/predictive_models/tree/main/output/figures): visual artifacts · `output/figures`

### Interpretation outputs

- [SHAP analysis](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/src/shap_analysis.py): batch script · Explains global and individual trained-model predictions. · `src/shap_analysis.py`
- **SHAP summary**: explanation figure
- **SHAP waterfall example**: explanation figure

### Other components

- [ML dependencies](https://github.com/dhanuk8778-netizen/predictive_models/blob/main/requirements.txt): Python requirements · `requirements.txt`

## Connections

- AI4I 2020 sensor data → Data preparation: load
- Data preparation → Validated dataset: clean and validate
- Validated dataset → Exploratory validation: analyze
- Exploratory validation → EDA figures: write
- Validated dataset → Feature construction: construct leakage-safe features
- Feature construction → Stratified training partition: stratified split
- Feature construction → Untouched test partition: reserve
- ML dependencies → Candidate training & selection: provides ML stack
- Stratified training partition → Candidate training & selection: cross-validate and tune
- Candidate training & selection → Random Forest candidate: train
- Candidate training & selection → XGBoost candidate: train
- Random Forest candidate → Selected trained model: compare by PR-AUC
- XGBoost candidate → Selected trained model: compare by PR-AUC
- Selected trained model → Held-out evaluation: score
- Untouched test partition → Held-out evaluation: held-out measurement
- Held-out evaluation → Evaluation figures: write curves and matrices
- Selected trained model → SHAP analysis: explain
- Untouched test partition → SHAP analysis: explain predictions
- SHAP analysis → SHAP summary: write
- SHAP analysis → SHAP waterfall example: write
