Mining Seasonal Products from Sales Data

The other day someone asked me how to include products with seasonal demand in recommendations based on collaborative filtering or some other technique. The solution to the problem involves two steps. The first step is to identify products with seasonal demand. The second step involves merging two ranked lists, one list being the recommendation list based on some recommendation algorithm, the other being the list of products with seasonal demand. The second list is a function of time.

Our focus in this post is on the first problem. As we will explore here in this post, products with seasonal demand can be found with some simple statistical technique. The solution involves running three Hadoop Map Reduce jobs in tandem. The implementation is available in open source project chombo available on github.

Order Transaction Data as Input

Our use case is for an on line store. We use the order transaction data with the following components.

  1. Transaction ID
  2. Product ID
  3. Monetary amount
  4. Time stamp

The monetary amount is used as an indicator for demand. We could have also used product quantity. Here is some sample input. Our data will contain higher than normal pre school sales for certain products.

Solution Outline

At any given time of the year we want the solution to give us a list of products that has high seasonal demand at that time of the year.

The solution has three steps. The first step is to detect seasonality in the data. Data with seasonal components has the following characteristics.

  1. A fixed cycle of seasonality e.g for week of the year seasonality,the cycle is a year
  2. A time interval e.g for week of the year seasonality, the interval is 7 days
  3. Seasonal cycle index, e.g for week of the year seasonality, the index is between 0 and 53

In this step, we process past transaction data and bin the transaction amount with one bin per week. Since are using week of the year as the seasonality cycle, there will be 53 bins, one of each week in an year.

We sum the amount for each bin. The output is essentially an de normalized histogram, A week is considered to begin on a Monday. Since there may be partial weeks near the year boundary, we may have have up to 53 weeks in a year.More on seasonality detection can be found in an earlier post.

In the second step we take the output of the first step and calculate normalized distribution of transaction amount and certain statistical properties from the distribution.

In the third and final step, we take the output of the second step and identify the modes of the histogram for products with significant non uniformity in the histogram distribution. The modes will correspond to weeks with  higher than average demand.

Map Reduce Pipeline

As mentioned earlier, there are three map reduce jobs that run in tandem, one for each of the 3 steps as described earlier. The first  map reduce job SeasonalDetector maps the time stamp to a week of the year index i.e cycle index for each transaction. It’s output  is as follows. There is one output record for each product and week of the years.


The different fields are as follows.

  1. Product ID
  2. Parent cycle index
  3. Cycle index i.e. week of the year in this case
  4. Attribute index
  5. Total amount

Some seasonality cycle have a parent cycle. For example, day of the month has parent cycle which is month of the year.

We have a field called attribute index which refers to the field for transaction amount in the input. It’s there to identify the column being analyzed, because this map reduce job can process multiple data columns for seasonality.

The next map reduce job NumericalAttrDistrStats takes the input of the first job and calculates distribution and some statistical quantities for each product. The  histogram bin is the week of the year index and the bin value is the total amount for the week. Here is some sample output. There is one output record for each product.


The fields are as follows. For our problem, we will be using the histogram and optionally the entropy value.

  1. Product ID
  2. Number of bins
  3. Bin and bin value pairs for all the bins. The output could be normalized optinally
  4. Entropy
  5. Mode
  6. 25% quantile
  7. 50% quantile or median
  8. 75% quantile

The  final map reduce job SeasonalCycleFinder detects modes in the histogram of products with significant non uniformity in distribution.  There are two ways to detect non uniformity and modes as follows

  1. Modes with value some percentage above the average value
  2. Top n modes.

There is also an additional filtering based on entropy. Entropy is a measure of randomness in a distribution. Distributions with high non uniformity and peaks have low entropy value.

With this filter on, we apply a upper limit for the entropy. With the second strategy for mode identification, it is important to apply the entropy based filter. Here is some sample output. We have used the first strategy for mode identification. The threshold for mode is set to 200% of average.


The fields in the output are as follows. There is one record for each week of the year.

  1. Week of the year index
  2. List of  product ID and  weekly sales amount for the product

We notice a sharp rise in the number of products from week 28 through week 34. That range of weeks happens to be the pre school sales season.

Product Recommendation

Although not the main focus, I will discuss briefly the technique to include seasonal products into the core product recommendation list. Typically, a personalized recommendation engine will have output with following components.

  1. User ID
  2. Product ID
  3. Score

We have to convert the seasonal product list to a similar format. We can can convert the amount which reflects demand to a score with same scale as the recommendation list. We also also have to replicate the list for each user by pre pending the the user IDs.

Once we have both ranked list, we can use the map reduce job  RankAggregator to consolidate the two lists and generate a net recommendation list. The scores from the two list could be combined in several ways, including average, weighted average etc.

Summing Up

We have gone through a Hadoop map reduce based solution for identifying seasonal products from sales data.  To execute the steps in this use case, please use the tutorial.

For commercial support for this solution or other solutions in my github repositories, please talk to ThirdEye Data Science Services. Support is available for Hadoop or Spark deployment on cloud including installation, configuration and testing,


About Pranab

I am Pranab Ghosh, a software professional in the San Francisco Bay area. I manipulate bits and bytes for the good of living beings and the planet. I have worked with myriad of technologies and platforms in various business domains for early stage startups, large corporations and anything in between. I am an active blogger and open source project owner. I am passionate about technology and green and sustainable living. My technical interest areas are Big Data, Distributed Processing, NOSQL databases, Machine Learning and Programming languages. I am fascinated by problems that don't have neat closed form solution.
This entry was posted in Big Data, Data Mining, Data Science, eCommerce, Map Reduce, Recommendation Engine and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s