0% found this document useful (0 votes)
7 views5 pages

Efficient Apriori Algorithm in Python

The document outlines a process for installing the 'efficient-apriori' package and using it to analyze two datasets: 'groceriesDataset.csv' and '5000i.csv'. It includes steps for uploading the datasets, cleaning the data, mapping items to integers, and running the apriori algorithm to find frequent itemsets and association rules. However, the results indicate that no frequent itemsets or association rules were found for both datasets under the specified support and confidence thresholds.

Uploaded by

5-Min Crafts
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)
7 views5 pages

Efficient Apriori Algorithm in Python

The document outlines a process for installing the 'efficient-apriori' package and using it to analyze two datasets: 'groceriesDataset.csv' and '5000i.csv'. It includes steps for uploading the datasets, cleaning the data, mapping items to integers, and running the apriori algorithm to find frequent itemsets and association rules. However, the results indicate that no frequent itemsets or association rules were found for both datasets under the specified support and confidence thresholds.

Uploaded by

5-Min Crafts
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

In [1]: !

pip install efficient-apriori

from efficient_apriori import apriori


import pandas as pd
from [Link] import files # for uploading CSVs

Collecting efficient-apriori
Downloading efficient_apriori-[Link] (6.7 kB)
Downloading efficient_apriori-[Link] (14 kB)
Installing collected packages: efficient-apriori
Successfully installed efficient-apriori-2.0.6

In [2]: # Upload [Link]


print("Upload [Link]")
u1 = [Link]()
groceries = pd.read_csv(next(iter([Link]())), header=None)

# Upload [Link] (or your second dataset)


print("Upload [Link]")
u2 = [Link]()
retail = pd.read_csv(next(iter([Link]())), header=None)

Upload [Link]
Upload widget is only available when the cell has been executed
Browse...
in the current browser session. Please rerun this cell to enable.
Saving [Link] to [Link]
Upload [Link]
Upload widget is only available when the cell has been executed
Browse...
in the current browser session. Please rerun this cell to enable.
Saving [Link] to [Link]

In [ ]: !pip install efficient-apriori

from efficient_apriori import apriori


import pandas as pd
from [Link] import files # for uploading CSVs
# Upload [Link]
print("Upload [Link]")
u1 = [Link]()
groceries = pd.read_csv(next(iter([Link]())), header=None)

# Upload [Link] (or your second dataset)


print("Upload [Link]")
u2 = [Link]()
retail = pd.read_csv(next(iter([Link]())), header=None)
# Convert each row to list of items (remove NaN)
def clean_row(row):
return [str(x).strip() for x in row if str(x) != "nan"]

transactions_g = [Link](clean_row, axis=1).tolist()


transactions_r = [Link](clean_row, axis=1).tolist()

# Remove header-like row if present


if transactions_g and "Item(s)" in transactions_g[0]:
transactions_g = transactions_g[1:]
# Map unique items to integers (required for efficient_apriori)
def create_mapping(transactions):
item_to_id = {}
id_to_item = {}
mapped = []
next_id = 1

for trans in transactions:


new_t = []
for item in trans:
if item not in item_to_id:
item_to_id[item] = next_id
id_to_item[next_id] = item
next_id += 1
new_t.append(item_to_id[item])
[Link](tuple(new_t))

return mapped, id_to_item

mapped_g, g_id_to_item = create_mapping(transactions_g)


mapped_r, r_id_to_item = create_mapping(transactions_r)

# Helper to run apriori & print results with readable names


def run_apriori(title, mapped_data, id_map, supp, conf):
print("\n" + "="*70)
print(f"{title} | min_support={supp}, min_confidence={conf}")
print("="*70)

itemsets, rules = apriori(


mapped_data,
min_support=supp,
min_confidence=conf
)

# Frequent itemsets
print("\nFrequent Itemsets:")
if not itemsets:
print(" None found.")
else:
for size, group in [Link]():
for items, support in [Link]():
readable = [id_map[i] for i in items]
print(" ", readable, "| support =", round(support, 4))

# Rules
print("\nAssociation Rules:")
if not rules:
print(" None found.")
else:
for r in rules:
lhs = [id_map[i] for i in [Link]]
rhs = [id_map[i] for i in [Link]]
print(" ", lhs, "->", rhs,
"| support =", round([Link], 4),
"| confidence =", round([Link], 4),
"| lift =", round([Link], 4))
run_apriori("Groceries Dataset (Case A)",
mapped_g, g_id_to_item,
supp=0.50, conf=0.75)

run_apriori("Retail Dataset (Case A)",


mapped_r, r_id_to_item,
supp=0.50, conf=0.75)
run_apriori("Groceries Dataset (Case B)",
mapped_g, g_id_to_item,
supp=0.60, conf=0.60)

run_apriori("Retail Dataset (Case B)",


mapped_r, r_id_to_item,
supp=0.60, conf=0.60)

In [3]: # Convert each row to list of items (remove NaN)


def clean_row(row):
return [str(x).strip() for x in row if str(x) != "nan"]

transactions_g = [Link](clean_row, axis=1).tolist()


transactions_r = [Link](clean_row, axis=1).tolist()

# Remove header-like row if present


if transactions_g and "Item(s)" in transactions_g[0]:
transactions_g = transactions_g[1:]

In [4]: # Map unique items to integers (required for efficient_apriori)


def create_mapping(transactions):
item_to_id = {}
id_to_item = {}
mapped = []
next_id = 1

for trans in transactions:


new_t = []
for item in trans:
if item not in item_to_id:
item_to_id[item] = next_id
id_to_item[next_id] = item
next_id += 1
new_t.append(item_to_id[item])
[Link](tuple(new_t))

return mapped, id_to_item

mapped_g, g_id_to_item = create_mapping(transactions_g)


mapped_r, r_id_to_item = create_mapping(transactions_r)
In [5]: # Helper to run apriori & print results with readable names
def run_apriori(title, mapped_data, id_map, supp, conf):
print("\n" + "="*70)
print(f"{title} | min_support={supp}, min_confidence={conf}")
print("="*70)

itemsets, rules = apriori(


mapped_data,
min_support=supp,
min_confidence=conf
)

# Frequent itemsets
print("\nFrequent Itemsets:")
if not itemsets:
print(" None found.")
else:
for size, group in [Link]():
for items, support in [Link]():
readable = [id_map[i] for i in items]
print(" ", readable, "| support =", round(support, 4))

# Rules
print("\nAssociation Rules:")
if not rules:
print(" None found.")
else:
for r in rules:
lhs = [id_map[i] for i in [Link]]
rhs = [id_map[i] for i in [Link]]
print(" ", lhs, "->", rhs,
"| support =", round([Link], 4),
"| confidence =", round([Link], 4),
"| lift =", round([Link], 4))

In [6]: run_apriori("Groceries Dataset (Case A)",


mapped_g, g_id_to_item,
supp=0.50, conf=0.75)

run_apriori("Retail Dataset (Case A)",


mapped_r, r_id_to_item,
supp=0.50, conf=0.75)
======================================================================
Groceries Dataset (Case A) | min_support=0.5, min_confidence=0.75
======================================================================

Frequent Itemsets:
None found.

Association Rules:
None found.

======================================================================
Retail Dataset (Case A) | min_support=0.5, min_confidence=0.75
======================================================================

Frequent Itemsets:
None found.

Association Rules:
None found.

In [7]: run_apriori("Groceries Dataset (Case B)",


mapped_g, g_id_to_item,
supp=0.60, conf=0.60)

run_apriori("Retail Dataset (Case B)",


mapped_r, r_id_to_item,
supp=0.60, conf=0.60)

======================================================================
Groceries Dataset (Case B) | min_support=0.6, min_confidence=0.6
======================================================================

Frequent Itemsets:
None found.

Association Rules:
None found.

======================================================================
Retail Dataset (Case B) | min_support=0.6, min_confidence=0.6
======================================================================

Frequent Itemsets:
None found.

Association Rules:
None found.

You might also like