You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于交易层级创建Apriori关联规则并限定Level1值仅在LHS?

Absolutely, you can leverage the hierarchical item metadata in the Groceries dataset to build Apriori association rules that meet your specific requirement—restricting certain Level1 category values to only appear in the left-hand side (LHS) of rules. Let’s walk through this with concrete steps and code examples.

1. Load Required Packages and Dataset

First, make sure you have the arules package installed and loaded, then import the Groceries dataset and examine its item metadata:

# Install if needed
install.packages("arules")
library(arules)

# Load and inspect the dataset
data("Groceries")
str(Groceries)
str(Groceries@itemInfo)

As you noted, Groceries@itemInfo includes labels (individual product names), level1 (broad product categories like "dairy products"), and level2 (more specific subcategories).

2. Identify Items Linked to Your Target Level1 Category

To enforce that a specific Level1 category only appears in the LHS, we first need to map that category to all individual product labels it contains. For example, if we want to restrict "dairy products" to the LHS:

# Define your target Level1 category
target_level1 <- "dairy products"

# Extract all product labels belonging to this Level1 category
lhs_only_items <- Groceries@itemInfo$labels[Groceries@itemInfo$level1 == target_level1]

This gives us a list of every product under the "dairy products" umbrella that we want to lock into the LHS.

3. Run Apriori with Appearance Constraints

The apriori() function has an appearance parameter that lets you control where items can appear in rules. We’ll use this to specify that our target Level1 items only go in the LHS, and all other items default to the RHS:

# Generate association rules with constraints
custom_rules <- apriori(
  data = Groceries,
  parameter = list(support = 0.001, confidence = 0.1), # Adjust thresholds to fit your needs
  appearance = list(
    lhs = lhs_only_items, # Target Level1 items stay in LHS
    default = "rhs"       # All other items can only be in RHS
  )
)
  • The support and confidence parameters are adjustable—tweak them based on how strict you want your rules to be.
  • default = "rhs" ensures that any item not in lhs_only_items can’t show up in the LHS, which enforces your requirement perfectly.

4. Verify the Rules Meet Your Constraint

Check the generated rules to confirm that your target Level1 items only appear in the LHS:

# Inspect the top 5 rules sorted by confidence
inspect(head(custom_rules, n = 5, by = "confidence"))

You’ll see that every rule has at least one "dairy products" item on the left, and no such items on the right.

5. Extend to Multiple Level1 Categories

If you need to restrict multiple Level1 categories to the LHS, just expand your target list and adjust the item selection:

# Define multiple target Level1 categories
target_level1s <- c("dairy products", "bread and cake")

# Extract all labels for these categories
lhs_only_items <- Groceries@itemInfo$labels[Groceries@itemInfo$level1 %in% target_level1s]

# Re-run Apriori with the updated constraint
custom_rules_multi <- apriori(
  data = Groceries,
  parameter = list(support = 0.001, confidence = 0.1),
  appearance = list(lhs = lhs_only_items, default = "rhs")
)

This approach works because we’re translating hierarchical category constraints into item-level constraints that the Apriori algorithm can understand via the appearance parameter.

内容的提问来源于stack exchange,提问作者Nell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:43:42