Metadata-Version: 2.5
Name: tabular-model-robustness
Version: 0.1.0
Summary: How cheaply can an attacker get past your fraud, bot or abuse model? Cost-aware evasion tests for XGBoost, LightGBM and scikit-learn models with realistic attacker constraints, plus hardening (adversarial training, feature removal) with the accuracy, robustness and inference-energy trade-off measured.
Project-URL: Homepage, https://www.ahmadzia.com/tools/tabular-model-robustness
Project-URL: Documentation, https://www.ahmadzia.com/tools/tabular-model-robustness
Project-URL: Source, https://github.com/MuhammadAhmadZia/tabular-model-robustness
Project-URL: Issues, https://github.com/MuhammadAhmadZia/tabular-model-robustness/issues
Project-URL: Changelog, https://github.com/MuhammadAhmadZia/tabular-model-robustness/blob/main/CHANGELOG.md
Project-URL: Author, https://www.ahmadzia.com
Project-URL: Research, https://www.ahmadzia.com/research
Author-email: Muhammad Ahmad Zia <muhammad.zia@cs.uol.edu.pk>
License-Expression: MIT
License-File: LICENSE
Keywords: adversarial-machine-learning,bot-detection,evasion,fraud-detection,gradient-boosting,green-ai,lightgbm,ml-security,robustness,security,tabular,xgboost
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Security
Requires-Python: >=3.9
Requires-Dist: numpy>=1.21
Requires-Dist: scikit-learn>=1.0
Provides-Extra: all
Requires-Dist: greeninfer>=0.1.0; extra == 'all'
Requires-Dist: joblib>=1.1; extra == 'all'
Requires-Dist: lightgbm>=3.3; extra == 'all'
Requires-Dist: pandas>=1.3; extra == 'all'
Requires-Dist: xgboost>=1.7; extra == 'all'
Provides-Extra: cli
Requires-Dist: greeninfer>=0.1.0; extra == 'cli'
Requires-Dist: pandas>=1.3; extra == 'cli'
Provides-Extra: energy
Requires-Dist: greeninfer>=0.1.0; extra == 'energy'
Provides-Extra: test
Requires-Dist: pandas>=1.3; extra == 'test'
Requires-Dist: pytest>=7; extra == 'test'
Description-Content-Type: text/markdown

# tabular-model-robustness

<!-- badges -->
[![PyPI](https://img.shields.io/pypi/v/tabular-model-robustness?color=3a7a4e&label=PyPI)](https://pypi.org/project/tabular-model-robustness/) [![CI](https://github.com/MuhammadAhmadZia/tabular-model-robustness/actions/workflows/ci.yml/badge.svg)](https://github.com/MuhammadAhmadZia/tabular-model-robustness/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-0d2018)](LICENSE) [![Agent skill](https://img.shields.io/badge/agent%20skill-tabular--model--robustness-c5dfc9)](skills/tabular-model-robustness/SKILL.md) [![Guide](https://img.shields.io/badge/guide-ahmadzia.com-3a7a4e)](https://www.ahmadzia.com/tools/tabular-model-robustness)

**How cheaply can an attacker get past your fraud, bot or abuse model?** This tool attacks your own XGBoost, LightGBM or scikit-learn model the way a real attacker would: changing only what they control, within realistic limits, and counting what it costs them. Then it hardens the model and shows the trade-off between accuracy, robustness and inference energy.

```bash
pip install "tabular-model-robustness[all]"
tabular-model-robustness attack --model bot_model.json --data recent_traffic.csv --label is_bot --constraints bot-constraints.json --budgets 1,5,10
tabular-model-robustness demo
```

```
Robustness, accuracy and inference cost

  model                     AUC  recall  evaded  median cost  ms/pred  J/1k rows
  original                0.968     94%    100%          0.7    1.381     0.6596
  adversarial training    0.968     94%     68%         3.29    1.859     0.7539
  without mouse_events    0.968     94%    100%         2.04    1.907     0.7486
  original: evaded at budget 1 = 64%, 5 = 100%, 10 = 100%
  adversarial training: evaded at budget 1 = 10%, 5 = 57%, 10 = 68%
  without mouse_events: evaded at budget 1 = 7%, 5 = 100%, 10 = 100%
```

(The built-in demo: a synthetic bot-detection dataset in which the model leans on signals that are cheap to fake. Same accuracy, very different robustness.)

## Why

Fraud and bot models are tested on accuracy. Attackers do not care about accuracy: they care about what it costs to get a fraudulent order or a scraping bot through. In the demo, a model with 0.97 AUC lets 64% of bots through when each bot spends one unit of effort, mostly by faking a few mouse events.

Tree ensembles are known to be easy to evade: Kantchelian, Tygar and Joseph (ICML 2016) found boosted trees more brittle than the other model families they tested, and showed that retraining on evading examples ("adversarial boosting") makes them much harder to evade. In tabular data, what matters is the attacker's cost, not how "close" the fake input is to a real one (Kireev, Kulynych and Troncoso, NDSS 2023). This tool is built around that.

## Describe the attacker

A JSON file lists the features the attacker controls and what changing them costs (`examples/bot-constraints.json`):

```json
{
  "budget": 10,
  "features": {
    "requests_per_min": {"min": 0, "direction": "decrease", "cost": 0.05},
    "mouse_events": {"min": 0, "direction": "increase", "integer": true, "cost": 0.05},
    "distinct_ips_24h": {"min": 1, "direction": "decrease", "integer": true, "cost": 3},
    "account_age_days": {"mutable": false}
  }
}
```

| Field | Meaning |
|---|---|
| `mutable` | Whether the attacker can change it. Features you list are mutable unless you say `false`; unlisted features are fixed (or set `"default_mutable": true`). |
| `min`, `max` | Allowed range |
| `max_change`, `max_change_rel` | Largest change, absolute or as a share of the current value |
| `direction` | `increase`, `decrease` or `both` (a bot can slow down but cannot make its account older) |
| `integer` | Whole numbers only |
| `cost` | Attacker cost per unit of change; `budget` caps the total |

Costs are in any unit you choose: dollars, minutes of effort, or a simple 1-to-10 scale. Agree them with the people who see the attacks.

## The attack

For each row the model flags, a greedy search changes one attacker-controlled feature at a time, picking the change that lowers the score most per unit of cost, until the row falls under the threshold or the budget runs out. For tree models the only values it tries are just either side of the model's own split points, read from the model (XGBoost, LightGBM, scikit-learn trees, forests and HistGradientBoosting), because nothing in between can change a tree ensemble's output. That makes the search fast and precise; on a one-split model it finds the exact minimum cost (tested). For other models, a grid of data values is used.

The results are **lower bounds**: a greedy search can miss a cheaper path, so a determined attacker may do better. Exact methods for tree ensembles (mixed-integer programming, Kantchelian et al. 2016) or verification (Chen et al., NeurIPS 2019) are stronger and much slower.

You get the evasion rate at each budget, the median cost to evade, which features attackers would change, and examples.

## Harden and measure the cost

```bash
tabular-model-robustness harden --model bot_model.joblib --train train.csv --test test.csv --label is_bot --constraints bot-constraints.json --budgets 1,5,10
```

- **Adversarial training:** attack training positives under the same constraints, add the evading versions with their true label, retrain, repeat (`--rounds`).
- **Dropping cheap features:** retrain without the features attacks used most (`--drop` to choose). In the demo this only moved the attack to other cheap signals: when everything the model relies on is cheap to fake, it needs signals that are costly to fake, such as account age, payment history or verified identity.

Each version is scored on held-out data (AUC, recall and false-positive rate at the threshold), attacked again, and benchmarked with [greeninfer](https://www.ahmadzia.com/tools/greeninfer) for single-prediction latency and energy per 1,000 rows. Robustness that doubles inference cost may not be worth it on a busy checkout; the table makes that visible. The quick benchmark in `harden` is noisy on shared machines; for published numbers, benchmark the final models with greeninfer and several repeats.

## From Python

```python
from tabular_robustness import Constraints, evaluate, compare
cons = Constraints.load("constraints.json", feature_names)
report = evaluate(model, X_test[y_test == 1], cons, budgets=[1, 5, 10])
print(report["evasion_rate"], report["median_cost"], report["features_used"])
```

## Responsible use

Test models you own or are allowed to test. The output shows where your own model is weak so you can fix it; the constraint file describes what an attacker can do, which you should treat as sensitive.

## SDG alignment

This tool supports **SDG target 9.1** (reliable, resilient infrastructure): fraud and bot defences that hold up under realistic attacks keep online stores and payment systems trustworthy. It also supports **target 9.5** (enhance scientific research): it measures robustness and inference energy together, a combination rarely reported in studies of defended tree models.

## Sources

- Kantchelian A., Tygar J. D., Joseph A. D. (2016). Evasion and Hardening of Tree Ensemble Classifiers. ICML. arXiv:1509.07892
- Kireev K., Kulynych B., Troncoso C. (2023). Adversarial Robustness for Tabular Data through Cost and Utility Awareness. NDSS. arXiv:2208.13058
- Chen H., Zhang H., Si S., Li Y., Boning D., Hsieh C.-J. (2019). Robustness Verification of Tree-based Models. NeurIPS.

## About

Built by [Ahmad Zia](https://www.ahmadzia.com), an AI and e-commerce consultant and researcher in efficient and secure inference for gradient-boosted tree models. MIT licensed.

<!-- ahmadzia-links -->
## Guide, help and author

- **Full guide with examples and FAQs:** [www.ahmadzia.com/tools/tabular-model-robustness](https://www.ahmadzia.com/tools/tabular-model-robustness)
- **Background reading:** [Is pickle safe? ML model file scanners compared](https://www.ahmadzia.com/blog/is-pickle-safe-ml-model-scanners-compared)
- **All 30 free tools and agent skills:** [ahmadzia.com/tools](https://www.ahmadzia.com/tools) · [ahmadzia.com/skills](https://www.ahmadzia.com/skills)
- **Want it set up, fixed or tuned for your store?** [Book a free call](https://www.ahmadzia.com/contact)

Built by **[Ahmad Zia](https://www.ahmadzia.com)** (Muhammad Ahmad Zia), an AI consultant for ecommerce since 2020 and green AI researcher at the University of Lahore. [Research](https://www.ahmadzia.com/research) · [Google Scholar](https://scholar.google.com/citations?user=sqjdX2EAAAAJ&hl=en) · [ORCID](https://orcid.org/0000-0002-3208-6246) · [LinkedIn](https://www.linkedin.com/in/muhammad-ahmadzia/) · [GitHub](https://github.com/MuhammadAhmadZia)
<!-- /ahmadzia-links -->

## More AI and store security tools

- [shopbot-shield](https://github.com/MuhammadAhmadZia/shopbot-shield): Prompt injection protection for ecommerce chatbots and AI shopping assistants
- [model-file-guard](https://github.com/MuhammadAhmadZia/model-file-guard): Scan pickle, PyTorch, joblib and other ML model files for hidden code before you load them
- [llm-store-review](https://github.com/MuhammadAhmadZia/llm-store-review): Security review of AI chatbots and agents in your code, mapped to the OWASP LLM Top 10 2026
- [rag-content-guard](https://github.com/MuhammadAhmadZia/rag-content-guard): Find hidden instructions and poisoned documents before they reach your RAG index or AI agent
- [storeguard](https://github.com/MuhammadAhmadZia/storeguard): Free security scanner for vibe-coded online stores

All 30 tools and agent skills: [github.com/MuhammadAhmadZia](https://github.com/MuhammadAhmadZia) · [ahmadzia.com/tools](https://www.ahmadzia.com/tools)
