15th QN
15th QN
```python
from itertools import combinations, chain
from collections import defaultdict
frequent_itemsets = dict()
current_itemset = itemset
k=1
while current_itemset:
itemset_support = defaultdict(int)
for transaction in transactions:
for item in current_itemset:
if item.issubset(transaction):
itemset_support[item] += 1
current_itemset = set()
for item, support in itemset_support.items():
if support >= min_support:
frequent_itemsets[item] = support
current_itemset.add(item)
current_itemset = join_set(current_itemset, k + 1)
k += 1
return frequent_itemsets
# Example Usage
file_path = 'path/to/your/groceries.csv'
transactions = load_dataset(file_path)
min_support = 2
min_confidence = 0.5
# Print rules
for rule in rules:
print(f"Rule: {set(rule[0])} => {set(rule[1])} (Confidence: {rule[2]:.2f})")
```
### Explanation
- **Loading the Dataset**: The `load_dataset` function reads the CSV file and converts each line into
a transaction represented as a frozenset.
- **Apriori Algorithm**: The `apriori` function iteratively finds frequent itemsets starting from 1-
itemsets up to higher-order itemsets based on the minimum support threshold.
- **Generating Association Rules**: The `generate_rules` function generates association rules from
the frequent itemsets and calculates their confidence based on the minimum confidence threshold.
- **Example Usage**: Replace `'path/to/your/groceries.csv'` with the actual path to your CSV file
containing the transactions.
### Notes
- **Simplicity**: This implementation focuses on simplicity while covering the essential steps of the
Apriori algorithm for association rule mining.
- **Adjustments**: You can adjust `min_support` and `min_confidence` according to your specific
dataset and requirements.
This code provides a straightforward implementation of Apriori in Python for association rule mining
on transactional data like groceries.