-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp13as.py
More file actions
28 lines (21 loc) · 804 Bytes
/
Copy pathp13as.py
File metadata and controls
28 lines (21 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# Load data
data = pd.read_excel('data.xlsx', header=None)
# Fill blank cells with "N/A"
data.fillna("N/A", inplace=True)
# Convert data to a list of transactions
transactions = data.values.tolist()
# Encoding data with one-hot encoding
te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df = pd.DataFrame(te_ary, columns=te.columns_)
# Building the model
frequent_itemsets = apriori(df, min_support=0.3, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)
# Displaying the results
print("Frequent Itemsets:")
print(frequent_itemsets)
print("\nAssociation Rules:")
print(rules)