Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
777 B
3
Indexable
Never
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import pandas as pd

# Sample dataset
data = {'TransactionID': [1, 2, 3, 4, 5],
        'Items': [['A', 'B', 'D'],
                  ['B', 'C'],
                  ['A', 'B', 'E'],
                  ['B', 'E'],
                  ['A', 'C']]
      }
df = pd.DataFrame(data)

# Convert the items column to a one-hot encoded format
oht = pd.Series(df['Items']).str.join('|').str.get_dummies()
frequent_itemsets = apriori(oht, min_support=0.2, use_colnames=True)

# Generate the association rules
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)

print("Frequent itemsets:")
print(frequent_itemsets)

print("\nAssociation rules:")
print(rules)
Leave a Comment