Generating Synthetic Data with Python: Tools and Practices for Developers
Synthetic data is becoming a critically important resource in development, machine learning, and testing. It helps bypass legal restrictions, simulate rare scenarios, and speed up development cycles without needing real data. In this article, we dive into key Python libraries—from Faker to GAN—with code examples and tips for picking the right approach for your task.
Faker: The Go-To Tool for Realistic Fake Data
The Faker library generates fictional but visually plausible data: names, addresses, emails, card numbers, and much more. It doesn't prioritize statistical accuracy—its goal is to mimic the structure and format of real records. This makes Faker perfect for populating demo environments, mocking APIs, and anonymizing Dev/Test setups.
Here's an example generating a user profile for an online store:
import pandas as pd
from faker import Faker
import json
fake = Faker('ru_RU')
def generate_user_profile():
profile = {
"name": fake.name(),
"email": fake.email(),
"phone": fake.phone_number(),
"address": fake.address(),
"payment_method": fake.credit_card_provider(),
"order_history": [
{
"order_id": fake.uuid4(),
"date": fake.date_between(start_date='-1y').strftime('%Y-%m-%d'),
"total": fake.random_int(min=1000, max=10000)
}
for _ in range(fake.random_int(min=0, max=10))
]
}
return profile
user_profiles = [generate_user_profile() for _ in range(100)]
df = pd.json_normalize(user_profiles, sep='_')
This data works great for UI testing, load testing, and demos, but not for training ML models—due to the lack of correlations and realistic distributions.
Scikit-learn: Synthetic Data for Machine Learning
For machine learning tasks, you need data with controlled statistical properties. Scikit-learn offers functions like make_classification, make_regression, and make_blobs to generate datasets with specified numbers of classes, noise levels, correlations, and balance.
Example creating a balanced dataset with a rare class:
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=10000,
n_features=20,
n_informative=10,
n_redundant=5,
n_clusters_per_class=1,
weights=[0.95, 0.05], # 5% rare class (e.g., fraud)
flip_y=0.01,
random_state=42
)
This approach lets you prototype models even without real data on rare events. However, these datasets are linear or only mildly nonlinear and don't capture the complexity of real business processes.
Advanced Tools: SDV and Gretel Synthetics
When you need to preserve complex dependencies between features (e.g., age → income → purchase types), turn to more powerful solutions:
- Synthetic Data Vault (SDV) uses probabilistic models (like Gaussian Copula) to capture multidimensional distributions and conditional dependencies.
- Gretel Synthetics is built on transformers and delivers high privacy through differential privacy.
Both tools require an initial dataset to train the generator, but the output maintains statistical equivalence to the original without leaking personal information.
Generative Adversarial Networks (GANs)
At the top of the hierarchy are GANs. They consist of two neural networks: a generator and a discriminator. The generator creates synthetic samples, while the discriminator tries to tell them apart from real ones. Through training, both networks improve until the synthetics are indistinguishable from the originals.
Example using PyTorch for tabular data generation:
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self, input_dim, output_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, 128),
nn.ReLU(),
nn.Linear(128, 256),
nn.ReLU(),
nn.Linear(256, output_dim),
nn.Tanh()
)
def forward(self, x):
return self.net(x)
class Discriminator(nn.Module):
def __init__(self, input_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, 256),
nn.LeakyReLU(0.2),
nn.Linear(256, 128),
nn.LeakyReLU(0.2),
nn.Linear(128, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.net(x)
GANs deliver maximum realism but demand significant compute resources and deep learning expertise.
Comparing Approaches and Recommendations
Your choice of tool depends on the goal:
- UI testing / demo setups → Faker (fast, simple, realistic-looking).
- ML model prototyping → Scikit-learn (controlled parameters, fits seamlessly into the ecosystem).
- Replacing sensitive data while preserving stats → SDV or Gretel Synthetics.
- Maximum realism for production models → GANs (if you have data and resources).
Key Takeaways
- Synthetic data doesn't replace real data but speeds up development and reduces risks.
- Faker is great for structural imitation but not for ML.
- For machine learning, preserving distributions and correlations is crucial—use Scikit-learn, SDV, or GANs.
- Always evaluate synthetic data quality: compare stats, visualize distributions, and train test models.
- Ethical risks and inherited biases need attention even with "clean" synthetic data.
— Editorial Team
No comments yet.