Skip to main content

Retrieval-Augmented Generation for Missing Data Imputation in Tabular Data

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tabrag_xai_imputer-0.0.1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tabrag_xai_imputer-0.0.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file tabrag_xai_imputer-0.0.1.tar.gz.

File metadata

  • Download URL: tabrag_xai_imputer-0.0.1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for tabrag_xai_imputer-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ba9f15ec90202703aaa88c61bddd67acc0f9c3ce0923ca5e287bf93594069fc7
MD5 b654098da049e7833b24f1a09cb8dd77
BLAKE2b-256 fd7d1c436f216a1f6f4a0b0fbf246c08abaa4a1c4e903c9a836b9dad38d4737e

See more details on using hashes here.

File details

Details for the file tabrag_xai_imputer-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tabrag_xai_imputer-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c81609ea1e85a0259ff6865258c82445e7ca00a2c17195656c741e138ef5d85f
MD5 091597f46bb61743c2fc517325ca1426
BLAKE2b-256 a6c9c8366ce791d6193db671b07cbc848059b004144ef887251697744ee534c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page