File size: 6,791 Bytes
77f1d1a
 
 
 
 
 
 
 
 
 
2532605
 
c56dbf6
ede8036
c56dbf6
ede8036
c56dbf6
 
 
ede8036
c56dbf6
ede8036
c56dbf6
2532605
c56dbf6
2532605
c56dbf6
ede8036
c56dbf6
 
 
2532605
 
 
c56dbf6
ede8036
c56dbf6
 
 
 
ede8036
c56dbf6
ede8036
 
2532605
c56dbf6
 
 
2532605
c56dbf6
 
 
e0bd84b
0f0781a
 
 
 
 
 
e0bd84b
0f0781a
 
e0bd84b
0f0781a
 
 
 
 
82b5462
c56dbf6
77f1d1a
82b5462
 
 
0f0781a
2532605
c56dbf6
2532605
c56dbf6
2532605
0f0781a
 
 
 
ede8036
0f0781a
 
 
 
 
ede8036
0f0781a
 
 
 
ede8036
2532605
 
c56dbf6
 
 
 
 
 
 
0f0781a
c56dbf6
ede8036
2532605
 
0f0781a
2532605
0f0781a
2532605
0f0781a
2532605
344a7cd
2532605
 
0f0781a
2532605
 
 
0f0781a
2532605
0f0781a
2532605
 
0f0781a
2532605
 
 
77f1d1a
0f0781a
2532605
 
0f0781a
2532605
0f0781a
ede8036
 
 
0f0781a
ede8036
0f0781a
ede8036
0f0781a
 
 
2532605
0f0781a
 
 
 
ede8036
0f0781a
 
 
 
2532605
 
 
c56dbf6
ede8036
c56dbf6
 
ede8036
c56dbf6
ede8036
c56dbf6
 
 
 
ede8036
0f0781a
c56dbf6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
title: Kronector
emoji: 🏎️
colorFrom: red
colorTo: gray
sdk: docker
app_port: 7860
pinned: false
---

<div align="center">

# Kronector: F1 Race Outcome Prediction & MLOps System

*An End-to-End Machine Learning Pipeline for Formula 1 Strategy and Intelligence.*

[![Live Demo](https://img.shields.io/badge/Live_App-Ready-00f0ff?style=for-the-badge&logo=vercel&logoColor=black)](#)
[![API Docs](https://img.shields.io/badge/API_Docs-Swagger-009688?style=for-the-badge&logo=fastapi&logoColor=white)](#)
[![Technical Report](https://img.shields.io/badge/Technical_Report-Read_Here-b026ff?style=for-the-badge&logo=markdown&logoColor=white)](TECHNICAL_REPORT.md)

<img src="assets/vercel_prediction.webp" alt="Kronector Dashboard UI" width="80%">

</div>

---

## 🎯 Why This Project Exists

Predicting Formula 1 is notoriously difficult due to chaotic variance (weather, safety cars, mechanical failures). Most public projects use basic scikit-learn models on small, un-normalized datasets. 

**Kronector** was built to demonstrate how a **production-grade ML system** tackles this problem. It solves critical engineering challenges: preventing temporal data leakage, handling regulation-era normalization, and serving sub-second inference alongside SHAP-based mathematical explainability through a natural language interface.

---

## ⚠️ Known Limitations

Having a rigorous model means being transparent about its blind spots. Currently, Kronector struggles with:
1. **Safety Cars & Red Flags:** The model cannot dynamically predict sudden race neutralizations which reset field gaps.
2. **Mechanical Failures (DNFs):** Engine failures and random reliability issues are treated as statistical noise.
3. **Weather Chaos:** Sudden rain introduces extreme variance that tabular historical models struggle to adapt to dynamically.

*Roadmap: Future iterations will ingest live radar weather data and historical Safety Car probability distributions per track.*

---

## πŸ“Š Results & Performance

The model is trained on **4,400+ driver-race instances** (covering all 20 cars across every race from 2014 to 2026).

### Validation Methodology
- **Leakage Prevention:** We strictly use `TimeSeriesSplit(n=5)`. A model is trained on years $T_0 \dots T_n$ and evaluated strictly on $T_{n+1}$.
- **Accuracy Calculation:** "Winner Accuracy" is calculated as the **Top-1 Prediction** (does the driver with the highest predicted probability actually win the race?).

### Season-by-Season Out-of-Sample Performance
| Season | Winner Accuracy | Podium Accuracy |
|--------|-----------------|-----------------|
| 2023   | 86.3%           | 73.1%           |
| 2024   | 68.4%           | 64.2%           |
| 2025   | 71.8%           | 67.5%           |

### Benchmarking vs Simple Baselines
To prove the model captures complex non-linear relationships rather than just predicting the favorite, we benchmark against naive heuristics over the 2023-2025 holdout set:

| Model / Baseline | Accuracy |
|------------------|----------|
| **Kronector LightGBM** | **~71%** |
| Current WDC Leader Wins | ~51% |
| Pole Position Wins | ~42% |

### Feature Importance & ROC Proofs
*Mathematical proof that the model relies on logical racing factors can be regenerated with `python -m scripts.generate_model_proofs`.*

---

## πŸ—οΈ System Architecture

Kronector implements a complete MLOps lifecycle: data ingestion, feature engineering, model training, MLflow tracking, API serving, and Evidently AI drift monitoring.

*(Note: A high-resolution PNG architecture diagram is coming soon.)*
```mermaid
graph TD
    subgraph Data Layer
        A[FastF1 API] --> C(Feature Pipeline)
        B[Jolpica API] --> C
    end
    
    subgraph MLOps
        C --> D{LightGBM Train}
        D <--> E[(MLflow Registry)]
        D --> F[Evidently AI Monitor]
    end
    
    subgraph Serving
        E --> G[FastAPI Backend]
        G --> H[Multi-Agent LLM]
    end
```

### The 4-Agent LLM Architecture
A raw probability output (e.g., "0.45") is not actionable for a strategist. We wrap the LightGBM inference engine in a 4-stage Agentic Pipeline to provide explainability:
1. **🧠 DataAgent:** Parses natural language into a strict JSON intent (resolving "Monaco 23" to `season: 2023, round: 6`).
2. **βš™οΈ PredictionAgent:** Executes the LightGBM model and extracts mathematical SHAP values.
3. **πŸ›‘οΈ CritiqueAgent:** A pure-Python deterministic safeguard. It rejects predictions where confidence is below 20% (statistical noise in a 20-car field).
4. **πŸ“» SynthesisAgent:** Translates the math and SHAP values into a natural language response formatted as a radio message.

### Why LightGBM?
We evaluated Neural Networks, XGBoost, and CatBoost. **LightGBM** was selected because it natively handles categorical variables (e.g., driver and team IDs) natively without creating sparse one-hot encoded matrices, and it integrates seamlessly with `shap.TreeExplainer` for sub-millisecond feature importance extraction in production.

---

## πŸ› οΈ Quick Start & Reproducibility

To ensure reproducibility, the entire pipeline can be run locally. Note that the 26GB+ raw telemetry cache is excluded from Git. 

### 1. Installation
```bash
git clone https://github.com/prats010/kronector.git
cd kronector
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements.txt
```

### 2. Download Data & Train
```bash
# Generate canonical driver IDs
python -m data.build_driver_map

# Run full pipeline: ingest data -> engineer features -> train LightGBM -> log to MLflow
python -m scripts.auto_retrain_pipeline
```

### 3. Run the API
Create a `.env` file with `GROQ_API_KEY=your_key` and the `KRONECTOR_MODEL_RUN_ID` outputted by the training script.

```bash
# Start backend
python -m uvicorn api.main:app --reload
```

---

## πŸ“‘ API Usage

The FastAPI backend exposes endpoints for programmatic access. 

### Python Requests Example
```python
import requests

response = requests.post(
    "http://localhost:8000/predict/f1",
    json={"query": "Who will win the 2026 Canadian GP?"}
)

data = response.json()
print(f"Predicted Win Probability: {data['win_probability'] * 100}%")
print(f"SHAP Key Factors: {data['shap_values']}")
```

---

## πŸ‘¨β€πŸ’» About the Author

**Prathamesh Anil Bhamare**  
*MSc Computer Science Student | Machine Learning Engineer*

I built Kronector combining a passion for Formula 1 strategy with rigorous Data Science and MLOps principles.

- πŸ’Ό [LinkedIn](https://linkedin.com/in/prats010)
- 🌐 [Portfolio](#)
- πŸ“„ [Read the Technical Report](TECHNICAL_REPORT.md)
- πŸ“§ [Contact Me](mailto:example@email.com)

---
<div align="center">
<sub>Built for the passion of racing and the pursuit of perfect data.</sub>
</div>