Association Rules with Online Transactions from H&M

Kamil Stawikowski

02.2024


Introduction

In this project, we delve into the world of unsupervised learning through market basket analysis, focusing specifically on association rule mining. Our primary dataset comes from H&M’s online transactions, offering a rich source of customer purchase data. The goal is to unveil the hidden links between products that customers tend to buy together, leveraging these insights to potentially enhance cross-selling strategies and improve customer shopping experiences.

Utilizing the arules package in R, we process and analyze the transactional data to identify meaningful patterns and associations between items. By fine-tuning parameters such as support and confidence, we aim to highlight the most significant product connections. The project emphasizes the practical application of unsupervised learning techniques to real-world data, showcasing how subtle relationships in customer behavior can be brought to light through association rule mining.

H&M

H&M (Hennes & Mauritz AB) is a global retail giant known for its fashion-forward clothing for men, women, children, and teenagers. Founded in Sweden in 1947, the company has grown to become one of the world’s leading fashion retailers, offering a wide range of apparel, accessories, and home goods. H&M is celebrated for its affordable, high-quality, and sustainable fashion, operating in numerous markets worldwide with a strong presence both in physical stores and online.

Data

The dataset provided for the Kaggle competition “H&M Personalized Fashion Recommendations” is a comprehensive collection of data from H&M, designed to support the development of personalized fashion recommendations. In this competition, H&M Group encouraged to develop product recommendations based on data from previous transactions, as well as from customer and product meta data. The dataset features a wide array of items across various categories, enabling participants to analyze purchasing patterns, preferences, and behaviors of H&M’s customers. With this dataset, competitors are tasked with creating models that can predict which items a customer will buy, aiming to enhance the shopping experience through tailored fashion suggestions.

data <- read_parquet("articles.pqt")
data1 <- read_parquet("transactions.pqt")

Selecting and converting products variable to character in both datasets

data_m <- data %>% select(article_id, prod_name) %>%
  mutate(article_id = as.character(article_id))
data_m1 <- data1 %>% select(customer_id, article_id) %>%
  mutate(article_id = as.character(article_id))

Determining the top 300 most popular products. With almost 46 000 product and 32 000 000 records in a data set, author decided to limit dataset due to problems with computing with such big dataset.

top_300_articles <- data_m1 %>%
  group_by(article_id) %>%
  summarise(transaction_count = n(), .groups = 'drop') %>%
  arrange(desc(transaction_count)) %>%
  slice_head(n = 300)

Filtering data_m to keep only the top 300 most popular articles

data_m_filtered <- data_m %>%
  semi_join(top_300_articles, by = "article_id")

Joining the filtered article data with transactions

joined_table <- inner_join(data_m_filtered, data_m1, by = "article_id")

Creating a table of customers with more than one transaction. Limiting Data to customers with more than one transactiion alow for better results.

customers_with_many_transactions <- data_m1 %>%
  group_by(customer_id) %>%
  summarise(transaction_count = n(), .groups = 'drop') %>%
  filter(transaction_count > 1)

Joining with the original joined_table to limit to customers with many transactions

final_table <- joined_table %>%
  inner_join(customers_with_many_transactions, by = "customer_id")

Display the final_table

head(final_table)
## # A tibble: 6 × 4
##   article_id prod_name customer_id                             transaction_count
##   <chr>      <chr>     <chr>                                               <int>
## 1 108775015  Strap top 05ed96931b707698bc94aa53766d44686ae5cc…                89
## 2 108775015  Strap top 05ed96931b707698bc94aa53766d44686ae5cc…                89
## 3 108775015  Strap top 0aaa9683d5f45b85d0f9a81e2f4d4ef774fed4…                49
## 4 108775015  Strap top 20ccafd82d923baecf1fb8705d459e243c8ca9…                25
## 5 108775015  Strap top 2135790e6b63d10b59f9d8b84a58e24328b0f5…                38
## 6 108775015  Strap top 3329634c9451f438049c5ebb4e77d0e0b7c730…                59
summary(final_table)
##   article_id         prod_name         customer_id        transaction_count
##  Length:2855974     Length:2855974     Length:2855974     Min.   :   2.0   
##  Class :character   Class :character   Class :character   1st Qu.:  23.0   
##  Mode  :character   Mode  :character   Mode  :character   Median :  50.0   
##                                                           Mean   :  76.4   
##                                                           3rd Qu.:  97.0   
##                                                           Max.   :1895.0
final_table<- final_table %>% select(-transaction_count)
summary(final_table)
##   article_id         prod_name         customer_id       
##  Length:2855974     Length:2855974     Length:2855974    
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character
transactions <- final_table %>%
  group_by(customer_id) %>%
  summarise(products = list(prod_name)) %>%
  ungroup()
# Create a list where each element is a character vector of product names per customer
trans_list <- lapply(transactions$products, unlist)

# Ensure that each element of trans_list is a character vector
trans_list <- lapply(trans_list, as.character)

Convert the list to a transactions object

trans <- as(trans_list, "transactions")
## Warning in asMethod(object): removing duplicated items in transactions

Frequency of Products

Those plots shows us what products were chosen most frequently - absolutely and relatively.

itemFrequencyPlot(trans, topN = 20, type = "absolute", main = "Top 20 Items")

itemFrequencyPlot(trans, topN = 20, type = "relative", main = "Top 20 Items")

transaction_lengths <- size(trans)

Sparcity of the data The plot below shows how sparse is the matrix for the first 20 transactions

image(trans[1:20])

The plot below shows how sparse is the matrix for the random sample of 200 transactions

image(sample(trans, 200))

##Mining association rules with Apriori The Apriori algorithm is a cornerstone in data mining for discovering frequent itemsets and deriving association rules from large databases of transactions. The algorithm iterates through levels of itemsets, starting from single items and expanding to larger combinations, each time calculating their support to determine their frequency. Apriori is particularly famed for its application in market basket analysis, where it helps unveil patterns about items often purchased together, providing valuable insights for cross-selling and recommendation systems in retail contexts.

Low Support, High Confidence Rules

rules <- apriori(trans, parameter = list(supp = 0.001, conf = 0.6))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.6    0.1    1 none FALSE            TRUE       5   0.001      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 685 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[208 item(s), 685018 transaction(s)] done [0.10s].
## sorting and recoding items ... [208 item(s)] done [0.02s].
## creating transaction tree ... done [0.21s].
## checking subsets of size 1 2 3 4 done [0.05s].
## writing ... [15 rule(s)] done [0.00s].
## creating S4 object  ... done [0.05s].
# Viewing the rules
inspect(rules[1:5]) 
##     lhs                                 rhs                          support confidence    coverage     lift count
## [1] {S Timeless High Wide Side Brie} => {Shake it in Balconette} 0.004677249  0.6185328 0.007561845 22.80810  3204
## [2] {The Low Line Highwaist}         => {Timeless Sports Top}    0.011939832  0.7662545 0.015582072 44.34384  8179
## [3] {Timeless Sports Top}            => {The Low Line Highwaist} 0.011939832  0.6909690 0.017279838 44.34384  8179
## [4] {The Low Line Highwaist,                                                                                      
##      Therese tee}                    => {Timeless Sports Top}    0.001113839  0.8302503 0.001341571 48.04734   763
## [5] {Therese tee,                                                                                                 
##      Timeless Sports Top}            => {The Low Line Highwaist} 0.001113839  0.7607178 0.001464195 48.82007   763
# Sorting rules by lift
rules_sorted <- sort(rules, by = "lift")

# Inspect top rules sorted by lift
inspect(rules_sorted[1:5])
##     lhs                             rhs                          support confidence    coverage     lift count
## [1] {Perrie Slim Mom Denim TRS,                                                                               
##      Timeless Sports Top}        => {The Low Line Highwaist} 0.001223326  0.7666972 0.001595579 49.20380   838
## [2] {Therese tee,                                                                                             
##      Timeless Sports Top}        => {The Low Line Highwaist} 0.001113839  0.7607178 0.001464195 48.82007   763
## [3] {The Low Line Highwaist,                                                                                  
##      Therese tee}                => {Timeless Sports Top}    0.001113839  0.8302503 0.001341571 48.04734   763
## [4] {The Low Line Highwaist,                                                                                  
##      Tilda tank}                 => {Timeless Sports Top}    0.001108000  0.8232104 0.001345950 47.63994   759
## [5] {Perrie Slim Mom Denim TRS,                                                                               
##      The Low Line Highwaist}     => {Timeless Sports Top}    0.001223326  0.8215686 0.001489012 47.54493   838
inspect(sort(rules, by="confidence")[1:5])
##     lhs                             rhs                          support confidence    coverage     lift count
## [1] {The Low Line Highwaist,                                                                                  
##      Therese tee}                => {Timeless Sports Top}    0.001113839  0.8302503 0.001341571 48.04734   763
## [2] {The Low Line Highwaist,                                                                                  
##      Tilda tank}                 => {Timeless Sports Top}    0.001108000  0.8232104 0.001345950 47.63994   759
## [3] {Perrie Slim Mom Denim TRS,                                                                               
##      The Low Line Highwaist}     => {Timeless Sports Top}    0.001223326  0.8215686 0.001489012 47.54493   838
## [4] {The Low Line Highwaist,                                                                                  
##      Timeless Midrise Brief}     => {Timeless Sports Top}    0.001017492  0.8011494 0.001270040 46.36325   697
## [5] {Perrie Slim Mom Denim TRS,                                                                               
##      Timeless Sports Top}        => {The Low Line Highwaist} 0.001223326  0.7666972 0.001595579 49.20380   838
inspect(sort(rules, by="support")[1:5])
##     lhs                                 rhs                          support confidence    coverage     lift count
## [1] {The Low Line Highwaist}         => {Timeless Sports Top}    0.011939832  0.7662545 0.015582072 44.34384  8179
## [2] {Timeless Sports Top}            => {The Low Line Highwaist} 0.011939832  0.6909690 0.017279838 44.34384  8179
## [3] {S Timeless High Wide Side Brie} => {Shake it in Balconette} 0.004677249  0.6185328 0.007561845 22.80810  3204
## [4] {Jade HW Skinny Denim TRS,                                                                                    
##      The Low Line Highwaist}         => {Timeless Sports Top}    0.001611636  0.7624309 0.002113813 44.12257  1104
## [5] {Jade HW Skinny Denim TRS,                                                                                    
##      Timeless Sports Top}            => {The Low Line Highwaist} 0.001611636  0.7263158 0.002218920 46.61227  1104

The Apriori algorithm’s generated association rules offer significant insights into H&M customers’ buying behaviors, unveiling powerful relationships among different products.

High Confidence and Lift: The rules {The Low Line Highwaist, Therese tee} => {Timeless Sports Top} and its converse {Therese tee, Timeless Sports Top} => {The Low Line Highwaist} have very high confidence and lift values, which suggests that these items are not only frequently bought together but also have a strong positive association. If a customer buys one, they are very likely to buy the other.

Sorting by Lift: When we sort the rules by lift, we notice that items like Perrie Slim Mom Denim TRS and Timeless Sports Top when bought together suggest a strong likelihood that The Low Line Highwaist will also be bought. These high-lift values indicate a stronger relationship than would be expected if the items were statistically independent.

Sorting by Confidence: Sorting the rules by confidence shows that combinations of items like The Low Line Highwaist and Therese tee have the highest confidence in leading to a purchase of Timeless Sports Top. This can be an indicator for recommending Timeless Sports Top to customers who have either of the other two items in their cart.

Sorting by Support: Sorting by support highlights the most frequent item combinations across all transactions. For example, {The Low Line Highwaist} => {Timeless Sports Top} has the highest support, meaning it occurs in a significant proportion of transactions, making it a reliable pattern in the dataset. Product Placement: Items like ‘Timeless Sports Top’ and ‘The Low Line Highwaist’ could be placed close to each other in both physical stores and online recommendation systems.

Cross-Selling: Sales staff could be trained to recommend items that have strong associations, such as suggesting ‘Shake it in Balconette’ to customers buying ‘S Timeless High Wide Side Brie’.

Scatter plot of support vs. confidence

plot(rules, measure = c("support", "confidence"), shading = "lift")

plot(rules, method = "matrix", measure = c("support", "confidence"), shading = "lift")
## Itemsets in Antecedent (LHS)
##  [1] "{Perrie Slim Mom Denim TRS,Timeless Sports Top}"     
##  [2] "{Therese tee,Timeless Sports Top}"                   
##  [3] "{The Low Line Highwaist,Therese tee}"                
##  [4] "{The Low Line Highwaist,Tilda tank}"                 
##  [5] "{Perrie Slim Mom Denim TRS,The Low Line Highwaist}"  
##  [6] "{Jade HW Skinny Denim TRS,Timeless Sports Top}"      
##  [7] "{The Low Line Highwaist,Timeless Midrise Brief}"     
##  [8] "{Tilda tank,Timeless Sports Top}"                    
##  [9] "{The Low Line Highwaist}"                            
## [10] "{Timeless Sports Top}"                               
## [11] "{Jade HW Skinny Denim TRS,The Low Line Highwaist}"   
## [12] "{S Timeless High Wide Side Brie}"                    
## [13] "{Lazer Razer Brief,Shake it in Balconette}"          
## [14] "{Shake it in Balconette,Simple as That Triangle Top}"
## [15] "{Julia RW Skinny Denim TRS,Madison skinny HW (1)}"   
## Itemsets in Consequent (RHS)
## [1] "{Jade HW Skinny Denim TRS}" "{Timeless Midrise Brief}"  
## [3] "{Shake it in Balconette}"   "{Timeless Sports Top}"     
## [5] "{The Low Line Highwaist}"

Graph plot to visualize a network of rules

subrules <- head(sort(rules, by = "lift"), 20)  # Subset top rules by lift for clearer visualization
plot(subrules, method = "graph")

The graph helps identify which items are commonly associated with others, with the thickness of the edges possibly representing the confidence or lift of the rule. Items that are central with many connections, such as “Timeless Sports Top” and “The Low Line Highwaist,” could be key products that are frequently purchased with other items. The size of the nodes may indicate the support or frequency of the items in the dataset, with larger nodes being more common.

plot(subrules, method="paracoord", control=list(reorder=TRUE))

Adjusting support and confidence

rules2 <- apriori(trans, parameter = list(supp = 0.008, conf = 0.1))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.1    0.1    1 none FALSE            TRUE       5   0.008      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 5480 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[208 item(s), 685018 transaction(s)] done [0.10s].
## sorting and recoding items ... [146 item(s)] done [0.02s].
## creating transaction tree ... done [0.19s].
## checking subsets of size 1 2 done [0.01s].
## writing ... [16 rule(s)] done [0.00s].
## creating S4 object  ... done [0.06s].

Viewing the top rules sorted by lift

rules_sorted_by_lift <- sort(rules2, by = "lift")
inspect(rules_sorted_by_lift[1:5])
##     lhs                              rhs                              support confidence   coverage      lift count
## [1] {The Low Line Highwaist}      => {Timeless Sports Top}         0.01193983  0.7662545 0.01558207 44.343845  8179
## [2] {Timeless Sports Top}         => {The Low Line Highwaist}      0.01193983  0.6909690 0.01727984 44.343845  8179
## [3] {Simple as that Cheeky Tanga} => {Simple as That Triangle Top} 0.01988415  0.5145242 0.03864570 11.959498 13621
## [4] {Simple as That Triangle Top} => {Simple as that Cheeky Tanga} 0.01988415  0.4621832 0.04302223 11.959498 13621
## [5] {Shake it in Balconette}      => {Timeless Midrise Brief}      0.01458502  0.5378156 0.02711900  8.373221  9991
inspect(sort(rules2, by="confidence")[1:5])
##     lhs                              rhs                              support confidence   coverage      lift count
## [1] {The Low Line Highwaist}      => {Timeless Sports Top}         0.01193983  0.7662545 0.01558207 44.343845  8179
## [2] {Timeless Sports Top}         => {The Low Line Highwaist}      0.01193983  0.6909690 0.01727984 44.343845  8179
## [3] {Shake it in Balconette}      => {Timeless Midrise Brief}      0.01458502  0.5378156 0.02711900  8.373221  9991
## [4] {Simple as that Cheeky Tanga} => {Simple as That Triangle Top} 0.01988415  0.5145242 0.03864570 11.959498 13621
## [5] {Simple as That Triangle Top} => {Simple as that Cheeky Tanga} 0.01988415  0.4621832 0.04302223 11.959498 13621
inspect(sort(rules2, by="support")[1:5])
##     lhs                              rhs                              support confidence   coverage      lift count
## [1] {}                            => {Jade HW Skinny Denim TRS}    0.10211994  0.1021199 1.00000000  1.000000 69954
## [2] {Simple as that Cheeky Tanga} => {Simple as That Triangle Top} 0.01988415  0.5145242 0.03864570 11.959498 13621
## [3] {Simple as That Triangle Top} => {Simple as that Cheeky Tanga} 0.01988415  0.4621832 0.04302223 11.959498 13621
## [4] {Shake it in Balconette}      => {Timeless Midrise Brief}      0.01458502  0.5378156 0.02711900  8.373221  9991
## [5] {Timeless Midrise Brief}      => {Shake it in Balconette}      0.01458502  0.2270733 0.06423043  8.373221  9991

Sorted by Lift: The first two rules have a very high lift of approximately 44.34, indicating that the presence of ‘The Low Line Highwaist’ in a transaction strongly implies the presence of ‘Timeless Sports Top’, and vice versa.

The third and fourth rules also have a high lift, around 11.96, suggesting that ‘Simple as that Cheeky Tanga’ and ‘Simple as That Triangle Top’ are frequently purchased together.

The fifth rule, with a lift of about 8.37, implies that ‘Shake it in Balconette’ and ‘Timeless Midrise Brief’ are also commonly bought together.

Sorted by Confidence” The first rule here indicates a confidence of approximately 76.63%, which means that when ‘The Low Line Highwaist’ is bought, in about 76.63% of those cases, ‘Timeless Sports Top’ is also purchased.

Similarly, the second rule shows that ‘Timeless Sports Top’ leads to ‘The Low Line Highwaist’ in about 69.10% of the transactions.

For the other rules, confidence ranges from approximately 46.22% to 53.78%, showing that these items also have a strong likelihood of being bought together.

Sorted by Support: The first rule, which is the empty set leading to ‘Jade HW Skinny Denim TRS’, has a support of about 10.21%, showing that this item is present in roughly 10.21% of all transactions, making it a very common item.

The second and third rules involving ‘Simple as that Cheeky Tanga’ and ‘Simple as That Triangle Top’ have a support of about 1.99%, indicating they are less common but still significant.

The next rules have lower supports, around 1.46%, but these associations are still important within the context of the dataset.

Scatter plot for support vs. confidence

plot(rules2, measure = c("support", "confidence"), shading = "lift")

top_rules <- head(sort(rules2, by = "lift"), 20)
plot(top_rules, method = "graph")

Pruned Rules

pruned_rules <- subset(rules2, lift > 3 & confidence > 0.3)
inspect(pruned_rules[1:5])
##     lhs                            rhs                            support confidence   coverage      lift count
## [1] {The Low Line Highwaist}    => {Timeless Sports Top}      0.011939832  0.7662545 0.01558207 44.343845  8179
## [2] {Timeless Sports Top}       => {The Low Line Highwaist}   0.011939832  0.6909690 0.01727984 44.343845  8179
## [3] {Jade Denim TRS}            => {Jade HW Skinny Denim TRS} 0.008055263  0.3977224 0.02025348  3.894659  5518
## [4] {Shake it in Balconette}    => {Timeless Midrise Brief}   0.014585018  0.5378156 0.02711900  8.373221  9991
## [5] {Julia RW Skinny Denim TRS} => {Jade HW Skinny Denim TRS} 0.011602615  0.3122373 0.03715961  3.057554  7948

The first two rules have an exceptionally high lift, suggesting a very strong association between The Low Line Highwaist and Timeless Sports Top, making them prime candidates for marketing strategies. The confidence levels are also varied, with the highest being the first rule. This suggests that while the association is strong, the likelihood of the consequent item being purchased given the antecedent item varies significantly across these rules. The support levels, while relatively low in absolute terms, are typical for retail datasets which often contain a large number of unique items and consequently, a wide distribution of itemset frequencies.

plot(rules2, method = "matrix", measure = c("support", "confidence"), shading = "lift")
## Itemsets in Antecedent (LHS)
##  [1] "{The Low Line Highwaist}"      "{Timeless Sports Top}"        
##  [3] "{Simple as that Cheeky Tanga}" "{Shake it in Balconette}"     
##  [5] "{Simple as That Triangle Top}" "{Timeless Midrise Brief}"     
##  [7] "{Jade Denim TRS}"              "{Julia RW Skinny Denim TRS}"  
##  [9] "{Curvy Jeggings HW Ankle}"     "{Jade HW Skinny Denim TRS}"   
## [11] "{Perrie Slim Mom Denim TRS}"   "{Luna skinny RW}"             
## [13] "{}"                           
## Itemsets in Consequent (RHS)
## [1] "{Luna skinny RW}"              "{Jade HW Skinny Denim TRS}"   
## [3] "{Julia RW Skinny Denim TRS}"   "{Timeless Midrise Brief}"     
## [5] "{Simple as That Triangle Top}" "{Shake it in Balconette}"     
## [7] "{Simple as that Cheeky Tanga}" "{The Low Line Highwaist}"     
## [9] "{Timeless Sports Top}"

Summary and Conclusion

In essence, the analysis of H&M customer purchase patterns through association rules provides deep insights into the interrelationships between products. Understanding these patterns allows retailers to fine-tune their approach across various aspects of the business, including marketing initiatives tailored to consumer buying habits, strategic stock planning, and crafting individualized product suggestions. Observed associations provide a foundation for strategic decision-making. The study highlights connections among specific categories of fashion items, hinting at potential for increased sales through cross-promotion and improved consumer contentment. Utilizing these insights, H&M and associated retailers can refine their product selections to resonate with consumer tastes, fostering a more compelling and streamlined digital apparel shopping journey. The utilization of association rules analysis emerges as a crucial tool for businesses intent on maintaining adaptability and reactivity within the ever-evolving e-commerce arena.