CTGAN is a Python library implementing a Conditional Generative Adversarial Network (GAN) specifically designed for synthesizing tabular data. It learns from real datasets to generate high-fidelity synthetic data, addressing challenges like mixed data types and imbalanced categorical columns. The library is actively maintained, with version 0.12.1 released in February 2026, and is part of the broader SDV (Synthetic Data Vault) ecosystem.
pip install ctganVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load a demo dataset, initialize a `CTGAN` model, fit it to the real data by specifying discrete columns, and then generate a sample of synthetic data. It prints the head of both the original and synthetic datasets for a quick comparison.
Ensure your pandas DataFrame columns have correct dtypes and handle missing values (e.g., imputation or removal) before calling `ctgan.fit()`.
After calling `ctgan.sample()`, apply rounding to the relevant synthetic columns (e.g., `synthetic_data['integer_column'] = synthetic_data['integer_column'].round().astype(int)`).
Consider feature engineering for high-cardinality columns, binning for skewed distributions, or augmenting small datasets if possible. Experimentation with hyperparameters is also crucial.
For complex data models, consider using the broader SDV library which offers features for defining and enforcing data constraints. Otherwise, apply post-processing to enforce critical rules.
If you were directly accessing and manipulating `loss_values` as PyTorch tensors, update your code to expect and work with float values. This change simplifies interaction for most users.
Preprocess your data to fill or remove NaN values. Common approaches include `df.dropna()` or `df.fillna(value)` with an appropriate strategy (e.g., mean, median, mode, or a constant).
Carefully check that all column names listed in `discrete_columns` exactly match the column names in your input pandas DataFrame. Pay attention to case sensitivity and typos.
No fix is needed. Continue monitoring the training process. Stable negative generator loss alongside discriminator loss oscillating around zero generally signifies successful training. Diverging or exploding losses are a concern.
Increase the number of `epochs`. Experiment with `CTGAN` hyperparameters such as `batch_size`, `generator_dim`, `discriminator_dim`, `generator_lr`, and `discriminator_lr`. Ensure your data quality is good and consider the limitations for high-cardinality/skewed data.