Generating synthetic data with cuDF and RAPIDS: Worked Example — Data Preparation (NVIDIA-Certified Professional: Accelerated Data Science)
Generating Synthetic Data with cuDF and RAPIDS: Worked Example Generating synthetic data is a crucial technique in data science workflows, especially...
Generating Synthetic Data with cuDF and RAPIDS: Worked Example
Generating synthetic data is a crucial technique in data science workflows, especially when real data is scarce, sensitive, or imbalanced. Leveraging cuDF and RAPIDS libraries enables GPU-accelerated synthetic data generation, significantly speeding up the process. This worked example demonstrates how to create synthetic tabular data using these tools in a realistic scenario.
Scenario
Suppose you are working on a credit risk model but have limited access to real customer data due to privacy concerns. To train and validate your model, you decide to generate synthetic customer data that mimics the statistical properties of the original dataset.
Step 1: Import Required Libraries
First, import cudf for GPU DataFrame operations and rapids components for data manipulation and synthetic data generation.
Code
import cudfimport cupy as cpfrom cuml.preprocessing import StandardScalerfrom cuml.datasets import make_classificationimport numpy as np
Step 2: Generate Base Synthetic Data
Use cuml.datasets.make_classification to create a synthetic classification dataset with features and labels. This simulates the original customer data structure.
Code
X, y = make_classification(n_samples=10000, n_features=10, n_informative=5, n_classes=2, random_state=42)X_cudf = cudf.DataFrame.from_records(X.get())y_cudf = cudf.Series(y.get(), name='target')
Step 3: Feature Transformation and Standardization
Standardize features using StandardScaler from cuML to ensure features have zero mean and unit variance, which is important before generating synthetic variations.
Code
scaler = StandardScaler()X_scaled = scaler.fit_transform(X_cudf)
Step 4: Generate Synthetic Variations
To augment the dataset, create synthetic samples by adding Gaussian noise to the standardized features. This simulates new but plausible data points.
Code
noise = cp.random.normal(0, 0.1, size=X_scaled.shape)X_synthetic = X_scaled.values + noiseX_synthetic_cudf = cudf.DataFrame.from_records(cp.asnumpy(X_synthetic))
Step 5: Inverse Transform to Original Scale
Convert the synthetic data back to the original feature scale to maintain interpretability.
Code
X_synthetic_original = scaler.inverse_transform(X_synthetic_cudf)
Step 6: Combine Synthetic Data with Labels
Assign labels to the synthetic data. For simplicity, assign the same class distribution as the original dataset or use domain knowledge to label.
Code
y_synthetic = y_cudf.sample(frac=1, replace=True).reset_index(drop=True)synthetic_data = X_synthetic_originalsynthetic_data['target'] = y_synthetic
Step 7: Validate Synthetic Data
Check statistical similarity between original and synthetic datasets to ensure quality.
Code
print('Original mean:', X_cudf.mean())print('Synthetic mean:', synthetic_data.drop('target', axis=1).mean())
Summary
This example illustrates generating synthetic tabular data using cuDF and RAPIDS libraries by creating base data, standardizing features, adding noise to create synthetic variations, and validating the output. This GPU-accelerated approach enables scalable and efficient data augmentation, a key skill for the NVIDIA-Certified Professional: Accelerated Data Science exam and real-world workflows.
For more details on RAPIDS and cuDF, visit the official documentation at rapids.ai.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →