# Imputation: filling missing cells with a tabular foundation model, MICE, kNN and MIDAS

Missing cells are the norm in real tables. Hide some yourself, choose how they go missing, and compare five ways of filling them, scored against the values you hid.

<div data-sim="tfm" data-v="imp"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

The table shows the filled values in italics for the method you choose; click any cell to hide it. The bars are the error on the hidden cells in standard deviations, for every method at once. Click a hidden cell to see each method's guess next to the true value, and the foundation model's uncertainty.

## Things to try

- Raise the missing rate to 50%.
- Switch to MNAR: everyone underestimates, because the high values are the hidden ones.
- Hide the price of several rows and compare MICE with the foundation model.

## How it works in real systems

Common choices are scikit-learn's SimpleImputer, KNNImputer and IterativeImputer (MICE), MIDAS for multiple imputation, and tabular foundation models that predict each missing cell from the observed cells of all rows.

## Code lab

The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.

<div class="cl-start"></div>

### Step 1 · Hide cells you know

```python
import numpy as np
rng = np.random.default_rng(11)
def houses(n):
    s = rng.integers(35, 181, n); ro = np.clip(np.round(s / 32 + rng.normal(0, .6, n)), 1, 6); ag = rng.integers(0, 81, n); d = np.round(rng.uniform(.5, 25, n), 1)
    p = 2.4 * s * (1 + .45 * np.exp(-d / 4)) - .9 * ag + 14 * ro + 40
    return np.c_[s, ro, ag, d].astype(float), np.round(p * (1 + rng.normal(0, .07, n)))
X, y = houses(60); X_test, y_test = houses(300)
```

```python
T = np.c_[X, y]                                          # five columns, price included
mask = rng.random(T.shape) < 0.2                         # MCAR: 20% of cells hidden
T_obs = T.copy(); T_obs[mask] = np.nan
def nrmse(T_hat): return np.sqrt(np.mean(((T_hat - T) / T.std(0))[mask] ** 2))
```

### Step 2 · Classical imputers

```python
from sklearn.impute import SimpleImputer, KNNImputer
from sklearn.experimental import enable_iterative_imputer  # noqa: F401
from sklearn.impute import IterativeImputer
for name, imp in [("mean", SimpleImputer()), ("kNN", KNNImputer(n_neighbors=5)), ("MICE", IterativeImputer(max_iter=10, random_state=0))]:
    print(name, round(nrmse(imp.fit_transform(T_obs)), 3))
```

### Step 3 · Impute with a tabular foundation model

Treat each column with gaps as a target: the rows where it is present are the context, the rows where it is missing are the queries. TabPFN accepts missing values in the features, so nothing else needs filling first.

```python
from tabpfn import TabPFNRegressor
T_tf = T_obs.copy()
for f in range(T.shape[1]):
    miss = np.isnan(T_obs[:, f])
    if not miss.any(): continue
    feats = np.delete(T_obs, f, axis=1)
    reg = TabPFNRegressor().fit(feats[~miss], T_obs[~miss, f])
    T_tf[miss, f] = reg.predict(feats[miss])
print("TabPFN", round(nrmse(T_tf), 3))
```

### Step 4 · Try MAR and MNAR

```python
mnar = (T - T.mean(0)) / T.std(0) > .3                   # high values are the ones that go missing
mask = mnar & (rng.random(T.shape) < .6)
```

Rerun the comparison: every method that learns from the observed cells now underestimates, because the observed cells are not representative.

<div class="cl-end"></div>

## More simulations

[← Probabilistic regression](/ai-simulations/tfm-probabilistic-regression) · [All AI Simulations](/ai-simulations) · [Masked value →](/ai-simulations/tfm-masked-value)
