Publish AI, ML & data-science insights to a global community of data professionals.
How I constructed thousands of rows of data points from scratch
Data is the foundation of data science and machine learning. Thousands upon thousands of data points are needed in order to analyze, visualize, draw conclusions, and build ML models. In some situations, data is readily available and free to download. Other times, the data is nowhere to be found.
In situations where data is not readily available but needed, you’ll have to resort to building up the data yourself. There are many methods you can use to acquire this data from webscraping to APIs. But sometimes, you’ll end up needing to create fake or "dummy" data. Dummy data can be useful in times where you know the exact features you’ll be using and the data types included but, you just don’t have the data itself.
Here, I’ll show you how I created 100,000 rows of dummy data. This data is also not purely random. If it was, building it would’ve been much easier. Sometimes when creating dummy data from scratch, you’ll need to develop tendencies or patterns within the data that might reflect real world behavior. You’ll see what I mean later on.
Sign up for a Medium Membership here to gain unlimited access and support content like mine! With your support I earn a small portion of the membership fee. Thanks!
Let’s say you are building an app from the ground up and need to establish a large user base for example testing. You are provided with a list of features and their respective data types.
This user base also needs to somewhat accurately reflect real world users and tendencies so it can’t be completely random. For example, you don’t want a user to have a college degree who is also 10 years old. Or, you might not want an overrepresentation of a specific data point, such as more males than females. These are all points of consideration to be aware of as you create your dataset.
Since real world data is rarely, truly random, a dataset such as this would be a great simulation for other datasets you’ll handle in the future. It could also serve as a testing ground for any machine learning models you wish to train.
Now let’s get started, feel free to follow along, and I’ll show you how I built this dataset…
To code along, start by importing the following libraries:
The dataset size will be 100,000 data points (you can do more but it may take longer to process). I assigned this amount to a constant variable, which I used throughout:
I picked 10 features I expected to be the most common in a regular dataset of users. These features and respective data types are:
I inputted the above as a list of features to initialize a Pandas dataframe:
Some attributes above should normally contain imbalanced data. It can be safely assumed with some quick research, some choices will not be equally represented. For a more realistic dataset, these tendencies need to be reflected.
For the ID attribute, I used the uuid library to generate a random string of characters 100,000 times. Then, I assigned it to the ID attribute in the dataframe.
UUID is a great library to generate unique IDs for each user because of its astronomically low chance of duplicating an ID. It is a great go-to when it comes to generating unique identifying sets of characters. But, if you want to be sure that no IDs were repeated, then you can do a simple check on the dataframe with the following:
This will return True if all the IDs in the dataset are unique.
This attribute is one of the instances where an equally random choice should probably not be used. Because, it can be safely assumed that each choice is not equally likely to occur.
For gender, I provided three options: male, female, and na. However, if I were to use Python’s random library, then each choice might be equally shown in the dataset. In reality, there would be significantly more male and female than na choices. So I decided to display that imbalance in the data:
By using the random library, I provided the choices() function with the list of gender options, weights for each choice, and how many choices to make represented by "k". It was then assigned to the dataframe’s "gender" attribute. The imbalance I described before, is represented in the weights section with a "na" choice appearing about 6% of the time.
For this attribute, the choices can be randomly selected between True and False. Since it can be reasonably expected that about half of the users would be subscribers.
Just like "Gender" before, I used random.choices() but without weights because this attribute can be randomly split between the two choices.
Here I used the Faker library to create thousands of names for all these users. The Faker library is great in this situation because it has an option for male and female names. In order to process the gendered names, I created a function to assign names based on a given gender.
I used my simple function to quickly produce a list of names based on the "Gender" attribute data before and assigned it to the dataframe.
Email turned out to be one of the trickier attributes. I wanted to create email addresses related to the names generated. However, there would probably be a chance of duplication because people can share the same name but not the same email.
First, I created a new function that would format names into email addresses with a default domain name. It would also handle duplicate addresses by simply adding a random number at the end of the formatted name:
Now in order to appropriately harness the purpose of this function, I created a loop that would rerun the function when necessary while iterating through the "Name" attribute. The loop would keep rerunning the function until a unique email name was created.
After all emails have been generated, I assigned them to the dataframe’s "Email" attribute. You can also do an optional check to see that each email is unique with the same method as IDs.
This attribute now requires specific formatting which was made easier with the utilization of the datetime library. Here I wanted users to have a history of logging in within the past month or so. I used another custom function to help:
The function basically generates a list of timestamps between two given times. It generated a list of random timestamps to assign to the dataframe.
This attribute is an easy one since it is similar to "Last Login". All I did was change the time format by removing the hour, minute, and seconds. I used datetime again to help randomly choose a date for each user but this time the range of time started from 1980 to 2006 to get a nice random distribution of ages. The code below is largely the same as before but with a different format and date range:
The "education" attribute is dependent on "dob". In these instances, the level of education is based on the user’s age and not the highest level of education they attained. This is another one of those attributes where randomly picking a level of education would not reflect real world tendencies.
I created another simple function that checks a user’s age based on today’s date and returns their current education level.
After generating a list of education levels, I then assigned it to the dataframe.
For this attribute, I wanted to vary the length of the bio depending on the user’s subscription status. If a user was a subscriber, then I would assume that their bios would be longer than non-subscribers.
To accommodate this aspect, I built a function that checks their subscription status and returns a random sentence from Faker that varies in length.
In the above function, I randomly chose the length of the fake sentences based on the subscription status. If they were a subscriber then their bios leaned towards being longer than usual and vice-versa.
To round out this dataset, I wanted to include a numerical data type. I chose "rating" to be of an integer type. The rating of 1 to 5 represents anything and it’s just there for any discretionary purpose.
For the ratings themselves, I chose to skew the distribution of 1 to 5 towards the extremes to reflect the tendency of users seemingly being more absolute with their ratings.
I used the random.choices() function once again but with the weights skewed towards 1 and 5.
Now that the data is complete and if you were coding along, feel free to view the dataframe before you decide to save it. If it all looks good, then save the dataframe as a .csv file with this simple command:
This saves the dataset as a fairly large CSV file in your local directory. And if you want to check on your saved dataset, used this command to view it:
Everything should look good and now, if you wish, you can perform some basic data visualization. If you want to learn more about data visualization check out my following article:
Why I Use Plotly for Data Visualization
I hoped you learned something new from my experience building this dataset. If anything, building it provided a bit of practice in creating Python functions and algorithms. Also, I believe building your own dataset can equip you with a better understanding of datasets in general. It can perhaps prepare you to handle any large amounts of data you need.
If you wish, you can improve upon it by adding more restrictions or dependencies to the data distribution or even add more attributes to the dataframe. Feel free to explore the data or expand upon it!
Not a Medium Member? Click here to support Medium and me!
Medium-Misc-Tutorials/Build-a-Dataset.ipynb at main · marcosan93/Medium-Misc-Tutorials
Written By
Share This Article
Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.
Step-by-step code guide to building a Convolutional Neural Network
A beginner’s guide to forecast reconciliation
Here’s how to use Autoencoders to detect signals with anomalies in a few lines of…
Feature engineering, structuring unstructured data, and lead scoring
Solving the resource constrained project scheduling problem (RCPSP) with D-Wave’s hybrid constrained quadratic model (CQM)
An illustrated guide on essential machine learning concepts
Derivation and practical examples of this powerful concept
Your home for data science and Al. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.











Leave a Reply