import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori,association_rules
关联规则
model=TransactionEncoder()###建立模型
data=pd.read_csv('C:\\Users\\Lenovo\\Desktop\\Dataset\\Mushroom.txt',header=None,sep=' ')###读取文件
data1=(data.iloc[:,0]==2)###因为要提取毒蘑菇数据,毒蘑菇数据为首列中值为2的数据,将首列中值为2的赋值为true,否则为false
data2 = data.loc[data1]###data1中为true的数据提取出来
data2=data2.iloc[:,:-1].values.tolist()###将data2中的数据变成列表,以便使用模型对数据进行处理
data3 = model.fit(data2).transform(data2)###利用模型对数据进行处理
data4=pd.DataFrame(data3,columns=model.columns_)###将处理过的数据变为df模式
fre_item = apriori(data4, min_support=0.6) ###利用apriori算法生成频繁项集
te=TransactionEncoder()
df = pd.read_excel('C:\\Users\\Lenovo\\Desktop\\Dataset\\cancer.xls')
###include_lowest=True参数表示包含边界最小值包含数据的最小值
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["脾胃虚弱证型系数&