apriori algorithm python update
from mlxtend.frequent_patterns import apriori, association_rules
from mlxtend.preprocessing import TransactionEncoder
import pandas as pd
dataset = [
['I1', 'I2', 'I3', 'I4'],
['I1', 'I2', 'I3'],
['I1', 'I2'],
['I1', 'I4'],
['I2', 'I4']
]
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
print("Transaction Data:")
print(df)
frequent_itemsets = apriori(df, min_support=0.4, use_colnames=True)
print("\nFrequent Itemsets:")
print(frequent_itemsets)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)
print("\nAssociation Rules:")
print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])
Output
Transaction Data:
I1 I2 I3 I4
0 True True True True
1 True True True False
2 True True False False
3 True False False True
4 False True False True
Frequent Itemsets:
support itemsets
0 0.8 (I1)
1 0.8 (I2)
2 0.4 (I3)
3 0.6 (I4)
4 0.6 (I1, I2)
5 0.4 (I1, I3)
6 0.4 (I1, I4)
7 0.4 (I2, I3)
8 0.4 (I2, I4)
Association Rules:
antecedents consequents support confidence lift
0 (I1) (I2) 0.6 0.750000 0.937500
1 (I2) (I1) 0.6 0.750000 0.937500
2 (I4) (I1) 0.4 0.666667 0.833333
3 (I4) (I2) 0.4 0.666667 0.833333
4 (I3) (I1) 0.4 1.000000 1.250000
5 (I3) (I2) 0.4 1.000000 1.250000
Comments
Post a Comment