PreTab
PreTab is a modular, extensible, and scikit-learn-compatible
preprocessing library for tabular data. It supports all sklearn transformers out of the
box and extends them with a rich set of custom encoders, splines, and neural basis expansions.
Note
These docs are for PreTab 0.1.0. The project is under active development and the
public API may evolve while the major version is 0.
Highlights
Spline expansions (B-splines, natural cubic, thin-plate, tensor-product, P-splines), neural basis maps (RBF, ReLU, sigmoid, tanh), custom binning, and Piecewise Linear Encoding (PLE).
Ordinal and one-hot encodings, pretrained language embeddings, and helpers such as
OneHotFromOrdinalTransformer.
Fully compatible with sklearn.pipeline.Pipeline and sklearn.compose.ColumnTransformer;
accepts any sklearn-native transformer and its hyperparameters.
Automatic feature-type detection (numerical vs. categorical) with support for both
pandas.DataFrame and numpy.ndarray inputs.
See it in action
import numpy as np
import pandas as pd
from pretab import Preprocessor
df = pd.DataFrame({
"age": np.random.randint(18, 65, size=100),
"income": np.random.normal(60_000, 15_000, size=100).astype(int),
"city": np.random.choice(["Berlin", "Munich", "Hamburg"], size=100),
})
y = np.random.randn(100)
# One strategy per feature type: PLE for numerics, integer codes for categoricals
pre = Preprocessor(numerical_method="ple", categorical_method="int")
X = pre.fit_transform(df, y) # dict of model-ready feature blocks
{k: v.shape for k, v in X.items()}
# {'num_age': (100, 7), 'num_income': (100, 7), 'cat_city': (100, 1)}
Preprocessor detects the column types, fits a strategy per column, and returns model-ready
arrays, either as a dict of blocks or, with return_array=True, a single stacked matrix.
Inspect the resolved layout at any time with get_feature_info(verbose=True):
feature kind pipeline dim cats
----------------------------------------------------------------
age numerical imputer -> minmax -> ple 7 -
income numerical imputer -> minmax -> ple 7 -
city categorical imputer -> continuous_ordinal 1 4
Mix strategies per column
Columns rarely want the same treatment. Pass a feature_preprocessing map to give each
column its own strategy, and a single fit still returns one coherent feature set:
pre = Preprocessor(feature_preprocessing={
"age": "ple", # piecewise-linear encoding
"income": "rbf", # radial-basis expansion
"city": "one-hot", # one-hot categorical
})
X = pre.fit_transform(df, y)
{k: v.shape for k, v in X.items()}
# {'num_age': (100, 7), 'num_income': (100, 7), 'cat_city': (100, 3)}
Get started
Install PreTab from PyPI or from source.
Fit and transform a dataset in a few lines.
See PreTab lift a linear model, baseline vs. PreTab.
Classification and full sklearn pipelines.
The Preprocessor and every transformer.
Project links
Source code: https://github.com/OpenTabular/PreTab
Issue tracker: https://github.com/OpenTabular/PreTab/issues