0% found this document useful (0 votes)
38 views3 pages

15th QN

Uploaded by

imadvocate31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

15th QN

Uploaded by

imadvocate31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Certainly!

Here's a simplified version of the Apriori algorithm implementation in Python for


association rule mining on a groceries dataset:

```python
from itertools import combinations, chain
from collections import defaultdict

# Step 1: Load the Dataset


def load_dataset(file_path):
transactions = []
with open(file_path, 'r') as f:
for line in f:
transaction = frozenset(line.strip().split(','))
transactions.append(transaction)
return transactions

# Step 2: Apriori Algorithm to Find Frequent Itemsets


def apriori(transactions, min_support):
itemset = set()
for transaction in transactions:
for item in transaction:
itemset.add(frozenset([item])) # Generate 1-itemsets

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

def join_set(itemset, length):


return set([i.union(j) for i in itemset for j in itemset if len(i.union(j)) == length])

# Step 3: Generate Association Rules


def generate_rules(itemsets, transactions, min_confidence):
rules = []
for itemset in itemsets:
if len(itemset) > 1:
for subset in chain(*[combinations(itemset, i) for i in range(1, len(itemset))]):
subset = frozenset(subset)
remain = itemset.difference(subset)
if remain:
support_itemset = sum(1 for trans in transactions if itemset.issubset(trans))
support_subset = sum(1 for trans in transactions if subset.issubset(trans))
confidence = support_itemset / support_subset
if confidence >= min_confidence:
rules.append((subset, remain, confidence))
return rules

# Example Usage
file_path = 'path/to/your/groceries.csv'
transactions = load_dataset(file_path)
min_support = 2
min_confidence = 0.5

# Find frequent itemsets using Apriori


itemsets = apriori(transactions, min_support)

# Generate association rules


rules = generate_rules(itemsets, transactions, min_confidence)

# 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.

You might also like