[go: up one dir, main page]

0% found this document useful (0 votes)
11 views2 pages

Data Mining with FP-Growth

The document discusses implementing association rule mining using the FP-Growth algorithm in Python. It loads market basket optimization data, converts it to transactions, applies FP-Growth to find frequent itemsets, generates association rules from the frequent itemsets, and sorts the rules by confidence.

Uploaded by

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

Data Mining with FP-Growth

The document discusses implementing association rule mining using the FP-Growth algorithm in Python. It loads market basket optimization data, converts it to transactions, applies FP-Growth to find frequent itemsets, generates association rules from the frequent itemsets, and sorts the rules by confidence.

Uploaded by

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

Kumaraguru College of Technology, Coimbatore

Department of Computer Science and Engineering


U18CSI6203-DATA WAREHOUSING AND DATA MINING
Academic Year: 2023 – 2024

Association Rule Mining [CO4, K3]


• Implement Python code to apply FP Growth algorithm to mine association rules for the
given dataset. Use suitable packages.
• Use Market_Basket_Optimisation dataset attached.
Code:
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import fpgrowth, association_rules
import numpy as np

# Load the dataset


df = pd.read_csv('Market_Basket_Optimisation.csv', header=None)
print(df.shape)
print(df.head())

# Convert the dataset into a list of transactions


transactions = []
for i in range(len(df)):
transactions.append([str(item) for item in df.iloc[i].dropna()])

# Use TransactionEncoder to transform the transaction data


te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df_te = pd.DataFrame(te_ary, columns=te.columns_)
# Apply the FP-Growth algorithm to find frequent itemsets
frequent_itemsets = fpgrowth(df_te, min_support=0.05, use_colnames=True)

# Generate association rules


rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.2)

# Display the frequent itemsets and association rules


print("Frequent Itemsets:")
print(frequent_itemsets)
print("\nAssociation Rules:")
print(rules)

print("\nSorting the Rules based on confidence")


sorted_rules = rules.sort_values(by='confidence', ascending=False)
print(sorted_rules)

Output:

The output shows that {Spaghetti}->{Mineral Water} has the highest confidence and they are
more related to each other.

You might also like