Metadata-Version: 2.4
Name: tabrag-xai-imputer
Version: 0.0.2
Summary: Retrieval-Augmented Generation for Missing Data Imputation in Tabular Data
Author-email: Arthur Dantas Mangussi <mangussiarthur@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Arthur D. Mangussi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://arthurmangussi.github.io/TabRAG-XAI-Imputer/
Project-URL: Repository, https://github.com/ArthurMangussi/TabRAG-XAI-Imputer
Keywords: imputation,missing data,RAG,LLM,tabular,scikit-learn,explainability,XAI
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: tqdm>=4.65
Requires-Dist: python-dotenv>=1.0
Provides-Extra: gemini
Requires-Dist: google-genai>=1.0; extra == "gemini"
Provides-Extra: openai
Requires-Dist: openai>=1.0; extra == "openai"
Provides-Extra: claude
Requires-Dist: anthropic>=0.40; extra == "claude"
Provides-Extra: all
Requires-Dist: google-genai>=1.0; extra == "all"
Requires-Dist: openai>=1.0; extra == "all"
Requires-Dist: anthropic>=0.40; extra == "all"
Provides-Extra: docs
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
Requires-Dist: mkdocstrings[python]>=0.25; extra == "docs"
Dynamic: license-file

# TabRAG-XAI-Imputer

> **Retrieval-Augmented Generation for Missing Data Imputation in Tabular Data**

[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![scikit-learn compatible](https://img.shields.io/badge/sklearn-compatible-orange.svg)](https://scikit-learn.org/)

**TabRAG-XAI-Imputer** is a novel imputation framework that combines the structural awareness of correlation-weighted retrieval with the generative capabilities of Large Language Models (LLMs). Rather than relying solely on statistical distance (like KNN) or parametric knowledge (like zero-shot LLMs), TabRAG-Imputer retrieves the most relevant complete records from your dataset and feeds them as grounded context to an LLM, enabling accurate, dataset-specific imputation.

Evaluated across 20 datasets under MAR and MNAR mechanisms, TabRAG-Imputer achieves the **lowest overall MAE under MNAR** and **ranks second under MAR**, trailing only MICE.

---

## How it works

TabRAG-Imputer operates in three stages for each incomplete row:

```
Incomplete row
      │
      ▼
┌─────────────────────────────┐
│  1. Correlation-weighted    │  Ranks observed features by their
│     context retrieval       │  correlation with missing features,
│                             │  then retrieves the k most similar
│                             │  complete rows via weighted distance
└─────────────┬───────────────┘
              │  k complete rows
              ▼
┌─────────────────────────────┐
│  2. Row serialisation       │  Converts retrieved rows and the
│                             │  incomplete query into structured
│                             │  key=value text representations
└─────────────┬───────────────┘
              │  Structured prompt
              ▼
┌─────────────────────────────┐
│  3. LLM-based generation    │  LLM predicts missing values using
│                             │  retrieved context as grounding;
│                             │  output enforced as CSV for parsing
└─────────────────────────────┘
```

### Retrieval mechanism

For a query row **x*** with observed features *O* and missing features *M*, each observed feature *j* is weighted by its average absolute Pearson correlation with the missing features:

$$\bar{r}_j = \frac{1}{|M|} \sum_{m \in M} |r_{jm}|, \qquad w_j = \frac{\bar{r}_j + \epsilon}{\sum_{l \in O}(\bar{r}_l + \epsilon)}$$

The *k* nearest complete rows are retrieved using weighted masked Euclidean distance:

$$d(\mathbf{x}^*, \mathbf{c}_i) = \sqrt{\sum_{j \in O} w_j \left(x^*_j - c_{ij}\right)^2}$$

---

## Installation

```bash
pip install tabrag-xai-imputer
```

Install with the extra(s) for your chosen LLM provider:

```bash
pip install "tabrag-xai-imputer[gemini]"      # Google Gemini
pip install "tabrag-xai-imputer[openai]"      # OpenAI / OpenRouter
pip install "tabrag-xai-imputer[claude]"      # Anthropic Claude
pip install "tabrag-xai-imputer[all]"         # all providers
```

### API key configuration

TabRAG-Imputer supports multiple LLM providers. Create a `.env` file in the project root and add the key(s) for the provider(s) you intend to use:

```env
# Only the key(s) you need — unused entries can be omitted
API_KEY_GEMINI=your_gemini_key_here
API_KEY_OPEN_ROUTER=your_openrouter_key_here
API_KEY_GPT=your_openai_key_here
API_KEY_CLAUDE=your_anthropic_key_here
```

> **Tip:** Gemini 3.0 Flash (`google/gemini-3-flash-preview`) is the recommended default — it offers competitive imputation accuracy at low cost.

---

## Quick start

### Imputation

```python
import pandas as pd
from tabrag_xai_imputer import RAGImputer

df = pd.read_csv("data/pima-indians-diabetes/pima_diabetes.csv")
X_train, X_test_missing = ...  # your train/test split with NaNs in X_test_missing

imputer = RAGImputer(
    n_neighbors=5,
    llm_model_name="gemini-2.0-flash",
    llm_api="gemini",               # "gemini" | "open_router" | "gpt" | "claude"
    dataset_name="Pima Indians Diabetes",
)

imputer.fit(X_train)
X_imputed = imputer.transform(X_test_missing)
```

### Explainability

After imputation, call `explain()` to get an LLM-generated paragraph for every row
that had missing values. It reuses the same retrieved neighbors, so no extra retrieval
cost is incurred.

```python
explanations = imputer.explain(X_test_missing, X_imputed)

for i, text in enumerate(explanations):
    print(f"[Row {i}] {text}\n")
```

`explain()` accepts an optional `row_indices` list if you only need a subset:

```python
explanations = imputer.explain(X_test_missing, X_imputed, row_indices=[0, 3, 7])
```

See [`examples/quickstart_explain.py`](examples/quickstart_explain.py) for a
self-contained runnable script covering the full workflow — data loading, artificial
missing-value injection, imputation, and explanation output.

---

## Key parameters

| Parameter | Type | Default | Description |
|---|---|---|---|
| `n_neighbors` | `int` | `10` | Number of complete rows retrieved as context (*k*). Higher values provide richer context but increase LLM prompt length and cost. |
| `llm_api` | `str` | `"gemini"` | LLM provider to use. Options: `"gemini"`, `"open_router"`, `"gpt"`, `"claude"`. |
| `llm_model_name` | `str` | — | Model identifier string for the chosen provider (e.g. `"google/gemini-3-flash-preview"`). |
| `dataset_name` | `str` | `""` | Human-readable dataset name included in the prompt for context. |

> **Choosing *k*:** Our ablation study shows that *k* = 10 provides the best accuracy–cost trade-off across continuous and mixed datasets. For high-dimensional categorical datasets, *k* = 5 may be preferable to control prompt length and inference time.


---

## Citation

If you use TabRAG-XAI-Imputer in your research, please cite:

```bash
Information will appear as soon as possible
```


---

## License

This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
