import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori,association_rules
df = pd.read_csv("C:/Users/wcj770801/Desktop/Python/Mushroom.txt",header=None,sep =' ')
ind = (df.iloc[:,0]==2)
b = df.loc[ind]
b= b.iloc[:,:-1].values.tolist()
te_ary = te.fit(b).transform(b)
df = pd.DataFrame(te_ary, columns=te.columns_)
fre_item = apriori(df, min_support=0.6)
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori,association_rules
te = TransactionEncoder()
df = pd.read_excel("C:/Users/wcj770801/Desktop/Python/cancer.xls")
df["肝气郁结证型系数"] = pd.cut(df["肝气郁结证型系数"], [0,0.179,0.258,0.35,0.504], labels=["A1", "A2", "A3", "A4"],include_lowest=True)
df["热毒蕴结证型系数"] = pd.cut(df ["热毒蕴结证型系数"], [0,0.15,0.296,0.485,0.78], labels=["B1", "B2", "B3", "B4"],include_lowest=True)
df["冲任失调证型系数"] = pd.cut(df["冲任失调证型系数"], [0,0.201,0.288,0.415,0.61], labels=["C1", "C2", "C3", "C4"],include_lowest=True)
df["气血两虚证型系数"] = pd.cut( df["气血两虚证型系数"], [0,0.172,0.251,0.357,0.552], labels=["D1", "D2", "D3", "D4"],include_lowest=True)
df["脾胃虚弱证型系数"] = pd.cut( df["脾胃虚弱证型系数"], [0,0.154,0.256,0.375,0.526], labels=["E1", "E2", "E3", "E4"],include_lowest=True)
df["肝肾阴虚证型系数"] = pd.cut( df["肝肾阴虚证型系数"], [0,0.178,0.261,0.353,0.607], labels=["F1", "F2", "F3", "F4"],include_lowest=True)
b = df.values.tolist()
te_ary = te.fit(b).transform(b)
df = pd.DataFrame(te_ary, columns=te.columns_)
fre_itemset = apriori(df, min_support=0.06,use_colnames=True)
rules =association_rules(fre_itemset, metric='confidence', min_threshold=1, support_only=False)
consequents = rules['consequents'] #frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。
idx = consequents.apply(lambda x: 'H4' in set(x))
aim_rules = rules[idx]
print(aim_rules)