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

Program

The document provides a Python program that implements the Apriori algorithm for mining frequent itemsets from transaction data. It allows users to input transactions, specify minimum support and confidence levels, and then generates and displays selected frequent itemsets based on the input criteria. The program utilizes the pandas library for data manipulation and the mlxtend library for the Apriori algorithm.
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)
5 views2 pages

Program

The document provides a Python program that implements the Apriori algorithm for mining frequent itemsets from transaction data. It allows users to input transactions, specify minimum support and confidence levels, and then generates and displays selected frequent itemsets based on the input criteria. The program utilizes the pandas library for data manipulation and the mlxtend library for the Apriori algorithm.
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

Program:

import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

def input_transactions():
transactions = []
items_set = set()
n_transactions = int(input("Enter the number of transactions: "))

for i in range(n_transactions):
transaction_input = input(f"Enter Transaction {i+1} in format 'ID - items': ")
transaction_items = transaction_input.split('-')[1].strip().split()
items_set.update(transaction_items)
transactions.append(transaction_items)

items = sorted(items_set)
transaction_data = []

for transaction in transactions:


transaction_data.append([item in transaction for item in items])

df = pd.DataFrame(transaction_data, columns=items)
return df

def apriori_steps(df, min_support, min_confidence):


print("\nInput Data (Transactions):")
print(df.astype(int))
print(f"\nGenerating frequent itemsets with minimum support of {min_support} transactions...")

support_threshold = min_support / len(df)

frequent_itemsets = apriori(df, min_support=support_threshold, use_colnames=True)

all_itemsets = apriori(df, min_support=support_threshold, use_colnames=True)


all_itemsets['support_count'] = all_itemsets['support'] * len(df)

selected_itemsets = all_itemsets[all_itemsets['support_count'] >= min_support]


dejected_itemsets = all_itemsets[all_itemsets['support_count'] < min_support]

for i in range(1, all_itemsets['itemsets'].apply(lambda x: len(x)).max() + 1):


print(f"\nSelected {i}-itemsets (support >= {min_support} transactions):")
if selected_itemsets.empty:
print("No selected itemsets found.")
else:
print(selected_itemsets[selected_itemsets['itemsets'].apply(lambda x: len(x) == i)][['itemsets',
'support_count']])

if __name__ == "__main__":
df = input_transactions()
min_support = int(input("\nEnter minimum support: "))
min_confidence = float(input("Enter minimum confidence as a percentage (e.g., 70 for 70%): ")) / 100
apriori_steps(df, min_support, min_confidence)
Output:

You might also like