Untitled

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


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

oht = pd.Series(df['Items']).str.join('|').str.get_dummies()
frequent_itemsets = apriori(oht, min_support=0.5, use_colnames=True)

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


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

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