Building a Market Basket Analysis with Overseed
This week we launched the multi-value probability specification. This specification allows users to configure a probability on a combination of two or more values! We will use a Market Basket Analysis to demonstrate this feature.
Imagine a grocery store that would like to understand the relationship between two items sold in the store. The store would like to know if there is a relationship between Milk and Eggs in customers' purchases.
This relationship will help them gain insight into the following questions:
- Should we place Milk and Eggs closer to each other in the store?
- Can we suggest Eggs as a customer's next purchase if they put Milk in their basket on our online store?
- Can we bundle the Milk and Eggs with a discount to increase the sales of both items, for example, when we have an overstock?
The store tasks the data science team with building an algorithm that reveals the association between Milk and Eggs. Conclusively the team wants to answer the following question:
- How likely are customers going to buy Eggs if they buy Milk?
The team wants to prototype the algorithm right away. However, the same old question comes up:
How do we get access to this data?
That question always holds back the data science team. It could mean days or weeks, requesting access to databases, getting approvals, or creating scripts to generate mock data. These activities take away from the ability to prototype quickly and focus on a working solution.
Overseed to the rescue
With Overseed, they can start right away using synthetic data! The team can build a schema and generate the data they need for their prototype. Moreso, they can version this schema as they uncover edge-cases and make changes to their data.
Building the schema
The team designs a schema and configures the values to establish a strong association between Milk and Eggs as follows:
The schema specifies four fields, simulating a market basket of 4 items:
- item_1 specifies the attribute as a standard datatype of fruit. The specification will result in a random fruit value for that field, mimicking a customer placing a random item in their market basket.
- item_2 specifies the attribute as a standard datatype of vegetable. The specification will result in a random vegetable value for that field, mimicking a customer placing a random in their market basket.
- item_3 and item_4 specify two attributes with a primitive datatype of string. They will configure the values and probability using the randospecs section in their schema. In the schema, we see the values Milk for item_3 and Eggs for item_4 configured at a probability of 5%. This probability indicates that customers will buy Milk and Eggs 5% of the time. In other words, in a dataset of 100 market baskets, Milk will appear in item_3, and Eggs will appear in item_4 approximately ten times.
Generating the data
Now that the team has configured the association between Milk and Eggs; They can generate a synthetic dataset for their algorithm to analyze. They directly upload this schema to Overseed and create a static generator. A static generator is a good choice since the team will use a CSV file in their prototype. If the team chose to make this a real-time system in the future, they could use a stream generator.
Connecting to the data
The team decided to build the prototype in a Jupyter Notebook. In the notebook, the team calls the Overseed API and retrieves the generated dataset.
Next, the notebook displays the counts for the values in the synthetic dataset and a histogram for the Milk and Eggs values, as shown in the image below.
The histogram shows the equally distributed values for Milk and Eggs individually. In comparison, the combination of the values is about half of the count. They know the Apriori algorithm should calculate a strong relationship between the two items!
Finally, they calculate the association between all the items in our dataset. The team is interested in only the Milk and Eggs relationship, so they output that result as shown in the image below.
Success! The team was able to capture the association between Milk and Eggs as configured in the synthetic dataset. They can now conclude the following from their output:
- Milk and Eggs are bought together by about 4.85% of our customers.
- Customers are likely to buy Milk and Eggs about 32.79% of the time.
- It is about 218.62% more likely for customers to purchase Eggs if they just bought Milk.
They are now ready to demo the prototype and apply it to production data with confidence. Their schema in Overseed can evolve their analysis evolves.
Using Overseed's multi-value probability specification, the team created an association between Milk and Eggs in the data and then discovered it using their algorithm. The team can now apply this algorithm in the systems where it is helpful such as:
- Online store basket recommendations
- Store product placement
- Discount Bundle campaigns
Ultimately, we saw how a data science team can build a Market Basket Analysis using a synthetic dataset generated by Overseed. The synthetic dataset allowed the team to prototype quickly without being blocked by data access barriers. Moreover, Overseed's versioning feature will enable them to add more attributes and capture edge-cases as the project evolves.
We look forward to how you use this feature in the wild. Give it a try and give us feedback.
We are open for early access! Sign-up here.
The code for this article is in this notebook. Feel free to copy the notebook to your own Colaboratory Google Workspace. You can run the notebook using the following steps:
- Create the schema in this post under a project in your Overseed organization.
- Create a static generator of 10,000 rows using the schema.
- Replace the dataset URL with your static generator's URL.
- Get your Overseed API Token from the Account Settings and enter it into the input box when prompted at run-time.