An open source project from Data to AI Lab at MIT.
We are happy to announce that our new model for synthetic data called CTGAN is open-sourced. Please check the new model in this repo. The new model is simpler and gives better performance on many datasets.
TGAN
Generative adversarial training for synthesizing tabular data.
Overview
TGAN is a tabular data synthesizer. It can generate fully synthetic data from real data. Currently, TGAN can
generate numerical columns and categorical columns.
Requirements
Python
TGAN has been developed and runs on Python 3.5,
3.6 and
3.7.
Also, although it is not strictly required, the usage of a virtualenv
is highly recommended in order to avoid interfering with other software installed in the system where TGAN
is run.
Installation
The simplest and recommended way to install TGAN is using pip:
Alternatively, you can also clone the repository and install it from sources
git clone git@github.com:sdv-dev/TGAN.git
cd TGAN
make install
For development, you can use make install-develop instead in order to install all the required
dependencies for testing and code linting.
Data Format
Input Format
In order to be able to sample new synthetic data, TGAN first needs to be fitted to
existing data.
The input data for this fitting process has to be a single table that satisfies the following
rules:
- Has no missing values.
- Has columns of types
int, float, str or bool.
- Each column contains data of only one type.
An example of such a tables would be:
| str_column |
float_column |
int_column |
bool_column |
| 'green' |
0.15 |
10 |
True |
| 'blue' |
7.25 |
23 |
False |
| 'red' |
10.00 |
1 |
False |
| 'yellow' |
5.50 |
17 |
True |
As you can see, this table contains 4 columns: str_column, float_column, int_column and
bool_column, each one being an example of the supported value types. Notice aswell that there is
no missing values for any of the rows.
NOTE: It's important to have properly identifed which of the columns are numerical, which means
that they represent a magnitude, and which ones are categorical, as during the preprocessing of
the data, numerical and categorical columns will be processed differently.
Output Format
The output of TGAN is a table of sampled data with the same columns as the input table and as
many rows as requested.
Demo Datasets
TGAN includes a few datasets to use for development or demonstration purposes. These datasets
come from the UCI Machine Learning repository, and have been
preprocessed to be ready to use with TGAN, following the requirements specified in the
Input Format section.
These datasets can be browsed and directly downloaded from the
hdi-project-tgan AWS S3 Bucket
Census dataset
This dataset contains a single table, with information from the census, labeled with information of
wheter or not the income of is greater than 50.000 $/year. It's a single csv file, containing
199522 rows and 41 columns. From these 41 columns, only 7 are identified as continuous. In
TGAN this dataset is called census.
Cover type
This dataset contains a single table with cartographic information labeled with the different
forrest cover types. It's a single csv file, containing 465588 rows and 55 columns. From these
55 columns, 10 are identified as continuous. In TGAN this dataset is called covertype.
Quickstart
In this short tutorial we will guide you through a series of steps that will help you getting
started with the most basic usage of TGAN in order to generate samples from a given dataset.
NOTE: The following examples are also covered in a Jupyter notebook,
which you can execute by running the following commands inside your virtualenv:
pip install jupyter
jupyter notebook examples/Usage_Example.ipynb
1. Load the data
The first step is to load the data wich we will use to fit TGAN. In order to do so, we will first
import the function tgan.data.load_data and call it with the name of the dataset that we want to
load.
In this case, we will load the census dataset, which we will use during the subsequent steps,
and obtain two objects:
-
data, that will contain a pandas.DataFrame with the table of data from the census
dataset ready to be used to fit the model.
-
continuous_columns, that will contain a list with the indices of continuous columns.
>>> from tgan.data import load_demo_data
>>> data, continuous_columns = load_demo_data('census')
>>> data.head(3).T[:10]
0 1 2
0 73 58 18
1 Not in universe Self-employed-not incorporated Not in universe
2 0 4 0
3 0 34 0
4 High school graduate Some college but no degree 10th grade
5 0 0 0
6 Not in universe Not in universe High school
7 Widowed Divorced Never married
8 Not in universe or children Construction Not in universe or children
9 Not in universe Precision production craft & repair Not in universe
>>> continuous_columns
[0, 5, 16, 17, 18, 29, 38]
2. Create a TGAN instance
The next step is to import TGAN and create an instance of the model.
To do so, we need to import the tgan.model.TGANModel class and call it with the
continuous_columns as unique argument.
This will create a TGAN instance with the default parameters:
>>> from tgan.model import TGANModel
>>> tgan = TGANModel(continuous_columns)
3. Fit the model
Once you have a TGAN instance, you can proceed to call it's fit method passing the data that
you loaded before in order to start the fitting process:
This process will not return anything, however, the progress of the fitting will be printed in the
screen.
NOTE Depending on the performance of the system you are running, and the parameters selected
for the model, this step can take up to a few hours.
4. Sample new data
After the model has been fitted, you are ready to generate new samples by calling the sample
method of the TGAN instance passing it the desired amount of samples:
>>> num_samples = 1000
>>> samples = tgan.sample(num_samples)
>>> samples.head(3).T[:10]
0 1 2
0 12 27 56
0 12 27 56
1 Not in universe Self-employed-not incorporated Private
2 0 4 35
3 0 34 22
4 Children Some college but no degree Some college but no degree
5 0 0 500
6 Not in universe Not in universe Not in universe
7 Never married Married-civilian spouse present Married-civilian spouse present
8 Not in universe or children Construction Finance insurance and real estate
9 Not in universe Precision production craft & repair Adm support including clerical
The returned object, samples, is a pandas.DataFrame containing a table of synthetic data with
the same format as the input data and 1000 rows as we requested.
5. Save and Load a model
In the steps above we saw that the fitting process can take a lot of time, so we probably would
like to avoid having to fit every we want to generate samples. Instead we can fit a model once,
save it, and load it every time we want to sample new data.
If we have a fitted model, we can save it by calling it's save method, that only takes
as argument the path where the model will be stored. Similarly, the TGANModel.load allows to load
a model stored on disk by passing as argument the path where the model is stored.
>>> model_path = 'models/mymodel.pkl'
>>> tgan.save(model_path)
Model saved successfully.
Bear in mind that in case the file already exists, TGAN will avoid overwritting it unless the
force=True argument is passed:
>>> tgan.save(model_path)
The indicated path already exists. Use `force=True` to overwrite.
In order to do so:
>>> tgan.save(model_path, force=True)
Model saved successfully.
Once the model is saved, it can be loaded back as a TGAN instance by using the TGANModel.load
method:
>>> new_tgan = TGANModel.load(model_path)
>>> new_samples = new_tgan.sample(num_samples)
>>> new_samples.head(3).T[:10]
0 1 2
0 12 27 56
0 12 27 56
1 Not in universe Self-employed-not incorporated Private
2 0 4 35
3 0 34 22
4 Children Some college but no degree Some college but no degree
5 0 0 500
6 Not in universe Not in universe Not in universe
7 Never married Married-civilian spouse present Married-civilian spouse present
8 Not in universe or children Construction Finance insurance and real estate
9 Not in universe Precision production craft & repair Adm support including clerical
At this point we could use this model instance to generate more samples.
Loading custom datasets
In the previous steps we used some demonstration data but we did not show you how to load your own
dataset.
In order to do so you will need to generate a pandas.DataFrame object from your dataset. If your
dataset is in a csv format you can do so by using pandas.read_csv and passing to it the path to
the CSV file that you want to load.
Additionally, you will need to create 0-indexed list of columns indices to be considered continuous.
For example, if we want to load a local CSV file, path/to/my.csv, that has as continuous columns
their first 4 columns, that is, indices [0, 1, 2, 3], we would do it like this: