Skip to main content

TabRAG-XAI-Imputer

Retrieval-Augmented Generation for Missing Data Imputation in Tabular Data

Python 3.8+ License: MIT scikit-learn compatible

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

pip install tabrag-xai-imputer

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

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:

# 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

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.

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:

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

See 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:

Information will appear as soon as possible

License

This project is licensed under the MIT License. See LICENSE for details.

Metadata

Release files for tabrag-xai-imputer 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tabrag-xai-imputer 0.0.2
File Size Uploaded Provenance
tabrag_xai_imputer-0.0.2.tar.gz 16.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tabrag-xai-imputer 0.0.2
File Interpreter ABI Platform
tabrag_xai_imputer-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.8 kB

Release files / tabrag_xai_imputer-0.0.2.tar.gz

Download URL tabrag_xai_imputer-0.0.2.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e6035463f254277e7b5a49e02b6b60d95e92061531e7d37aa7c997e68926b79e
BLAKE2b-256 checksum
How to use checksums
b5d5274f7a15e94e26d4761d9f09e0bc5073884729376d29dd8792805796ed74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / tabrag_xai_imputer-0.0.2-py3-none-any.whl

Download URL tabrag_xai_imputer-0.0.2-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92b7df47be6420c57098256d6eff149bc33add1e3591922ee55e9425a060a3bd
BLAKE2b-256 checksum
How to use checksums
9978ea61b70e103fcf34d14edc6fd36a1139d49fca6ad8bfd7e6a7d088cf3185
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for tabrag-xai-imputer 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page