Tabular Classification
Scikit-learn
Joblib
fraud-detection
logistic-regression
baseline
Eval Results (legacy)
Instructions to use Marvinds/fraud-baseline-logreg with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Marvinds/fraud-baseline-logreg with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Marvinds/fraud-baseline-logreg", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Add baseline model, model card and training script
Browse files- README.md +96 -0
- metrics.json +43 -0
- model.joblib +3 -0
- model.joblib.sha256 +1 -0
- train.py +94 -0
README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: sklearn
|
| 4 |
+
pipeline_tag: tabular-classification
|
| 5 |
+
tags:
|
| 6 |
+
- fraud-detection
|
| 7 |
+
- logistic-regression
|
| 8 |
+
- baseline
|
| 9 |
+
- tabular-classification
|
| 10 |
+
datasets:
|
| 11 |
+
- Marvinds/synthetic-bank-transactions
|
| 12 |
+
metrics:
|
| 13 |
+
- roc_auc
|
| 14 |
+
- average_precision
|
| 15 |
+
model-index:
|
| 16 |
+
- name: fraud-baseline-logreg
|
| 17 |
+
results:
|
| 18 |
+
- task:
|
| 19 |
+
type: tabular-classification
|
| 20 |
+
dataset:
|
| 21 |
+
name: Synthetic Bank Transactions
|
| 22 |
+
type: Marvinds/synthetic-bank-transactions
|
| 23 |
+
metrics:
|
| 24 |
+
- type: average_precision
|
| 25 |
+
value: 0.7203
|
| 26 |
+
name: PR-AUC (time-based test split)
|
| 27 |
+
- type: roc_auc
|
| 28 |
+
value: 0.9849
|
| 29 |
+
name: ROC-AUC (time-based test split)
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
# fraud-baseline-logreg
|
| 33 |
+
|
| 34 |
+
A deliberately simple **logistic-regression baseline** for fraud detection, trained on
|
| 35 |
+
[Marvinds/synthetic-bank-transactions](https://huggingface.co/datasets/Marvinds/synthetic-bank-transactions).
|
| 36 |
+
|
| 37 |
+
## Why this project
|
| 38 |
+
|
| 39 |
+
Every later experiment I run (gradient boosting, sequence models, graph features) needs a number
|
| 40 |
+
to beat. A baseline that's transparent, quick to retrain and honestly evaluated is that number.
|
| 41 |
+
Logistic regression fits the job: its coefficients show what the model learned, and that can
|
| 42 |
+
be checked against the known fraud mechanism of the synthetic dataset.
|
| 43 |
+
|
| 44 |
+
It also practises two habits from production fraud systems:
|
| 45 |
+
- **time-based evaluation** (train on the past, test on the future), never a random split;
|
| 46 |
+
- **PR-AUC and recall at a fixed precision** instead of accuracy, because accuracy is meaningless
|
| 47 |
+
at a 0.6% fraud rate (predicting "never fraud" scores 99.4%).
|
| 48 |
+
|
| 49 |
+
## Results
|
| 50 |
+
|
| 51 |
+
Time-based split: first 80% of transactions for training, last 20% (20,000 rows, 0.6% fraud) for testing.
|
| 52 |
+
|
| 53 |
+
| Metric | Value |
|
| 54 |
+
|---|---|
|
| 55 |
+
| ROC-AUC | 0.9849 |
|
| 56 |
+
| **PR-AUC** | **0.7203** |
|
| 57 |
+
| Recall @ precision ≥ 0.5 | 0.7355 |
|
| 58 |
+
| Recall @ precision ≥ 0.8 | 0.6281 |
|
| 59 |
+
|
| 60 |
+
Strongest coefficients: `txn_count_24h` (+1.66) and `amount_vs_avg` (+1.61), which match the
|
| 61 |
+
velocity and amount-spike pattern injected by the generator. Full list is in `metrics.json`.
|
| 62 |
+
|
| 63 |
+
> These numbers come from synthetic data with a known generator. They show the pipeline works.
|
| 64 |
+
> They are **not** a claim about real-world fraud performance.
|
| 65 |
+
|
| 66 |
+
## Features
|
| 67 |
+
|
| 68 |
+
`log(amount)`, `amount_vs_avg`, `txn_count_24h`, `is_foreign`, `is_new_device`, `is_night` (00–04),
|
| 69 |
+
plus one-hot `merchant_category` and `channel`. Standard-scaled, `class_weight="balanced"`.
|
| 70 |
+
|
| 71 |
+
## Usage
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
import hashlib, joblib, pandas as pd
|
| 75 |
+
from huggingface_hub import hf_hub_download
|
| 76 |
+
|
| 77 |
+
path = hf_hub_download("Marvinds/fraud-baseline-logreg", "model.joblib")
|
| 78 |
+
expected = open(hf_hub_download("Marvinds/fraud-baseline-logreg", "model.joblib.sha256")).read().strip()
|
| 79 |
+
assert hashlib.sha256(open(path, "rb").read()).hexdigest() == expected
|
| 80 |
+
model = joblib.load(path) # pickle: only load files you trust
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
Input must have the feature columns listed above (see `features()` in `train.py`).
|
| 84 |
+
Trained with scikit-learn 1.9.1.
|
| 85 |
+
|
| 86 |
+
## Reproduce
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
uv run train.py --data path/to/transactions.csv --out model
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
## Next experiments
|
| 93 |
+
|
| 94 |
+
- [ ] HistGradientBoosting vs this baseline, same split
|
| 95 |
+
- [ ] Probability calibration and threshold selection based on review-queue capacity
|
| 96 |
+
- [ ] Performance under concept drift (dataset v2)
|
metrics.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"trained_at": "2026-11-28T17:20:59Z",
|
| 3 |
+
"sklearn_version": "1.9.1",
|
| 4 |
+
"features": [
|
| 5 |
+
"log_amount",
|
| 6 |
+
"amount_vs_avg",
|
| 7 |
+
"txn_count_24h",
|
| 8 |
+
"is_foreign",
|
| 9 |
+
"is_new_device",
|
| 10 |
+
"is_night",
|
| 11 |
+
"merchant_category",
|
| 12 |
+
"channel"
|
| 13 |
+
],
|
| 14 |
+
"metrics": {
|
| 15 |
+
"split": "time-based 80/20",
|
| 16 |
+
"test_rows": 20000,
|
| 17 |
+
"test_fraud_rate": 0.006,
|
| 18 |
+
"roc_auc": 0.9849,
|
| 19 |
+
"pr_auc": 0.7203,
|
| 20 |
+
"recall_at_precision_0.5": 0.7355,
|
| 21 |
+
"recall_at_precision_0.8": 0.6281
|
| 22 |
+
},
|
| 23 |
+
"coefficients": {
|
| 24 |
+
"num__txn_count_24h": 1.6609,
|
| 25 |
+
"num__amount_vs_avg": 1.6094,
|
| 26 |
+
"cat__channel_atm": -1.1125,
|
| 27 |
+
"cat__merchant_category_restaurant": -1.0104,
|
| 28 |
+
"cat__channel_mobile_app": -0.9101,
|
| 29 |
+
"cat__merchant_category_grocery": -0.7254,
|
| 30 |
+
"cat__channel_pos": -0.7223,
|
| 31 |
+
"cat__merchant_category_entertainment": -0.7104,
|
| 32 |
+
"cat__merchant_category_fuel": -0.6785,
|
| 33 |
+
"num__is_night": 0.5204,
|
| 34 |
+
"cat__merchant_category_bills": -0.4688,
|
| 35 |
+
"cat__merchant_category_electronics": 0.4375,
|
| 36 |
+
"num__log_amount": 0.4026,
|
| 37 |
+
"cat__merchant_category_ecommerce": 0.3475,
|
| 38 |
+
"cat__channel_online": -0.3283,
|
| 39 |
+
"num__is_new_device": 0.2803,
|
| 40 |
+
"cat__merchant_category_travel": -0.2648,
|
| 41 |
+
"num__is_foreign": 0.1998
|
| 42 |
+
}
|
| 43 |
+
}
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c60c65839f46f5cff730455994e57d0b2a01a39b4256a34d85c5ca5362379080
|
| 3 |
+
size 3969
|
model.joblib.sha256
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
c60c65839f46f5cff730455994e57d0b2a01a39b4256a34d85c5ca5362379080
|
train.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.9"
|
| 3 |
+
# dependencies = ["numpy>=1.26", "pandas>=2.1", "scikit-learn>=1.4", "joblib>=1.3"]
|
| 4 |
+
# ///
|
| 5 |
+
"""Train a logistic-regression fraud baseline on synthetic-bank-transactions.
|
| 6 |
+
|
| 7 |
+
Usage: uv run train.py --data ../01-synthetic-bank-transactions/data/transactions.csv
|
| 8 |
+
"""
|
| 9 |
+
import argparse
|
| 10 |
+
import hashlib
|
| 11 |
+
import json
|
| 12 |
+
from datetime import datetime, timezone
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import joblib
|
| 16 |
+
import numpy as np
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import sklearn
|
| 19 |
+
from sklearn.compose import ColumnTransformer
|
| 20 |
+
from sklearn.linear_model import LogisticRegression
|
| 21 |
+
from sklearn.metrics import average_precision_score, precision_recall_curve, roc_auc_score
|
| 22 |
+
from sklearn.pipeline import Pipeline
|
| 23 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 24 |
+
|
| 25 |
+
NUMERIC = ["log_amount", "amount_vs_avg", "txn_count_24h", "is_foreign", "is_new_device", "is_night"]
|
| 26 |
+
CATEGORICAL = ["merchant_category", "channel"]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def features(df: pd.DataFrame) -> pd.DataFrame:
|
| 30 |
+
out = df.copy()
|
| 31 |
+
out["log_amount"] = np.log1p(out["amount_idr"])
|
| 32 |
+
out["is_night"] = out["hour"].between(0, 4).astype(int)
|
| 33 |
+
return out[NUMERIC + CATEGORICAL]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def recall_at_precision(y, p, target):
|
| 37 |
+
precision, recall, _ = precision_recall_curve(y, p)
|
| 38 |
+
ok = recall[precision >= target]
|
| 39 |
+
return float(ok.max()) if len(ok) else 0.0
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main() -> None:
|
| 43 |
+
parser = argparse.ArgumentParser()
|
| 44 |
+
parser.add_argument("--data", type=Path, default=Path("../01-synthetic-bank-transactions/data/transactions.csv"))
|
| 45 |
+
parser.add_argument("--out", type=Path, default=Path("model"))
|
| 46 |
+
args = parser.parse_args()
|
| 47 |
+
|
| 48 |
+
df = pd.read_csv(args.data, parse_dates=["timestamp"]).sort_values("timestamp")
|
| 49 |
+
cut = int(len(df) * 0.8)
|
| 50 |
+
train, test = df.iloc[:cut], df.iloc[cut:]
|
| 51 |
+
|
| 52 |
+
pipe = Pipeline(
|
| 53 |
+
[
|
| 54 |
+
("prep", ColumnTransformer([
|
| 55 |
+
("num", StandardScaler(), NUMERIC),
|
| 56 |
+
("cat", OneHotEncoder(handle_unknown="ignore"), CATEGORICAL),
|
| 57 |
+
])),
|
| 58 |
+
("clf", LogisticRegression(max_iter=1000, class_weight="balanced")),
|
| 59 |
+
]
|
| 60 |
+
)
|
| 61 |
+
pipe.fit(features(train), train["is_fraud"])
|
| 62 |
+
p = pipe.predict_proba(features(test))[:, 1]
|
| 63 |
+
y = test["is_fraud"].to_numpy()
|
| 64 |
+
|
| 65 |
+
metrics = {
|
| 66 |
+
"split": "time-based 80/20",
|
| 67 |
+
"test_rows": int(len(test)),
|
| 68 |
+
"test_fraud_rate": round(float(y.mean()), 4),
|
| 69 |
+
"roc_auc": round(roc_auc_score(y, p), 4),
|
| 70 |
+
"pr_auc": round(average_precision_score(y, p), 4),
|
| 71 |
+
"recall_at_precision_0.5": round(recall_at_precision(y, p, 0.5), 4),
|
| 72 |
+
"recall_at_precision_0.8": round(recall_at_precision(y, p, 0.8), 4),
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
args.out.mkdir(parents=True, exist_ok=True)
|
| 76 |
+
model_path = args.out / "model.joblib"
|
| 77 |
+
joblib.dump(pipe, model_path)
|
| 78 |
+
(args.out / "model.joblib.sha256").write_text(hashlib.sha256(model_path.read_bytes()).hexdigest() + "\n")
|
| 79 |
+
|
| 80 |
+
names = pipe.named_steps["prep"].get_feature_names_out()
|
| 81 |
+
coefs = pipe.named_steps["clf"].coef_[0]
|
| 82 |
+
meta = {
|
| 83 |
+
"trained_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
| 84 |
+
"sklearn_version": sklearn.__version__,
|
| 85 |
+
"features": NUMERIC + CATEGORICAL,
|
| 86 |
+
"metrics": metrics,
|
| 87 |
+
"coefficients": {n: round(float(c), 4) for n, c in sorted(zip(names, coefs), key=lambda x: -abs(x[1]))},
|
| 88 |
+
}
|
| 89 |
+
(args.out / "metrics.json").write_text(json.dumps(meta, indent=2) + "\n")
|
| 90 |
+
print(json.dumps(metrics, indent=2))
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
main()
|