### Install CTGAN from Source with Development Requirements Source: https://github.com/sdv-dev/ctgan/blob/main/RELEASE.md This snippet shows how to clone the CTGAN repository, navigate into the directory, checkout the main branch, and install development requirements. This is a prerequisite for the release process. ```bash git clone https://github.com/sdv-dev/CTGAN.git cd CTGAN git checkout main make install-develop ``` -------------------------------- ### Install CTGAN Standalone Library (pip) Source: https://github.com/sdv-dev/ctgan/blob/main/README.md This command installs the CTGAN library directly using pip. It's a prerequisite for using CTGAN as a standalone tool, which may require manual data preprocessing. ```bash pip install ctgan ``` -------------------------------- ### Install CTGAN Standalone Library (conda) Source: https://github.com/sdv-dev/ctgan/blob/main/README.md This command installs the CTGAN library using conda, specifying the pytorch and conda-forge channels. This is an alternative installation method for users who prefer the conda package manager. ```bash conda install -c pytorch -c conda-forge ctgan ``` -------------------------------- ### Advanced CTGAN Training Configuration (Python) Source: https://context7.com/sdv-dev/ctgan/llms.txt Provides examples of configuring CTGAN for different dataset sizes and custom learning rates. Includes options for embedding dimensions, network sizes, batch sizes, epochs, and learning rate decay. Also shows how to access and plot training loss. ```python from ctgan import CTGAN import matplotlib.pyplot as plt # Configure for large datasets ctgan_large = CTGAN( embedding_dim=256, # Larger embedding space generator_dim=(512, 512), # Deeper generator network discriminator_dim=(512, 512),# Deeper discriminator network batch_size=1000, # Larger batch size epochs=500, # More training epochs discriminator_steps=5, # Multiple D updates per G update pac=10, # Pac parameter for discriminator log_frequency=True, # Use log frequency for conditional sampling verbose=True ) # Configure for small datasets ctgan_small = CTGAN( embedding_dim=64, generator_dim=(128, 128), discriminator_dim=(128, 128), batch_size=100, epochs=1000, # More epochs for small data pac=5, verbose=True ) # Configure with custom learning rates ctgan_tuned = CTGAN( generator_lr=1e-4, # Lower learning rate for generator generator_decay=1e-5, # Weight decay for regularization discriminator_lr=1e-4, discriminator_decay=1e-5, epochs=300 ) # Assuming 'data' and 'discrete_columns' are defined elsewhere ctgan_tuned.fit(data, discrete_columns) samples = ctgan_tuned.sample(5000) # Access loss history for analysis loss_df = ctgan_tuned.loss_values plt.plot(loss_df['Epoch'], loss_df['Generator Loss'], label='Generator') plt.plot(loss_df['Epoch'], loss_df['Discriminator Loss'], label='Discriminator') plt.legend() plt.savefig('training_loss.png') ``` -------------------------------- ### Generate Synthetic Data with CTGAN (Python) Source: https://github.com/sdv-dev/ctgan/blob/main/README.md This Python code demonstrates how to use the CTGAN model to generate synthetic data. It loads a demo dataset, specifies discrete columns, trains the CTGAN model, and then samples synthetic data. Ensure the 'ctgan' library is installed. ```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) ``` -------------------------------- ### Run Linting and Tests for CTGAN Source: https://github.com/sdv-dev/ctgan/blob/main/RELEASE.md This command executes both the test suite and linting tools for the CTGAN project. It ensures that all tests pass without errors and that the code adheres to the project's coding standards. Successful execution is indicated by a summary of passed tests and linting checks. ```bash make test && make lint ``` -------------------------------- ### Load Demo Dataset for CTGAN Testing (Python) Source: https://context7.com/sdv-dev/ctgan/llms.txt Loads the Adult Census demo dataset using the `load_demo` function for quick testing and experimentation with the CTGAN model. Demonstrates fitting the model and sampling synthetic data. ```python from ctgan import load_demo # Load demo dataset (Adult Census) demo_data = load_demo() print(demo_data.shape) print(demo_data.head()) print(demo_data.columns.tolist()) # Demo data includes mixed types print(demo_data.dtypes) # Use demo data for quick testing from ctgan import CTGAN discrete_cols = [ 'workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country', 'income' ] model = CTGAN(epochs=10, verbose=True) model.fit(demo_data, discrete_cols) synthetic = model.sample(100) ``` -------------------------------- ### Create Release Candidate Branch in SDV Source: https://github.com/sdv-dev/ctgan/blob/main/RELEASE.md This sequence of bash commands demonstrates how to create a new branch in the SDV repository to test a CTGAN release candidate. It involves creating a new branch, updating the pyproject.toml file to specify the release candidate version, and pushing the new branch to the origin. ```bash git checkout -b test-ctgan-X.Y.Z git push --set-upstream origin test-ctgan-X.Y.Z ``` -------------------------------- ### CTGAN Synthesizer for Synthetic Tabular Data Source: https://context7.com/sdv-dev/ctgan/llms.txt Initializes and trains the CTGAN model using a demo dataset. It handles discrete columns and generates synthetic samples. Requires the 'ctgan' library. ```python from ctgan import CTGAN from ctgan import load_demo # Load demo dataset (Adult Census Dataset) real_data = load_demo() # Define discrete columns discrete_columns = [ 'workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country', 'income' ] # Initialize and train CTGAN model ctgan = CTGAN( epochs=300, batch_size=500, generator_dim=(256, 256), discriminator_dim=(256, 256), embedding_dim=128, generator_lr=2e-4, discriminator_lr=2e-4, pac=10, verbose=True ) # Fit the model to real data ctgan.fit(real_data, discrete_columns) # Generate synthetic samples synthetic_data = ctgan.sample(1000) print(synthetic_data.head()) ``` -------------------------------- ### Model Persistence for CTGAN and TVAE Source: https://context7.com/sdv-dev/ctgan/llms.txt Saves trained CTGAN and TVAE models to disk using pickle and loads them for later use without retraining. Requires 'ctgan' library. ```python from ctgan import CTGAN, TVAE # Train and save CTGAN model ctgan = CTGAN(epochs=300) ctgan.fit(train_data, discrete_columns) ctgan.save('my_ctgan_model.pkl') # Load saved model loaded_ctgan = CTGAN.load('my_ctgan_model.pkl') new_samples = loaded_ctgan.sample(1000) # Same for TVAE tvae = TVAE(epochs=200) tvae.fit(train_data, discrete_columns) tvae.save('my_tvae_model.pkl') loaded_tvae = TVAE.load('my_tvae_model.pkl') tvae_samples = loaded_tvae.sample(2000) ``` -------------------------------- ### GPU/CPU Device Management for CTGAN Source: https://context7.com/sdv-dev/ctgan/llms.txt Demonstrates how to control device placement (GPU or CPU) for CTGAN model training and inference. Allows enabling GPU acceleration or forcing CPU usage. Requires 'ctgan' and 'torch' libraries. ```python from ctgan import CTGAN import torch # Enable GPU acceleration (default) ctgan_gpu = CTGAN(enable_gpu=True, epochs=100) ctgan_gpu.fit(data, discrete_columns) # Force CPU usage ctgan_cpu = CTGAN(enable_gpu=False, epochs=100) ctgan_cpu.fit(data, discrete_columns) # Manually set device after initialization ctgan = CTGAN() ctgan.fit(data, discrete_columns) # Switch to CPU ctgan.set_device(torch.device('cpu')) cpu_samples = ctgan.sample(500) # Switch to GPU if torch.cuda.is_available(): ctgan.set_device(torch.device('cuda:0')) gpu_samples = ctgan.sample(1000) ``` -------------------------------- ### Update SDV pyproject.toml for CTGAN Release Candidate Source: https://github.com/sdv-dev/ctgan/blob/main/RELEASE.md This TOML snippet illustrates how to modify the `pyproject.toml` file in the SDV project to specify a minimum version constraint for CTGAN. This is typically done when testing a release candidate to ensure compatibility. ```toml 'ctgan>=X.Y.Z.dev0' ``` -------------------------------- ### Check CTGAN Release Readiness Source: https://github.com/sdv-dev/ctgan/blob/main/RELEASE.md This command performs a final check to ensure that the CTGAN project is ready for an official release. It should be executed after all previous release steps, including HISTORY.md updates, have been completed. ```bash make check-release ``` -------------------------------- ### Random State Control for Reproducibility Source: https://context7.com/sdv-dev/ctgan/llms.txt Illustrates setting random seeds for NumPy and PyTorch to ensure reproducible synthetic data generation with the CTGAN model. Requires 'ctgan', 'numpy', and 'torch' libraries. ```python from ctgan import CTGAN import numpy as np import torch # Example usage would involve setting seeds before model initialization or fitting. # For instance: # np.random.seed(42) # torch.manual_seed(42) # if torch.cuda.is_available(): # torch.cuda.manual_seed_all(42) # ctgan = CTGAN(seed=42) # If CTGAN supports a direct seed parameter. # ctgan.fit(data, discrete_columns) ``` -------------------------------- ### Set Random State for CTGAN Reproducibility (Python) Source: https://context7.com/sdv-dev/ctgan/llms.txt Demonstrates how to set a random state for the CTGAN model using either an integer seed or custom NumPy and PyTorch random state objects. This ensures reproducible synthetic data generation. ```python from ctgan import CTGAN import numpy as np import torch # Assuming 'data' and 'discrete_columns' are defined elsewhere # Method 1: Set random state with integer seed ctgan = CTGAN(epochs=100) ctgan.set_random_state(42) ctgan.fit(data, discrete_columns) samples1 = ctgan.sample(1000) # Rerun with same seed produces identical results ctgan2 = CTGAN(epochs=100) ctgan2.set_random_state(42) ctgan2.fit(data, discrete_columns) samples2 = ctgan2.sample(1000) # samples1 equals samples2 # Method 2: Use custom random state objects np_state = np.random.RandomState(seed=123) torch_state = torch.Generator().manual_seed(123) ctgan = CTGAN(epochs=100) ctgan.set_random_state((np_state, torch_state)) ctgan.fit(data, discrete_columns) reproducible_samples = ctgan.sample(500) ``` -------------------------------- ### TVAE Synthesizer for Synthetic Tabular Data Source: https://context7.com/sdv-dev/ctgan/llms.txt Initializes and trains the TVAE model for synthetic tabular data generation. It requires a pandas DataFrame and a list of discrete columns. Uses custom parameters for model configuration. Requires 'ctgan' and 'pandas' libraries. ```python from ctgan import TVAE import pandas as pd # Load your data data = pd.read_csv('your_data.csv') # Define discrete columns discrete_cols = ['category_col1', 'category_col2'] # Initialize TVAE with custom parameters tvae = TVAE( epochs=300, batch_size=500, embedding_dim=128, compress_dims=(128, 128), decompress_dims=(128, 128), l2scale=1e-5, loss_factor=2, verbose=True ) # Train the model tvae.fit(data, discrete_cols) # Generate synthetic data synthetic_samples = tvae.sample(5000) # Access training loss values print(tvae.loss_values) ``` -------------------------------- ### Conditional Sampling with CTGAN Source: https://context7.com/sdv-dev/ctgan/llms.txt Generates synthetic data with specific conditions on discrete columns using the CTGAN model. Allows controlling the distribution of generated values by specifying condition columns and values. Requires 'ctgan' library. ```python from ctgan import CTGAN, load_demo # Setup and train model real_data = load_demo() discrete_columns = ['education', 'income', 'sex'] ctgan = CTGAN(epochs=100, verbose=False) ctgan.fit(real_data, discrete_columns) # Generate samples with conditional constraints # Increase probability of specific category combinations samples_high_income = ctgan.sample( n=1000, condition_column='income', condition_value='>50K' ) samples_doctorate = ctgan.sample( n=500, condition_column='education', condition_value='Doctorate' ) # Verify conditional generation worked print(samples_high_income['income'].value_counts()) print(samples_doctorate['education'].value_counts()) ``` -------------------------------- ### Python Assertions in CTGAN Code Source: https://github.com/sdv-dev/ctgan/blob/main/static_code_analysis.txt These code snippets demonstrate the use of Python's 'assert' statement, which is flagged by the Bandit linter. Assertions are used for debugging and will be removed in optimized code, potentially impacting runtime checks. ```python assert args.sample_condition_column_value is not None ``` ```python assert item[0] == 'D' ``` ```python assert idx in discrete ``` ```python assert idx in meta['discrete_columns'] ``` ```python assert st == data.shape[1] ``` ```python assert input_.size()[0] % self.pac == 0 ``` ```python assert batch_size % 2 == 0 ``` ```python assert st == recon_x.size()[1] ``` -------------------------------- ### CTGAN with NumPy Arrays (Python) Source: https://context7.com/sdv-dev/ctgan/llms.txt Demonstrates how to train and use the CTGAN model directly with NumPy arrays instead of pandas DataFrames. This involves specifying discrete columns by their indices. ```python from ctgan import CTGAN import numpy as np # Create numpy array data data_array = np.array([ [25, 50000, 1, 0], [30, 60000, 0, 1], [35, 75000, 1, 1], [40, 90000, 0, 0] ]) # Specify discrete columns by index (not name) discrete_column_indices = [2, 3] # Third and fourth columns are discrete # Train model with numpy array ctgan = CTGAN(epochs=100) ctgan.fit(data_array, discrete_column_indices) # Generate synthetic numpy array synthetic_array = ctgan.sample(1000) print(type(synthetic_array)) # numpy.ndarray print(synthetic_array.shape) # Model preserves input type assert isinstance(synthetic_array, np.ndarray) ``` -------------------------------- ### DataTransformer for Tabular Data Transformation (Python) Source: https://context7.com/sdv-dev/ctgan/llms.txt Utilizes the DataTransformer class to convert tabular data into a model-ready format. It applies Bayesian GMM for continuous columns and one-hot encoding for discrete columns, with support for inverse transformation. ```python from ctgan.data_transformer import DataTransformer import pandas as pd # Create sample data data = pd.DataFrame({ 'age': [25, 30, 35, 40], 'income': [50000, 60000, 75000, 90000], 'education': ['Bachelors', 'Masters', 'PhD', 'Bachelors'] }) # Initialize transformer transformer = DataTransformer( max_clusters=10, weight_threshold=0.005 ) # Fit and transform data discrete_columns = ['education'] transformer.fit(data, discrete_columns) transformed_data = transformer.transform(data) print(f"Original shape: {data.shape}") print(f"Transformed shape: {transformed_data.shape}") print(f"Output dimensions: {transformer.output_dimensions}") # Inverse transform back to original format recovered_data = transformer.inverse_transform(transformed_data) print(recovered_data) # Convert column name/value to internal ID column_info = transformer.convert_column_name_value_to_id('education', 'PhD') print(column_info) # Output: {'discrete_column_id': 0, 'column_id': 2, 'value_id': 2} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.