Skrub as preprocessing for Table Foundation Models#

TabICL is an open source tabular foundation model: its a powerful predictor that doesn’t need hyper-parameter tuning. In this example, skrub takes care of preparing the dataframe, and TabICL makes the predictions.

Note

This example requires tabicl, which is not installed with skrub. You can install it with pip install tabicl.

The TabICL documentation also has a tutorial on how to handle string columns with skrub: TabICL string handling tutorial.

Loading the data#

We use the employee salaries dataset: the goal is to predict the salary of an employee from information such as their department, position title and hiring date. We only keep the first 500 rows to keep the memory pressure and runtime of the example low.

import skrub
from skrub.datasets import fetch_employee_salaries

data = fetch_employee_salaries()
X, y = data.X.iloc[:500], data.y.iloc[:500]
skrub.TableReport(X)

Please enable javascript

The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").



Building the pipeline#

tabular_pipeline() recognizes TabICL estimators and configures the TableVectorizer with the following configuration:

skrub.TableVectorizer(
    low_cardinality="passthrough",
    high_cardinality=skrub.StringEncoder(),
    cardinality_threshold=10,
    datetime=skrub.DatetimeEncoder(periodic_encoding="spline"),
)

String columns with at most 10 unique values are passed through as-is, since TabICL handles them natively, while columns with more unique values are encoded with a StringEncoder. TabICL handles missing values and scaling by itself, so no imputer or scaler is added.

from tabicl import TabICLRegressor

# To keep this example fast, we use a single estimator. Results would probably
# be a bit better with the default ``n_estimators=8``, but it is more expensive.
model = skrub.tabular_pipeline(TabICLRegressor(n_estimators=1))
model
Pipeline(steps=[('tablevectorizer',
                 TableVectorizer(cardinality_threshold=10,
                                 datetime=DatetimeEncoder(periodic_encoding='spline'),
                                 low_cardinality='passthrough')),
                ('tabiclregressor', TabICLRegressor(n_estimators=1))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Evaluating the model#

We evaluate the pipeline with a 3-fold cross-validation.

from sklearn.model_selection import cross_validate

tabicl_results = cross_validate(model, X, y, cv=3)
scores = tabicl_results["test_score"]
del model
print(f"R2 score: {scores.mean():.3f} ± {scores.std():.3f}")
Checkpoint 'tabicl-regressor-v2-20260212.ckpt' not cached.
 Downloading from Hugging Face Hub (jingang/TabICL).

Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
R2 score: 0.760 ± 0.091

Comparing with the default pipeline#

Passing "regressor" to tabular_pipeline() gives a pipeline that ends with a HistGradientBoostingRegressor. The data preparation is still handled by skrub: switching the model is a one-line change.

hgb_results = cross_validate(skrub.tabular_pipeline("regressor"), X, y, cv=3)

named_results = [("TabICL", tabicl_results), ("HGBT", hgb_results)]
for name, results in named_results:
    scores = results["test_score"]
    duration = results["fit_time"].mean() + results["score_time"].mean()
    print(
        f"{name:<7} R2 score: {scores.mean():.3f} ± {scores.std():.3f}"
        f"  (fit + predict time: {duration:.1f} s)"
    )
TabICL  R2 score: 0.760 ± 0.091  (fit + predict time: 4.3 s)
HGBT    R2 score: 0.697 ± 0.113  (fit + predict time: 0.5 s)

We use a boxplot to visualize the distribution of R2 scores across folds for each model.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
names, scores = zip(*[(name, result["test_score"]) for name, result in named_results])
ax.boxplot(scores, orientation="horizontal")
ax.set_yticks(range(1, len(names) + 1), labels=list(names), size=12)
ax.set_xlabel("R2 score", size=14)
ax.set_title("R2 distribution across folds (higher is better)", size=14)
plt.show()
R2 distribution across folds (higher is better)

On this small subset, TabICL gives better predictions than the default pipeline, at the cost of a longer computation time. This comparison is only meant to show how easily models can be swapped: results depend a lot on the dataset and its size.

Note that TabICL does not need hyperparameter tuning: it works well with its default parameters. The data preparation, however, still has parameters that can matter, such as the number of components of the StringEncoder. These can be tuned like in any scikit-learn pipeline, for example with GridSearchCV.

Conclusion#

skrub prepares the dataframe for TabICL in a single line with tabular_pipeline(), which also makes it easy to compare TabICL with other models.

Total running time of the script: (0 minutes 21.752 seconds)

Estimated memory usage: 1370 MB

Gallery generated by Sphinx-Gallery