An offline shopping recommendation engine using the Apriori algorithm and association analysis

Machine learning methods are often applied to model complex systems where the function mapping inputs to outputs is unknown but a relationship is suspected or known to exist. Human behavior is one of these complex systems where machine learning can add insight to apparently random behavior. By looking at large samples of behavior, machine learning practitioners can highlight patterns. Retail stores and marketers have a vested interest in determining these patterns to support their decision-making processes and ultimately maximize profits.

To empirically show the power of machine learning in the retail and marketing domain, I apply association analysis to Tom Brijs’s retail dataset. Tom Brijs’s retail dataset (“retail.dat”, 4MB) is an anonymized set of approximately 88,000 real-world sales transactions from a Belgian supermarket.  By determining frequently co-occurring sets of products, retailers can make more personal recommendations to shoppers or group those products closer together to take advantage of the natural impulse to purchase those items together.

I implement an offline shopping recommendation engine using Agrawal and Srikant’s Apriori algorithm and association rule extraction. This engine is trained and tested on the publicly-available retail dataset. If the engine performs well, real-world data can then be used and the resulting rules/recommendations can then be experimentally applied in the real-world domain. The recommendation engine can be improved by continuing to train the engine with later purchasing behavior. In this way, the system can receive feedback on its own recommendations.

The engine is implemented using the Python programming language with the NumPy and SciPy scientific computing libraries. The Apriori algorithm and analytical components are developed through guidance from Tom Brijs’’s own academic publications as well as popular publications for machine learning practitioners.


Shopping behaviors of customers in stores can be understood as a function of the influences of variables such as desires, needs, and socio-economic factors. For retailers and marketers to capitalize on these mysterious mappings, they could commission complex studies requiring years, a staff of highly-trained researchers, and great expense to determine patterns of behavior among consumers. Retailers and marketers could then devise strategies to maximize profits by exploiting these patterns.

Fortunately, such a time-consuming and costly approach is unnecessary. Retailers and marketers already have the behavioral data they need in the form of shopping transactions of existing customers. By applying machine learning methods to find undiscovered links between their products, retailers and marketers can more efficiently target their advertising, placement, and pricing to take advantage of these links.

Additionally, retailers can directly recommend items for a customer to purchase given the items they are or have selected in the past. This model has become prevalent among online retailers, which provides a personalized shopping experience based on the retailer’s knowledge of the customer’s purchasing patterns and other customers who have similar purchasing patterns.




Brick-and-mortar retailers currently employ schemes such as customer loyalty and rewards programs to track demographic and purchasing information. This data can be anonymized and similar individuals can be compared for patterns or targeted for personalized marketing (Harrington, 224). Alternatively, credit cards can be used to associate purchases with a specific individual. In cases where multiple purchases cannot be linked to an individual, the transaction can still be treated as an itemset and compared against other itemsets.

Lots of sources of behavioral information. Wal-Mart – logistical cost minimization. KDD

What are the limits of current practice?


While some other machine learning frameworks, such as Tom Brijs’s PROFSET, focus on maximizing use of retailer resources (Brijs, et al., 2000), a customer-focused approach through personalized recommendations can be successful by exploiting the customer’s self-interest rather than the retailer.

“The technique of association rules produces a set of rules describing underlying purchase patterns in the data, like for instance bread => (implies) cheese [support = 20%, confidence = 75% ].”

Association analysis: Support and confidence

Support: Percentage of the dataset that contains this itemset

Lift: lift(X -> Y) = P( Y | X) / P( Y )

The probability of consequent Y given the presence of antecedent itemset X, divided by the probability of Y in the entire dataset.

Apriori algorithm: Alternative to brute-force method                                                                                                                                                                                                                                                                                                                                                                                                    RRESULTS AND CONCLUSIONS

While looking at anonymized behavior in the form of sales transactions is clearly valuable, the inclusion of demographic information could further improve the value of the basket analysis for marketers in particular. For personalized recommendations, inclusion of demographic data can provide additional dimensions which could be used as part of a broader “itemset” that includes features of a consumer’s common identity as well as common purchase items.


Brijs, T. Retail Market Basket Data Set. Limburgs Universitair Centrum: Diepenbeenk, Belgium. or

Brijs, T., Swinnen, G. and Vanhoof, G. (1999). Using association rules for product assortment decisions: a case study. In Proceedings of the Fifth International Conference on Knowledge Discovery and Data Mining. Association for Computing Machinery: San Diego. 254-260.

Brijs, T., Swinnen, G. and Vanhoof, G. (2000). A Data Mining Framework for Optimal Product Selection in Retail Supermarket Data: The Generalized PROFSET Model. In KDD ’00 Proceedings of the sixth ACM SIGKDD international conference on Knowledge discovery and data mining. Association for Computing Machinery: New York. 300 – 304.

Coelho, L. & Richert, W. (2013). Building Machine Learning Systems with Python. Packt Publishing: Birmingham, UK. 147 – 179.

Harrington, P. (2012). Machine Learning in Action. Manning: Shelter Island. 225 – 265.

Leave a Reply

Your email address will not be published. Required fields are marked *