赞
踩
from mlxtend.frequent_patterns import apriori,association_rules from mlxtend.preprocessing import TransactionEncoder import pandas as pd #一次购物行为(事物) dataset=[['Milk','Onion','Nutmeg','Kidney Beans','Eggs','Yogurt'], ['Dill','Onion','Nutmeg','Kidney Beans','Eggs','Yogurt'], ['Milk','Apple','Kidney Beans','Egg'], ['Milk','Unicorn','Corn','Kidney Beans','Yogurt'], ['Corn','Onion','Onion','Kidney Beans','Icy cream','Egg']] transEn = TransactionEncoder() oht_ary=transEn.fit(dataset).transform(dataset) df=pd.DataFrame(oht_ary,columns=transEn.columns_) print(df) frequent_itemset=apriori(df,min_support=0.1,use_colnames=True) print(frequent_itemset) rules=association_rules(frequent_itemset,metric='lift',min_threshold=1.2) print(rules.head(10))
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。