A generative adversarial network (GAN) uses two deep neural networks: a generator creates samples from random noise, while a discriminator distinguishes them from real examples. They train simultaneously, pushing the generator toward the original data distribution without directly seeing its instances. section 2.3.3
For tabular data, GAN-based generation can expand scarce datasets, strengthen minority classes, create rare edge cases for testing, and produce fictitious records that preserve useful characteristics without reproducing private instances. section 2.3.3
Use synthetic generation when you need to:
- Increase the number and diversity of available tabular examples. section 2.3.3
- Generate unusual cases for system testing or support privacy-conscious experimentation. section 2.3.3
Listing 2.5 Generating a synthetic dataset
import pandas as pd
from sdv.metadata import SingleTableMetadata #1
from sdv.single_table import TVAESynthesizer #2
data = pd.read_csv("./german_credit_data.csv", index_col=0) #3
data = data.reset_index()
print(data.shape)
print(data.head)
metadata = SingleTableMetadata() #4
metadata.detect_from_dataframe(data) #5
print(metadata.to_dict()) #6
metadata.update_column(column_name="Saving accounts",
sdtype="categorical") #7
synthesizer = TVAESynthesizer(metadata, epochs=10_000) #8
synthesizer.fit(data) #9
synthetic_data = synthesizer.sample(num_rows=10_000) #10
synthetic_data.head()
Shows a related SDV workflow for fitting a tabular synthesizer to German credit data and sampling synthetic rows. section 2.3.3
Listing 2.6 Testing a synthetic dataset using adversarial validation
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import roc_auc_score
X = pd.concat([synthetic_data, data]) #1
categorical_columns = [
"Sex", "Housing", "Saving accounts",
"Checking account", "Purpose"]
X_encoded = pd.get_dummies(X, columns=categorical_columns) #2
X= X.drop("index", axis="columns")
y = [0] * len(synthetic_data) + [1] * len(data)
model = RandomForestClassifier()
cv_preds = cross_val_predict(
model, #3
X_encoded, #3
y, #3
cv=5, #3
n_jobs=-1, #3
method="predict_proba" #3
) #3
roc_adv_score = roc_auc_score(y_true=y, y_score=cv_preds[:, 1]) #4
print(f"roc auc adv score: {roc_adv_score:0.3f}")
Demonstrates adversarial validation for testing whether synthetic and original datasets remain distinguishable. section 2.3.3











