- name
- classification
- description
- Calculate classification metrics (Accuracy, Precision, Recall, F1, AUROC) per variable in a QUIQ-format table with Ground_truth labels. Use for data quality assessment when predicted values can be compared against ground truth, generating per-variable confusion matrices and ROC curves.
- tier
- community
- category
- lydus
- parameters
- {"quiq_path":{"description":"Path to QUIQ-format CSV file containing both Value (predicted) and Ground_truth columns.","type":"string"},"save_path":{"description":"Directory path to save output files (classification_total.txt, classification_summary.csv, classification_plots/).","type":"string"}}
# Classification Metrics
Calculates classification performance metrics for each variable in a QUIQ-format table by comparing `Value` (predicted) against `Ground_truth` (true label). Used in the LYDUS quality management program to evaluate how accurately recorded values match ground truth.
## When to Use This Skill
- QUIQ table has `Ground_truth` column filled with reference labels
- To evaluate per-variable classification quality (accuracy, AUC, etc.)
- To generate confusion matrices and ROC curves per variable
## Input Requirements
A QUIQ-format CSV with:
- `Value` โ predicted/recorded value
- `Ground_truth` โ true reference label (must have at least one non-null value)
- `Original_table_name`, `Variable_name` โ used to group variables
Rows where either `Value` or `Ground_truth` is null are excluded.
## Metrics
For each `(Original_table_name, Variable_name)` group:
| Metric | Method | Note |
|--------|--------|------|
| `Accuracy` | `accuracy_score` | Overall correctness |
| `Precision` | `precision_score(average='macro')` | Per-class average |
| `Recall` | `recall_score(average='macro')` | Per-class average |
| `F1Score` | `f1_score(average='macro')` | Harmonic mean |
| `AUROC` | `roc_auc` via `LabelBinarizer` | Mean across classes (OvR) |
Final output also includes **weighted averages** of all metrics (weighted by number of samples per variable).
## Output
| File | Description |
|------|-------------|
| `classification_total.txt` | Weighted Accuracy / Precision / Recall / F1 / AUROC |
| `classification_summary.csv` | Per-variable metrics table |
| `classification_plots/{n}_{table}_{variable}.png` | Confusion matrix + ROC curve per variable |
## How to Run
```python
import os
import pandas as pd
skill_dir = os.path.dirname(os.path.abspath(__file__))
import sys; sys.path.insert(0, skill_dir)
from scripts.classification import get_classification, draw_auroc_variable_plot
quiq = pd.read_csv("/path/to/quiq.csv")
result_df, df_grouped, (w_acc, w_prec, w_rec, w_f1, w_auc) = get_classification(quiq)
save_path = "/path/to/output"
os.makedirs(f"{save_path}/classification_plots", exist_ok=True)
with open(f"{save_path}/classification_total.txt", "w") as f:
f.write(f"Weighted Accuracy = {w_acc}\n")
f.write(f"Weighted Precision = {w_prec}\n")
f.write(f"Weighted Recall = {w_rec}\n")
f.write(f"Weighted F1score = {w_f1}\n")
f.write(f"Weighted AUROC = {w_auc}\n")
result_df.to_csv(f"{save_path}/classification_summary.csv", index=False)
for n, (idx, target_df) in enumerate(df_grouped):
y_true = target_df["Ground_truth"].astype(str)
y_pred = target_df["Value"].astype(str)
draw_auroc_variable_plot(save_path, n, idx[0], idx[1], y_true, y_pred, y_true.unique())
print(f"Saved โ {save_path}")
```
### As a script with config
```yaml
# config.yaml
quiq_path: /path/to/quiq.csv
save_path: /path/to/output
```
```bash
python scripts/classification.py --config config.yaml
```
## Critical Notes
1. **Ground_truth ํ์** โ `Ground_truth` ์ปฌ๋ผ์ ๊ฐ์ด ์์ผ๋ฉด AssertionError ๋ฐ์.
2. **AUROC (binary)** โ ์ด์ง ๋ถ๋ฅ ์ `y_true_binary[:, 0]` ๊ธฐ์ค ๋จ์ผ AUC ๊ณ์ฐ. ๋ค์ค ํด๋์ค๋ OvR(One-vs-Rest) ๋ฐฉ์์ผ๋ก ํด๋์ค๋ณ AUC ํ๊ท .
3. **ํ๋กฏ ํ์ผ๋ช
** โ `re.sub(r'[\\/:*?"<>|]', ' ', name)`์ผ๋ก ํน์๋ฌธ์ ์ ๊ฑฐ ํ ์ ์ฅ.
4. **sklearn ์์กด์ฑ** โ DuckDB SQL ๋ณํ ๋ถ๊ฐ. scikit-learn, matplotlib ํ์.
## References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Fawcett, T. (2006). An introduction to ROC analysis. *Pattern Recognition Letters*, 27(8), 861โ874.
View on GitHub