Skip to main content

A Keras-based entity embedding encoder for tabular ML and GBM pipelines

Project description

Category Embedding

A Keras-based neural encoder for categorical variables in tabular machine learning.
It learns dense vector representations (embeddings) for categorical features and outputs a clean numeric DataFrame that integrates seamlessly with gradient-boosted tree models.

This library is designed for ML engineers who want the benefits of deep-learning-based embeddings without replacing their GBM models.
It is sklearn-compatible, deterministic, and production-ready.


🚀 Features

  • Multi-column categorical embeddings
    Each categorical feature receives its own learned embedding matrix.

  • Smart default embedding dimensions
    Uses a simple, interpretable rule:

    • If n_cat ≤ 10: dim = n_cat - 1
    • Else: dim = max(10, n_cat // 2)
    • Always capped at 30.
  • Per-column embedding dimension overrides
    Pass a list of integers to control embedding size manually.

  • Hashing for unseen categories
    Unseen values at inference time are deterministically mapped into valid embedding indices.

  • Residual MLP architecture
    LayerNorm + GELU + Dropout + skip connections for stable training.

  • Automatic numeric feature scaling
    Numeric columns are scaled using StandardScaler during training.
    You can choose whether the transformed output returns scaled or raw numeric values via scaled_num_out.

  • Log-scaling of regression targets
    For regression tasks, the target is automatically transformed using
    log(y + 1e-6) during training and inverse‑transformed during prediction.

  • Supports regression and binary classification
    The neural head is used only for training/tuning; GBMs remain the final predictor.

  • Optional external validation set
    Enables clean early stopping and stable embedding learning.

  • Sklearn-compatible API
    Implements fit, transform, predict, and get_feature_names_out.

  • Outputs a pandas DataFrame
    Perfect for LightGBM, XGBoost, CatBoost, or any sklearn model.


📦 Installation

pip install category-embedding

Requires:

  • Python ≥ 3.9
  • TensorFlow ≥ 2.12
  • scikit-learn ≥ 1.2
  • pandas ≥ 1.5

🔧 Quick Start

import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from lightgbm import LGBMRegressor
from category_embedding import CategoryEmbedding

df = pd.DataFrame({
    "country": ["DE", "FR", "DE", "US", "US"],
    "device": ["mobile", "desktop", "tablet", "mobile", "desktop"],
    "age": [25, 40, 31, 22, 35],
})
y = [10.5, 20.1, 15.3, 8.7, 18.0]

categorical = ["country", "device"]
numeric = ["age"]

# CategoryEmbedding acts as a transformer inside a sklearn pipeline
preprocess = ColumnTransformer(
    transformers=[
        ("emb", CategoryEmbedding(
            task="regression",
            categorical_cols=categorical,
            numeric_cols=numeric,
            epochs=20,
            batch_size=32,
            scaled_num_out=True,   # return scaled numeric features
        ), categorical + numeric)
    ],
    remainder="drop",
)

model = Pipeline([
    ("prep", preprocess),
    ("lgbm", LGBMRegressor(n_estimators=300, learning_rate=0.05)),
])

model.fit(df, y)
preds = model.predict(df)

🧠 Why Use Category Embedding?

Traditional encoders struggle with:

  • high-cardinality categorical features
  • sparse interactions
  • noisy or rare categories

Neural embeddings solve this by learning dense, continuous representations that capture similarity structure between categories.

This library gives you:

  • the power of deep learning
  • the simplicity and performance of GBMs
  • a clean sklearn interface
  • deterministic, production-ready behavior
  • automatic numeric scaling and target log-transforming for stable training

⚙️ API Overview

CategoryEmbedding(...)

Parameters

Parameter Type Description
task "regression" or "classification" Determines loss function and whether log-scaling is applied to the target.
categorical_cols list[str] Names of categorical columns to embed.
numeric_cols list[str] Names of numeric columns to include as inputs.
embedding_dims list[int] or None Optional per-column embedding sizes. If None, smart defaults are used.
hidden_units int Width of each residual MLP block.
n_blocks int Number of residual blocks.
dropout_rate float Dropout rate inside residual blocks and before output head.
l2_emb float L2 regularization for embedding weights.
l2_dense float L2 regularization for dense layers.
batch_size int Training batch size.
epochs int Maximum number of training epochs.
lr float Learning rate for Adam optimizer.
random_state int TensorFlow random seed.
verbose int Verbosity passed to Keras .fit().
patience int Early stopping patience.
reduce_lr_factor float LR reduction factor when validation loss plateaus.
reduce_lr_patience int Patience before reducing LR.
val_set tuple(X_val, y_val) or None Optional external validation set.
scaled_num_out bool If True, transform() returns scaled numeric columns; otherwise raw numeric values.

Methods

.fit(X, y)

Trains the embedding model:

  • learns embeddings
  • fits numeric scaler
  • applies log-scaling to regression targets
  • trains the neural model

.transform(X)

Returns a DataFrame containing:

  • learned embeddings
  • numeric features (scaled or raw depending on scaled_num_out)

.predict(X)

Uses the neural head for tuning/evaluation.
For regression: automatically applies inverse log-transform.

.get_feature_names_out()

Returns the names of all output columns.


🔑 Keywords

machine learning, deep learning, tabular data, categorical encoding, entity embeddings, category embeddings, neural encoder, keras, tensorflow, scikit-learn, sklearn transformer, feature engineering, gradient boosting, lightgbm, xgboost, embeddings for gbm, high-cardinality features, optuna tuning, ml pipelines


📄 License

This project is licensed under the MIT License. See the LICENSE file 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

category_embedding-0.1.6.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

category_embedding-0.1.6-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file category_embedding-0.1.6.tar.gz.

File metadata

  • Download URL: category_embedding-0.1.6.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for category_embedding-0.1.6.tar.gz
Algorithm Hash digest
SHA256 4ca89be4c5fc392f02fa206c6c5e98097b8aab07c6423d8b38230e4b624a9c28
MD5 55535cd2713244a668927af9046aba4d
BLAKE2b-256 ab5dccf09d3f767ec683bbf5eca52b34e7c36469d30a60cf003c72d0b5fa596c

See more details on using hashes here.

Provenance

The following attestation bundles were made for category_embedding-0.1.6.tar.gz:

Publisher: publish.yml on Karabush/category-embedding

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file category_embedding-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for category_embedding-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 cc12fdf8fd43154fcd2b7b38fd773395b0fde00bd22e39d589e3035149061b5e
MD5 7bc3ea7135b389034fab118114b12dea
BLAKE2b-256 fe59b6a4e10c09e310b451de4899c42bd9796e2756bf7dcdfcb7995ad1e94b5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for category_embedding-0.1.6-py3-none-any.whl:

Publisher: publish.yml on Karabush/category-embedding

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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