Registry / ai-ml / ctgan
library0.12.1pypypi✓ verified 87d ago

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 ctgan
INSTALL
IMPORT
SIG · CTGAN
C
ctgan
ai-mlpythonv0.12.1
Install
78.4s avg
Import
10028ms
Disk
5146MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.12.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 86.7s
py 3.11
✕ build_error
✓ 82.17s
py 3.12
✕ build_error
✓ 74.03s
py 3.13
✕ build_error
✓ 70.73s
py 3.9
✕ build_error
✕ timeout
5146MB installed
● package 5146MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CTGAN
from ctgan import CTGAN
CTGANSynthesizer
from ctgan import CTGANSynthesizer
from ctgan.synthesizers import CTGANSynthesizer
The CTGANSynthesizer class is directly importable from the top-level `ctgan` package.
load_demo
from ctgan import load_demo
Used for quickly loading a demonstration dataset.

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.

import pandas as pd from ctgan import CTGAN, load_demo # Load demo data (Adult Census Dataset) or replace with your own DataFrame real_data = load_demo() # Identify discrete columns discrete_columns = [ 'workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country' ] # Initialize and train the CTGAN model # Set verbose=True to see training progress ctgan = CTGAN(epochs=10, verbose=True) ctgan.fit(real_data, discrete_columns) # Generate synthetic data synthetic_data = ctgan.sample(num_rows=1000) print("Original data head:") print(real_data.head()) print("\nSynthetic data head:") print(synthetic_data.head())
Debug
Known issues
gotchaWhen using CTGAN directly (not through SDV), manual data preprocessing is often required. Continuous columns must be floats, discrete columns as integers or strings, and the data should not contain any missing values (NaNs).
fix
Ensure your pandas DataFrame columns have correct dtypes and handle missing values (e.g., imputation or removal) before calling `ctgan.fit()`.
affects: All versions
gotchaCTGAN generates float values for all numerical columns. If your original data contains integer columns that require integer output, you must manually round the generated synthetic values.
fix
After calling `ctgan.sample()`, apply rounding to the relevant synthetic columns (e.g., `synthetic_data['integer_column'] = synthetic_data['integer_column'].round().astype(int)`).
affects: All versions
gotchaCTGAN can struggle with high-cardinality features, highly skewed distributions, or very small datasets. Performance may be less accurate in these scenarios.
fix
Consider feature engineering for high-cardinality columns, binning for skewed distributions, or augmenting small datasets if possible. Experimentation with hyperparameters is also crucial.
affects: All versions
gotchaCTGAN does not inherently handle primary key/foreign key constraints or other complex relational data integrity rules. The generated data may violate such constraints if not enforced externally.
fix
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.
affects: All versions
deprecatedThe `loss_values` attribute of a trained CTGAN model changed from returning `torch.Tensors` to standard Python floats.
fix
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.
affects: <0.12.1
Errors
Common errors & fixes
ValueError: Input data contains NaN values. CTGAN cannot handle missing values.
The input DataFrame passed to `ctgan.fit()` or `ctgan.sample()` contains null (NaN) values.
fix
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).
KeyError: 'column_name' not found in discrete_columns list.
There is a mismatch between a column name in your DataFrame that you intend to be discrete and the `discrete_columns` list provided to the CTGAN model.
fix
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.
Generator loss is becoming negative during training.
This is often a misunderstood aspect of GAN training. A negative generator loss usually indicates that the generator is successfully improving at fooling the discriminator, which is a desirable outcome.
fix
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.
Model does not seem to converge / Loss values are unstable or not improving.
Training a GAN can be challenging. This can be due to insufficient epochs, unsuitable hyperparameters, or inherent complexity/issues within the dataset.
fix
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.
Upgrade
Version history
0.12.1latest on PyPI · released Feb 13, 2026
Audit
Dependencies
numpyrequiredNumerical operations, required for data handling.
pandasrequiredDataFrame manipulation and data loading.
rdtrequiredData transformation utilities.
torchrequiredCore deep learning framework (PyTorch).
tqdmrequiredProgress bars for training.
Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
1
Resources
ctgan — pip install ctgan · libregistry