# CTGAN

> Generate synthetic tabular data with conditional GANs

- Category: Data & Labeling / Synthetic Data
- Language: Python
- Repository: https://github.com/sdv-dev/CTGAN
- Canonical page: https://ai-tldr.dev/tools/ctgan/

## In short

CTGAN is a Python library of deep-learning models that learn from a single table of real data and generate synthetic data that keeps the same structure and statistics. It sits in the Synthetic Data category of the Data & Labeling tools and is open-source, written in Python. To get started: install with pip.

## Overview

CTGAN is a Python library of deep-learning models that learn from a single table of real data and generate synthetic data that keeps the same structure and statistics. It implements the CTGAN and TVAE models from the 2019 NeurIPS paper "Modeling Tabular data using Conditional GAN".

It is meant for data scientists and engineers who need realistic stand-in data for testing, sharing, or training when the real records are limited or sensitive. You point it at a pandas table, tell it which columns are discrete, and it produces as many synthetic rows as you ask for.

CTGAN is part of the Synthetic Data Vault (SDV) ecosystem. You can use it standalone for direct control, or through the higher-level SDV library, which adds data preprocessing and constraints for people getting started with synthetic data.

## What it does

- Implements two tabular generators: CTGAN (a conditional GAN) and TVAE
- Learns from a single real table and samples any number of synthetic rows
- Handles mixed column types when you mark which columns are discrete
- Ships with a built-in demo dataset (load_demo) so you can try it right away
- Installs from both pip and conda
- Part of the SDV ecosystem, with an optional higher-level API for preprocessing and constraints

## Getting started

Install CTGAN as a standalone library, then fit a model on a real table and sample synthetic rows. Your data should have continuous values as floats, discrete values as ints or strings, and no missing values.

1. Install with pip

   Install the standalone CTGAN package from PyPI.

```bash
pip install ctgan
```

2. Or install with conda

   CTGAN is also available through conda using the pytorch and conda-forge channels.

```bash
conda install -c pytorch -c conda-forge ctgan
```

3. Fit a model and sample data

   Load the built-in demo dataset, list the discrete columns, train CTGAN, then generate synthetic rows.

```python
from ctgan import CTGAN
from ctgan import load_demo

real_data = load_demo()

# Names of the columns that are discrete
discrete_columns = [
    'workclass',
    'education',
    'marital-status',
    'occupation',
    'relationship',
    'race',
    'sex',
    'native-country',
    'income',
]

ctgan = CTGAN(epochs=10)
ctgan.fit(real_data, discrete_columns)

# Create synthetic data
synthetic_data = ctgan.sample(1000)
```

## When to use it

- Create shareable test data that mirrors a sensitive table without exposing real records
- Expand a small dataset with extra synthetic rows for experiments or model training
- Stand up realistic demo or staging data for an application
- Study and benchmark GAN-based tabular generation from the original NeurIPS paper
