Unlocking the Potential of Generative AI for Synthetic Data Generation
Explore the capabilities and applications of generative AI to create realistic synthetic data for software development, analytics, and machine learning
Generative AI refers to a class of artificial intelligence algorithms capable of generating new data similar to a given dataset. These algorithms learn the underlying patterns and relationships in the data and use this knowledge to create new data consistent with the original dataset. Generative AI is a rapidly evolving field that has the potential to revolutionize the way we generate and use data.
Generative AI can generate synthetic data based on patterns and relationships learned from actual data. This ability to generate synthetic data has numerous applications, from creating realistic virtual environments for training and simulation to generating new data for machine learning models. In this article, we will explore the capabilities of generative AI and its potential to generate synthetic data, both directly and indirectly, for software development, data analytics, and machine learning.
Tabular data: This type of synthetic data is often used to generate datasets that resemble real-world data in terms of structure and statistical properties.
Time series data: This type of synthetic data generates datasets that resemble real-world time series data. It is commonly used when real-world time series data is unavailable or too expensive.
Image and video data: This synthetic data is used to generate realistic images and videos for training machine learning models or simulations.
Text data: This synthetic data generates realistic text for natural language processing tasks or for generating training data for machine learning models.
Sound data: This synthetic data generates realistic sound for training machine learning models or simulations.
Synthetic Tabular Data Types
Synthetic tabular data refers to artificially generated datasets that resemble real-world tabular data in terms of structure and statistical properties. Tabular data is organized into rows and columns, like tables or spreadsheets. Some specific types of synthetic tabular data include:
Financial data: Synthetic datasets that resemble real-world financial data such as bank transactions, stock prices, or credit card information.
Customer data: Synthetic datasets that resemble real-world customer data, such as purchase history, demographic information, or customer behavior.
Medical data: Synthetic datasets that resemble real-world medical data, such as patient records, medical test results, or treatment history.
Sensor data: Synthetic datasets that resemble real-world sensor data such as temperature readings, humidity levels, or air quality measurements.
Sales data: Synthetic datasets that resemble real-world sales data, such as sales transactions, product information, or customer behavior.
Inventory data: Synthetic datasets that resemble real-world inventory data, such as stock levels, product information, or supplier information.
Marketing data: Synthetic datasets that resemble real-world marketing data, such as campaign performance, customer behavior, or market trends.
Human resources data: Synthetic datasets that resemble real-world human resources data, such as employee records, performance evaluations, or salary information.
Challenges with Creating Synthetic Data
According to sources including Towards Data Science, enov8, and J.P. Morgan, there are several challenges in creating synthetic data, including:
Technical difficulty: Properly modeling complex real-world behaviors such as synthetic data is challenging, given available technologies.
Biased behavior: The flexible nature of synthetic data makes it prone to potentially biased results.
Privacy concerns: Care must be taken to ensure synthetic data does not reveal sensitive information.
Quality of the data model: If the quality of the data model is not high, wrong conclusions can be reached.
Time and effort: Synthetic data generation requires time and effort.
Difficult Patterns and Behaviors to Model
Many patterns and behaviors can be challenging to model in synthetic data, for example:
Rare events: If certain events rarely occur in the real world, generating synthetic data that accurately reflects their distribution can be difficult.
Complex relationships: Synthetic data generators may struggle to capture complex relationships between variables, such as non-linear interactions, feedback loops, or conditional dependencies.
Contextual variability: Contextual factors, such as time, location, or individual differences, can have a significant impact on the distribution of data. Modeling this variability accurately can be challenging.
Outliers and anomalies: Synthetic data generators may be unable to generate outliers or anomalies that are realistic and representative of the real world.
Dynamic data: If the data is dynamic and changes over time, it can be challenging to generate synthetic data that captures these changes accurately.
Unobserved variables: Sometimes, variables may be necessary for understanding the data distribution but are not directly observable. These variables can be challenging to model in synthetic data.
Data bias: If the real-world data is biased in some way, such as over-representation of certain groups or under-representation of others, it can be challenging to generate synthetic data that is unbiased and representative of the population.
Time-dependent patterns: If the data exhibits time-dependent patterns, such as seasonality or trends, it can be challenging to generate synthetic data that accurately reflects them.
Spatial patterns: If the data has a spatial component, such as location data or images, it can be challenging to generate synthetic data that captures the spatial patterns realistically.
Data sparsity: If the data is sparse or incomplete, it can be difficult to generate synthetic data that accurately reflects the distribution of the entire dataset.
Human behavior: If the data involves human behavior, such as in social science or behavioral economics, it can be challenging to model the complex and nuanced behaviors of individuals and groups.
Sensitive or confidential information: In some cases, the data may contain sensitive or confidential information that cannot be shared, making it challenging to generate synthetic data that preserves privacy while accurately reflecting the underlying distribution.
Overall, many patterns can be challenging to model accurately in synthetic data. It often requires careful consideration of the specific data characteristics and the synthetic data generation techniques’ limitations.
Easily Modeled Patterns and Behaviors
There are many simple and well-understood patterns that can be easily modeled in synthetic data, for example:
Randomness: If the data is purely random, it can be easily generated using a random number generator or other simple techniques.
Gaussian distribution: If the data follows a Gaussian or normal distribution, it can be generated using a Gaussian random number generator.
Uniform distribution: If the data follows a uniform distribution, it can be easily generated using a uniform random number generator.
Linear relationships: If the data follows a linear relationship between variables, it can be modeled using simple linear regression techniques.
Categorical variables: If the data consists of categorical variables, such as gender or occupation, it can be generated using a categorical distribution.
Text data: If the data consists of text, it can be generated using natural language processing techniques, such as language models or text generation algorithms.
Seasonality: If the data exhibits seasonal patterns, such as higher sales data during holiday periods, it can be generated using seasonal time series models.
Proportions and percentages: If the data consists of proportions or percentages, such as product sales distribution across different regions, it can be generated using beta or Dirichlet distributions.
Multivariate normal distribution: If the data follows a multivariate normal distribution, it can be generated using a multivariate Gaussian random number generator.
Networks: If the data consists of network or graph data, such as social networks or transportation networks, it can be generated using network models, such as Erdos-Renyi or Barabasi-Albert models.
Binary data: If the data consists of binary data, such as whether a customer churned, it can be generated using a Bernoulli distribution.
Geospatial data: If it involves geospatial data, such as the location of points of interest, it can be easily using geospatial models, such as point processes or spatial point patterns.
Customer behaviors: If the data involves customer behaviors, such as browsing or purchase histories, it can be generated using customer journey models, such as Markov models.
In general, simple and well-understood patterns can be easily modeled using synthetic data techniques. In contrast, more complex and nuanced patterns may require more sophisticated modeling techniques and a deeper understanding of the underlying data characteristics.
Creating Synthetic Data with Generative AI
We can use many popular generative AI-powered tools to create synthetic data for testing applications, constructing analytics pipelines, and building machine learning models. Tools include OpenAI ChatGPT, Microsoft’s all-new Bing Chat, ChatSonic, Tabnine, GitHub Copilot, and Amazon CodeWhisperer. For more information on these tools, check out my recent blog post:
Let’s start with a simple example of generating synthetic sales data. Suppose we have created a new sales forecasting application for coffee shops that we need to test using synthetic data. We might start by prompting a generative AI tool like OpenAI’s ChatGPT for some data:
Create a CSV file with 25 random sales records for a coffee shop. Each record should include the following fields: - id (incrementing integer starting at 1) - date (random date between 1/1/2022 and 12/31/2022) - time (random time between 6:00am and 9:00pm in 1-minute increments) - product_id (incrementing integer starting at 1) - product - calories - price in USD - type (drink or food) - quantity (random integer between 1 and 3) - amount (price * quantity) - payment type (cash, credit, debit, or gift card)
The content and structure of a prompt can vary, and this can strongly influence ChatGPT’s response. Based on the above prompt, the results were accurate but not very useful for testing our application in this format. ChatGPT cannot create a physical CSV file. Furthermore, ChatGPT’s response length is limited; only about twenty records were returned. According to ChatGPT, in general, ChatGPT can generate responses of up to 2,048 tokens, the maximum output length allowed by the GPT-3 model.
OpenAI ChatGPT UI
Instead of outputting the actual synthetic data, we could ask ChatGPT to write a program that can, in turn, generate the synthetic data. This option is certainly more scalable. Let’s prompt ChatGPT to write a Python program to generate synthetic sales data with the same characteristics as before:
Create a Python3 program to generate 100 sales of common items sold in a coffee shop. The data should be written to a CSV file and include a header row. Each record should include the following fields: - id (incrementing integer starting at 1) - date (random date between 1/1/2022 and 12/31/2022) - time (random time between 6:00am and 9:00pm in 1-minute increments) - product_id (incrementing integer starting at 1) - product - calories - price in USD - type (drink or food) - quantity (random integer between 1 and 3) - amount (price * quantity) - payment type (cash, credit, debit, or gift card)
Using a single concise prompt, ChatGPT generated a complete Python program, including code comments, to generate synthetic sales data. Unfortunately, given ChatGPT’s response size limitation, the coffee shop menu was limited to just six items. Reprompting for more items would result in the truncation of the output and, thus, the program, making it unrunnable. Instead, we could use an additional prompt to generate a longer Python list of menu items and combine the two pieces of code in our IDE. Regardless, we will still need to copy and paste the code into our IDE to review, debug, test, and run.
OpenAI ChatGPT UI
ChatGPT’s Python program, copied and pasted into VS Code, ran without modifications, and wrote 100 synthetic sales records to a CSV file!
Running ChatGPT’s Python program in VS Code
Using IDE-based Generative AI Tools
Although generating synthetic data directly or snippets of code in chat-based generative AI tools are helpful for limited use cases, writing code in IDE gives us several advantages:
Code does not need to be copied and pasted from external sources into an IDE
Consecutive lines of code, method, and block code completion overcome the single response size limits of chat-based tools like OpenAI ChatGPT
Code can be reused and adapted to evolving use cases over time
Python interpreter and debugger or equivalent for other languages
Automatic code formatting, linting, and code style enforcement
Unit, integration, and functional testing
Static code analysis (SCA)
Vulnerability scanning
IntelliSense for code completion
Source code management (SCM) / version control
Let’s use the same techniques we used with ChatGPT, but from within an IDE to generate three types of synthetic data. We will choose Microsoft’s VS Code with GitHub Copilot and Python as our programming language.
VS Code IDE with GitHub Copilot Nightly extension installed
Source Code
All the code examples shown in this post can be found on GitHub.
Example #1: Coffee Shop Sales Data
First, we will start by outlining the program’s objective using code comments on the top of our Python file. This detailed context helps us to clearly express our goal and enables Copilot to generate an accurate response.
# Write a program that creates synthetic sales data for a coffee shop. # The program should accept a command line argument that specifies the number of records to generate. # The program should write the sales data to a file called 'coffee_shop_sales_data.csv'. # The program should contain the following functions: # - main() function that calls the other functions # - function that returns one random product from a list of dictionaries # - function that returns a dictionary containing one sales record # - function that writes the sales records to a file
Following the import statements also generated with the assistance of Copilot, we will write the first function to return a random product from a list of 25 products. Again, we will use code comments as a prompt to generate the code. Copilot was able to generate 100% of the function’s code from the comments.
# Write a function to create list of dictionaries. # The list of dictionaries should contain 15 drink items and 10 food items sold in a coffee shop. # Include the product id, product name, calories, price, and type (Food or Drink). # Capilize the first letter of each product name. # Return a random item from the list of dictionaries.
Below is an example of Copilot’s ability to generate complete lines of code. Ultimately, it generated 100% of the function including choosing the items sold in a coffee shop, with a reasonable price and caloric count. Copilot is not limited to just understanding code.
Copilot can generate entire lines of code
Next, we will write a function to return a random sales record.
# Write a function to return a random sales record. # The record should be a dictionary with the following fields: # - id (an incrementing integer starting at 1) # - date (a random date between 1/1/2022 and 12/31/2022) # - time (a random time between 6:00am and 9:00pm in 1 minute increments) # - product_id, product, calories, price, and type (from the get_product function) # - quantity (a random integer between 1 and 3) # - amount (price * quantity) # - payment type (Cash, Credit, Debit, Gift Card, Apple Pay, Google Pay, or Venmo)
Again, Copilot generated 100% of the function’s code as a single block based on the code comments.
Copilot can generate entire blocks of code or methods
Lastly, we will create a function to write the sales data into a CSV file using Copilot’s help.
# Write a function to write the sales records to a CSV file called 'coffee_shop_sales.csv'. # Use an input parameter to specify the number of records to write. # The CSV file must have a header row and be comma delimited. # All string values must be enclosed in double quotes.
Again below, we see an example of Copilot’s ability to generate an entire Python function. I needed to correct a few problems with the generated code. First, there was a lack of quotes for string values, which I added to the function (quotechar='"', quoting=csv.QUOTE_NONNUMERIC). Also, the function was missing a key line of code, sale = get_sales_record(), which would have caused the code to fail. Remember, just because the code was generated does not mean it is correct.
Copilot can generate entire blocks of code or methods
Here is the complete program that creates synthetic sales data for a coffee shop with Copilot assistance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copilot generated an astounding 80–85% of the program’s final code. The initial program took 10–15 minutes to write using code comments. I then added a few new features, including the ability to pass in the record count on the command line and the hash-based transaction id, which took another 5 minutes. Finally, I used GitHub Code Brushes to optimize the code and generate the Python docstrings, and Black Formatter and Flake8 extensions to format and lint, all of which took less than 5 minutes. With testing and debugging, the total time was about 25–30 minutes.
The most significant difference with Copilot was that I never had to leave the IDE to look up code references or find existing sales datasets or even a coffee shop menu to duplicate. The code, as well as the list of products, price, calories, and product type, were all generated by Copilot.
To make this example more realistic, you could use Copilot’s assistance to write algorithms capable of reflecting daily, weekly, and seasonal variations in product choice and sales volumes. This might include simulating increased sales during the busy morning rush hour or a preference for iced drinks in the summer months versus hot drinks during the winter months.
Here is an example of the synthetic sales data output by the example application:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We could use these same techniques to generate a list of residential addresses. To start, we can prompt Copilot for the values in a list of common street names and street types in the United States:
# Write a function that creates a list of common street names # in the United States, in alphabetical order. # List should be in alphabetical order. Each name should be unique. # Return a random street name. def get_street_name(): street_names = [ "Ash", "Bend", "Bluff", "Branch", "Bridge", "Broadway", "Brook", "Burg", "Bury", "Canyon", "Cape", "Cedar", "Cove", "Creek", "Crest", "Crossing", "Dale", "Dam", "Divide", "Downs", "Elm", "Estates", "Falls", "Fifth", "First", "Fork", "Fourth", "Glen", "Green", "Grove", "Harbor", "Heights", "Hickory", "Hill", "Hollow", "Island", "Isle", "Knoll", "Lake", "Landing", ... ]
return random.choice(street_names)
# Write a function that creates a list of common street types # in the United States, in alphabetical order. # List should be in alphabetical order. Each name should be unique. # Return a random street type. def get_street_type(): street_types = [ "Alley", "Avenue", "Bend", "Bluff", "Boulevard", "Branch", "Bridge", "Brook", "Burg", "Circle", "Commons", "Court", "Drive", "Highway", "Lane", "Parkway", "Place", "Road", "Square", "Street", "Terrace", "Trail", "Way" ]
return random.choice(street_types)
Next, we can create a function that returns a property type based on a categorical distribution of common residential property types with the prompt:
# Write a function to return a random property type. # Accept a random value between 0 and 1 as an input parameter. # The function must return one of the following values based on the %: # 63% Single-family, 26% Multi-family, 4% Condo, # 3% Townhouse, 2% Mobile home, 1% Farm, 1% Other.
Again, Copilot generated 100% of the function’s code as a single block based on the code comments.
Copilot generating a categorical distribution of common residential property types
Additionally, we could have Copilot help us generate a list of the 50 largest cities in the United States with state, zip code, and population, with the prompt:
# Write a function to returns the 50 largest cities in the United States. # List should be sorted in descending order by population. # Include the city, state abbreviation, zip code, and population. # Return a list of dictionaries.
Once again, Copilot generated 100% of the function’s code using a combination of single lines and code blocks based on the code comments.
Copilot generating a block of US cities with state, zip code, and populations
When randomly choosing a city, we can use a categorical distribution of populations of all the cities to control the distribution of cities in the final synthetic dataset. For example, there will be more addresses in larger cities like New York City or Los Angeles than in smaller cities like Buffalo or Virginia Beach, with the prompt:
# Write a function that calculates the total population of the list of cities. # Add a 'pcnt_of_total_population' and 'pcnt_running_total' columns to list. # Returns a sorted list of cities by population.
Here is the complete program that creates synthetic US-based address data with Copilot assistance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To make this example more realistic, you could use Copilot’s assistance to write algorithms capable of accurately reflecting assessed property values based on the type of residence and the zip code.
Here is an example of the synthetic US-based residential address data output by the example application:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We could use the same techniques again to generate synthetic demographic data. With the assistance of Copilot, we can write functions that randomly return typical feminine or masculine first names (forenames) and common last names (surnames) found in the United States, for this use case.
# Write a function that generates a list of common feminine first names in the United States. # List should be in alphabetical order. # Each name should be unique. # Return random first name. def get_first_name_feminine(): first_name_feminine = [ "Alice", "Amanda", "Amy", "Angela", "Ann", "Anna", "Barbara", "Betty", "Brenda", "Carol", "Carolyn", "Catherine", "Christine", "Cynthia", "Deborah", "Debra", "Diane", "Donna", "Doris", "Dorothy", "Elizabeth", "Frances", "Gloria", "Heather", "Helen", "Janet", "Jennifer", "Jessica", "Joyce", "Julie", "Karen", "Kathleen", "Kimberly", ]
return random.choice(first_name_feminine)
With the assistance of Copilot, we can also write functions that return demographic information, such as age, gender, race, marital status, religion, and political affiliation. Similar to the previous sales data example, we can influence the final synthetic dataset based on categorical distributions of different demographic categories, for instance, with the prompt:
# Write a function that returns a person's martial status. # Accept a random value between 0 and 1 as an input parameter. # The function must return one of the following values based on the %: # 50% Married, 33% Single, 17% Unknown. def get_martial_status(rnd_value): if rnd_value < 0.50: return "Married" elif rnd_value < 0.83: return "Single" else: return "Unknown"
By altering the categorical distributions, we can quickly alter the resulting synthetic dataset to reflect differing demographic characteristics: an older or younger population, the predominance of a single race, religious affiliation, or marital status, or the ratio of males to females.
Next, we can use a Gaussian distribution (aka normal distribution) to return the year of birth in a bell-shaped curve, given a mean year and a standard deviation, using Python’s random.normalvariate function.
# Write a function that generates a normal distribution of date of births. # with a mean year of 1975 and a standard deviation of 10. # Return random date of birth as a string in the format YYYY-MM-DD def get_dob(): day_of_year = random.randint(1, 365) year_of_birth = int(random.normalvariate(1975, 10)) dob = date(int(year_of_birth), 1, 1) + timedelta(day_of_year - 1) dob = dob.strftime("%Y-%m-%d") return dob
Here is the complete program that creates synthetic demographic data with Copilot’s assistance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To make this example more realistic, you could use Copilot’s assistance to write algorithms capable of more accurately representing the nuanced associations and correlations between age, gender, race, marital status, religion, and political affiliation.
Here is an example of the synthetic demographic data output by the example application:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In addition to writing code and documentation, a common use of generative AI code assistants like Copilot is unit tests. For example, we can create unit tests for each function in our coffee shop sales data code generator, using the same method of prompting with code comments.
Copilot can also assist with writing unit tests
Conclusion
In this post, we learned how Generative AI could assist us in creating synthetic data for software development, analytics, and machine learning. The examples herein generated data using simple techniques. Using advanced modeling techniques, we could generate increasingly complex, realistic synthetic data.
🔔 To keep up with future content, follow Gary Stafford on LinkedIn.
This blog represents my viewpoints and not those of my employer, Amazon Web Services (AWS). All product names, logos, and brands are the property of their respective owners.
AWS Area Principal Solutions Architect | 10x AWS Certified Pro | DevOps | Data/ML | Serverless | Polyglot Developer | Former ThoughtWorks and Accenture