1 前提条件
确保你已经安装了mlxtend
库,可以使用以下命令进行安装:
pip install mlxtend
2 实现代码1
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# 数据集
data = [
{'A13', 'C22', 'D13', 'D22', 'D32', 'F12', 'F13', 'R1'},
{'A32','A13','A33','D13', 'D22', 'D32', 'F12', 'F13', 'R1'},
{'A33','D11','A13',"A33",'F12','F11','R3'},
{'A33','D11','A13','F12','A13','F12','A22','F11','R3'},
{'B21','A51','F12','A13','F14','R1'},
{'A33', 'D11', 'A13', 'F13', 'F14', 'R3'},
{'A21', 'B21', 'F14', 'F15', 'R1'},
{'A11', 'B22', 'D14', 'A13', 'A33', 'A32', 'F14', 'F11', 'R3', 'R10'},
{'A51', 'A13', 'F12', 'F11', 'F13', 'A12', 'R1'},
{'A51', 'A13', 'A12', 'F12', 'R1'},
{'A32', 'A13', 'F12', 'F13', 'F14', 'R1'},
{'C12', 'D11', 'F12', 'F14', 'F15', 'R3'},
{'D11', 'C12', 'A33', 'A13', 'A31', 'F12', 'F14', 'F15', 'R3'},
{'B15', 'A11', 'A13', 'F12', 'F14', 'F15', 'R4'},
{'B13', 'E21', 'A13', 'F15', 'F14', 'R1'},
{'D11', 'B22', 'F15', 'F14', 'F12', 'R3'},
{'D11', 'A33', 'B14', 'A13', 'F13', 'F12', 'F14', 'R3'},
{'B22', 'A11', 'F15', 'F13', 'F12', 'F14', 'R4'},
{'A11', 'F15', 'F12', 'F14', 'F13', 'R2'},
{'B14', 'A32', 'F15', 'F12', 'F13', 'D22', 'R4'},
{'B15', 'D11', 'F15', 'F14', 'R2', 'R11'},
{'B22', 'D11', 'F14', 'F12', 'R4'},
{'A33', 'D11', 'F12', 'F13', 'R2'},
{'D11', 'F12', 'F13', 'R3'},
{'D11', 'C12', 'F12', 'F13', 'R3'},
{'B22', 'A11', 'F15', 'F12', 'F14', 'R4'},
{'A32', 'B22', 'F13', 'F12', 'F14', 'R3'},
{'A32', 'B21', 'F14', 'F12', 'F13', 'R1'},
{'D11', 'A51', 'F14', 'F15', 'R3'},
{'B21', 'F11', 'F13', 'F14', 'R1'},
{'B22', 'F15', 'R2'},
{'E13', 'F14', 'R1'},
{'E13', 'F14', 'R1'},
{'D11', 'C12', 'F14', 'R3'},
{'E13', 'F14', 'R1'},
{'E11', 'F12', 'F14', 'R8'},
{'D11', 'B14', 'F15', 'R3'},
{'B22', 'D11', 'F12', 'F14', 'R2'},
{'E11', 'A21', 'F12', 'F13', 'R8'},
{'B22', 'D11', 'F15', 'F14', 'R5'},
{'B21', 'A51', 'F14', 'F13', 'R1'},
{'D15', 'C11', 'F12', 'F14', 'R11'},
{'E11', 'A13', 'F12', 'F13', 'R8'},
{'D15', 'F14', 'F15', 'R5'},
{'D15', 'F15', 'F14', 'R3'},
{'D11', 'A51', 'F12', 'F14', 'R3'},
{'D11', 'A51', 'F15', 'F14', 'R3'},
{'B21', 'A51', 'F14', 'F13', 'R1'},
{'A51', 'F14', 'F12', 'R5'},
{'R5', 'A51', 'F14', 'F12', 'R2', 'R3'},
{'B21', 'A51', 'F14', 'F13', 'R1'},
{'A52', 'F12', 'F14', 'R10'},
{'D11', 'C12', 'F14', 'F12', 'F15', 'R3'},
{'B22', 'F12', 'F14', 'R2'},
{'A51', 'A11', 'F14', 'F12', 'F15', 'R4'},
{'A32', 'A11', 'F14', 'F12', 'F13', 'R3'},
{'A32', 'A11', 'F14', 'F15', 'F12', 'R2'},
{'A51', 'A11', 'F14', 'F12', 'R4'},
{'A32', 'A11', 'F14', 'F12', 'F13', 'R3'},
{'A33', 'A51', 'F14', 'F12', 'F13', 'R1'},
{'B21', 'D11', 'A51', 'F14', 'F12', 'F13', 'R1'},
{'D11', 'A32', 'A51', 'F14', 'F12', 'F13', 'R3'},
{'E13', 'A32', 'B21', 'F14', 'F12', 'F13', 'R1'},
{'A32', 'B21', 'F14', 'F12', 'F13', 'R1'},
{'A33', 'B21', 'F14', 'F13', 'R1'},
{'B21', 'D32', 'F14', 'F13', 'R1'},
{'B22', 'D11', 'F14', 'F12', 'R2'},
{'A32', 'B22', 'F14', 'F12', 'R1'},
{'D11', 'B21', 'F14', 'F12', 'R1'},
{'D11', 'D32', 'F14', 'F12', 'F15', 'R3'},
{'D11', 'A33', 'F12', 'F14', 'R3'},
{'D11', 'A33', 'F14', 'F12', 'F13', 'R3'},
{'E13', 'F14', 'R1'},
{'D15', 'F15', 'R12'},
{'D14', 'B22', 'D11', 'F12', 'F14', 'R2'},
{'B11', 'F14', 'F12', 'F13', 'R1'},
{'B14', 'D11', 'F15', 'F14', 'R3'},
{'B14', 'B13', 'F14', 'F15', 'R6'},
{'D11', 'C12', 'F14', 'F13', 'R3'},
{'B21', 'A51', 'F14', 'F13', 'R1'},
{'D11', 'C12', 'F14', 'F13', 'R3'},
{'D11', 'A33', 'F14', 'F13', 'R3'},
{'A51', 'F12', 'F14', 'R5'},
{'B23', 'E21', 'F15', 'R5'},
{'A51', 'B14', 'B22', 'F15', 'R4'},
{'F15', 'B23', 'F13', 'F12', 'R6'},
{'A51', 'B14', 'F15', 'R4'},
{'D11', 'C12', 'F14', 'R3'},
{'D11', 'C12', 'F12', 'R3'},
{'A51', 'F14', 'F12', 'R4'},
{'A51', 'A32', 'B11', 'F14', 'F12', 'R12'},
{'A52', 'A32', 'C21', 'A51', 'F14', 'F12', 'R1'},
{'A51', 'A52', 'B21', 'F14', 'R1'},
{'A32', 'A51', 'F14', 'F12', 'R1'},
{'A33', 'A51', 'B21', 'F14', 'F12', 'F13', 'R1'},
{'A51', 'B21', 'A52', 'A33', 'F14', 'R1'},
{'A52', 'D11', 'B24', 'F14', 'R1'},
{'B25', 'B24', 'A51', 'A14', 'F15', 'F14', 'R1'},
{'A13', 'A51', 'A33', 'B21', 'A11', 'F14', 'R1'},
{'B11', 'B12', 'F13', 'F14', 'F15', 'R1'},
{'B24', 'F14', 'R1'},
{'A12', 'A13', 'A51', 'B25', 'F11', 'F12', 'D43', 'R1', 'R7'},
{'A51', 'A52', 'B11', 'B25', 'F14', 'F12', 'F13', 'R1', 'R3'},
{'A51', 'B11', 'F14', 'F13', 'F12', 'C24', 'R1'},
{'B11', 'A51', 'F14', 'D43', 'F13', 'F12', 'R1'},
{'B11', 'B21', 'D11', 'C22', 'C12', 'F13', 'F12', 'F14', 'F15', 'R1'},
{'B11', 'B21', 'C22', 'D11', 'F14', 'F12', 'F13', 'F11', 'F15', 'R1'},
{'A11', 'B14', 'B23', 'A33', 'C25', 'F12', 'F14', 'F13', 'R3'},
{'B21', 'B24', 'F12', 'A51', 'F14', 'A13', 'R1'},
{'B21', 'B24', 'A51', 'F14', 'A13', 'R1'},
{'B21', 'B24', 'A51', 'F14', 'A13', 'R1'},
{'B21', 'B24', 'A11', 'F14', 'A51', 'R1'},
{'B21', 'B24', 'F12', 'F14', 'R1'},
{'B21', 'B24', 'F12', 'F14', 'R1'},
{'B21', 'B23', 'B24', 'F12', 'F14', 'R1'},
{'B21', 'B23', 'B24', 'F12', 'F14', 'R1'},
{'B21', 'B23', 'B24', 'F12', 'F14', 'R1'},
{'B21', 'A51', 'F14', 'R1'},
{'B24', 'F14', 'R1'},
{'B24', 'A51', 'B25', 'F14', 'R1'},
{'B21', 'A51', 'A13', 'F14', 'R1'},
{'A33', 'A51', 'B11', 'A13', 'F14', 'R1'},
{'B21', 'B11', 'A33', 'A51', 'A13', 'F14', 'R1'},
{'A33', 'A51', 'B11', 'B21', 'C11', 'C13', 'C22', 'C25', 'D12', 'F14', 'F15', 'R1'},
{'A51', 'B21', 'C22', 'A13', 'F14', 'R1'},
{'A51', 'B21', 'C22', 'A13', 'F14', 'R1'},
{'B11', 'B15', 'B21', 'A13', 'F14', 'F15', 'R1'},
{'B21', 'C25', 'A13', 'A51', 'F14', 'R1'},
{'A51', 'B21', 'C25', 'A13', 'F13', 'R1', 'R3'},
{'B21', 'C25', 'A13', 'A51', 'F12', 'R1'},
{"A14", "B21", "A33", "A52", "F14", "R1"},
{"A51", "B21", "D32", "E15", "A13", "A14", "A33", "B24", "F14", "R1"},
{"A51", "B21", "B24", "C22", "A13", "A14", "A33", "F12", "F14", "R1"},
{"A51", "B21", "C22", "A33", "A13", "F14", "F12", "R1"},
{"A51", "C22", "C13", "B21", "A13", "A14", "F12", "F14", "R1"},
{"C22", "A52", "F14", "A13", "F12", "F14", "R1"},
{"B21", "B11", "F14", "F12", "D15", "R1"},
{"B21", "B24", "A13", "F12", "R1"},
{"B21", "A51", "A13", "F12", "R1"},
{"B21", "C25", "A13", "F12", "R1"},
{"E15", "B21", "F14", "F11", "R3", "R12"},
{"E15", "C25", "D41", "F12", "F14", "R3"},
{"E15", "A51", "F14", "F12", "R1"},
{"A51", "B24", "F12", "F14", "R1"},
{"A51", "B24", "C12", "A13", "B25", "R1", "R3"},
{"A51", "B24", "A13", "C33", "R1"},
{"A32", "B14", "B23", "A11", "A13", "F12", "R3", "R5"},
{"B22", "B23", "A13", "A32", "F12", "R3"},
{"C24", "B22", "A11", "A13", "F14", "R2"},
{"C24", "B23", "A13", "F15", "R3", "R2"},
{"C25", "D32", "F15", "F12", "F13", "R2"},
{"A13", "A51", "C24", "C25", "F14", "F12", "F13", "R3"},
{"A32", "A33", "A51", "F12", "F13", "F14", "R1"},
{"A11", "A32", "A13", "B21", "B24", "F12", "F13", "F14", "F15", "R1"},
{"A11", "A13", "A32", "B21", "B24", "F12", "F13", "R1"},
{"A11", "A13", "B21", "B24", "F12", "F14", "R1"},
{"A11", "A32", "B24", "F12", "F14", "R1"},
{"A11", "A32", "A33", "F12", "F14", "R1"},
{"A32", "B21", "F12", "F13", "F14", "R1"},
{"A32", "B21", "F12", "F14", "R1"},
{"A32", "B21", "A13", "F12", "R1"},
{"A32", "B21", "A13", "F12", "R1"},
{"D11", "A33", "A13", "F12", "R3"},
{"B14", "A33", "A13", "F15", "R3"},
{"D11", "B25", "C12", "A13", "F15", "R3"},
{"B21", "B11", "C13", "A13", "F14", "R1"},
{"B21", "B11", "C13", "A13", "F15", "R1"},
{"B15", "C11", "A13", "F15", "E14", "R2", "R3"},
{"A51", "B24", "A13", "F15", "E13", "R1"},
{"A51", "B15", "B24", "F13", "F14", "R1"},
{"A33", "D11", "A13", "F12", "R3"},
{"A33", "D11", "A13", "F12", "R3"},
{"A33", "D11", "A13", "F12", "R3"},
{"B24", "A13", "F13", "R1"},
{"E23", "B21", "B24", "A13", "F14", "R1"},
{"A51", "B21", "C13", "F14", "F12", "R1"},
{"C24", "C25", "F14", "R2"},
{"C24", "F14", "R2"},
{"C24", "A51", "F12", "F14", "R2"},
{"A33", "A51", "B21", "A13", "F12", "F13", "R1"},
{"A51", "B21", "B24", "A13", "F12", "F14", "R1"},
{"A31", "A51", "B21", "A13", "F12", "F14", "R1"},
{"A51", "A33", "B21", "A13", "F12", "F14", "R1"},
{"A51", "B24", "A12", "A13", "F11", "F12", "R1"},
{"A51", "B15", "A13", "A12", "F15", "R1"},
{"A51", "A13", "R8"},
{"A51", "A13", "F12", "F14", "R3"},
{"B14", "B25", "F12", "F14", "R5"},
{"A11", "A13", "A33", "B21", "B24", "C25", "F12", "F14", "R1"},
{"A11", "A51", "A13", "F12", "F14", "R3"},
{"A51", "A33", "B24", "A13", "F12", "F14", "R4"},
{"A11", "A13", "B21", "E15", "F13", "F12", "F14", "R4"},
{"A51", "B24", "B21", "F12", "F14", "F15", "R1"},
{"B24", "E15", "A51", "F12", "F14", "F15", "R3"},
{"A13", "A52", "A33", "B21", "F14", "F11", "F12", "F13", "R1"},
{"A31", "A32", "B25", "C24", "A13", "F11", "F12", "F14", "R2"},
{"A11", "F12", "F14", "A52", "R12"},
{"A32", "B23", "C11", "F12", "F13", "R3"},
{"E13", "B24", "F12", "F13", "R1"},
{"D13", "B21", "A12", "F11", "F12", "F14", "R1"},
{"D11", "B23", "F12", "F15", "R3"},
{"A11", "A12", "A33", "A51", "B12", "F13", "F15", "R3", "R12"},
{"B21", "A12", "F11", "F12", "F14", "R1"},
{"A11", "A13", "A33", "B21", "B24", "F12", "F14", "R1"},
{"A11", "A12", "A51", "B14", "F12", "F14", "R2"},
{"D11", "A11", "A13", "A33", "A51", "F14", "F15", "R3"},
{"B13", "B15", "B21", "E22", "F15", "R12"},
{"B14", "D11", "F15", "F14", "R3"},
{"B22", "D11", "F12", "F14", "R2"},
{"B14", "D11", "F15", "F14", "R3"},
{"B22", "D14", "F12", "F14", "R2"},
{"B22", "D11", "F12", "F14", "R2"},
{"B22", "D11", "F15", "F14", "R2"},
{"E23", "F15", "F14", "R5"},
{"B14", "D11", "F15", "F14", "F12", "R5"},
{"B21", "B11", "F14", "C13", "F13", "R1"},
{"B22", "B14", "A51", "F15", "F14", "R4"},
{"B22", "F12", "F15", "F14", "R2"},
{"E11", "B14", "F13", "F15", "F12", "R5"},
{"A12", "F12", "B14", "F13", "F15", "F14", "R3"},
{"C21", "E15", "F13", "F14", "R4"},
{"D11", "A33", "F13", "F12", "R3"},
{"D11", "C12", "F13", "F12", "B25", "R3"},
{"D11", "B14", "F13", "F12", "F15", "R3"},
{"B21", "A33", "F13", "F12", "F14", "R1"},
{"B21", "A51", "F13", "F12", "F14", "R1"},
{"D11", "C25", "F13", "F12", "F15", "R3"},
{"B22", "B23", "B25", "D11", "A51", "F14", "F15", "R3"},
{"B21", "E13", "A51", "F14", "F12", "R1"},
{"B22", "B23", "D14", "A51", "F15", "F14", "R3"},
{"D11", "C12", "A51", "F14", "F12", "F15", "R3"},
{"B21", "D11", "A51", "F14", "F12", "R1"},
{"B22", "B23", "A51", "F14", "F15", "R5"},
{"B14", "B22", "A51", "F14", "F15", "R4"},
{"B14", "B22", "A51", "F14", "R3"},
{"B21", "E13", "A51", "F14", "F12", "R1"},
{"D11", "B22", "A51", "F14", "F12", "R3"},
{"A21", "A14", "F14", "F12", "F13", "R3"},
{"B22", "B23", "F15", "F14", "F12", "R5"},
{"B21", "D11", "A51", "F14", "F12", "R1", "R3"},
{"B23", "F15", "F14", "F12", "R5"},
{"B21", "E13", "A51", "F14", "F12", "R1"},
{"B21", "E13", "A51", "F14", "F12", "A33", "R1"},
{"D11", "C12", "A51", "F14", "F12", "F13", "R3"},
{"B22", "D11", "B14", "A51", "F12", "R3"},
{"B21", "E13", "A51", "F14", "F12", "A33", "R1"},
{"D11", "C12", "A51", "F12", "F14", "R3"},
{"D11", "B14", "A51", "F12", "F14", "C12", "R3"},
{"B22", "B14", "A51", "F12", "F14", "C24", "R4"},
{"D11", "C25", "B22", "F12", "F14", "F13", "R3"},
{"B22", "B14", "F12", "F15", "F14", "R5"},
{"B21", "B25", "C11", "A13", "A14", "F15", "F12", "R1"},
{"B22", "B23", "C11", "F14", "A11", "A13", "F15", "F12", "R5"},
{"B22", "B23", "E15", "C11", "F14", "F12", "D32", "R3"},
{"D11", "E15", "C12", "B25", "A11", "A13", "F12", "F13", "R3"},
{"E11", "E15", "F14", "F15", "A11", "A13", "A14", "F12", "R8"},
{"B22", "B14", "C24", "E21", "A11", "A13", "F15", "F13", "R2"},
{"B14", "B15", "F14", "F15", "A13", "F12", "F13", "R11"},
{"B22", "B14", "D11", "F14", "F12", "A13", "F15", "A11", "A14", "R5"},
{"D11", "B23", "C21", "F14", "A13", "F12", "F13", "A11", "R3"},
{"D11", "B23", "C21", "F15", "A13", "F12", "F13", "A11", "R3"},
{"B23", "C21", "D11", "A13", "F13", "F12", "R3"},
{"A33", "D11", "B23", "C12", "A13", "F12", "F13", "F14", "R3"},
{"A33", "D11", "B23", "C12", "A13", "A51", "F14", "R3"},
{"B11", "B22", "B23", "B15", "A11", "F14", "F15", "R3"},
{"B23", "E15", "C21", "A13", "F14", "F12", "R3"},
{"B14", "B11", "B22", "B25", "F14", "F15", "R2"},
{"B14", "B11", "B22", "B25", "A33", "F14", "F15", "F12", "C21", "R2"},
{"B15", "B14", "B25", "D14", "F15", "F14", "F12", "R3", "R4"},
{"A51", "A52", "B15", "B14", "F14", "F12", "F15", "R4"},
{"A51", "B15", "A32", "F12", "F13", "F14", "R4"},
{"B21", "B15", "A51", "A13", "B11", "F12", "F13", "F15", "R12"},
{"A32", "B21", "C14", "C21", "A52", "F12", "F14", "R12"},
{'A33', 'B22', 'C11', 'D14', 'F12', 'F13', 'R2'}
]
# 创建DataFrame
df = pd.DataFrame(data)
# 使用TransactionEncoder将数据转换为One-Hot编码格式
te = TransactionEncoder()
te_ary = te.fit(data).transform(data)
df_encoded = pd.DataFrame(te_ary, columns=te.columns_)
# 使用Apriori算法生成频繁项集
frequent_itemsets = apriori(df_encoded, min_support=0.001, use_colnames=True)
# 生成关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
# 只保留有意义的规则(例如,置信度大于0.5的规则)
rules = rules[rules['confidence'] > 0.2]
# 打印频繁项集和关联规则
print("频繁项集:")
print(frequent_itemsets)
print("\n关联规则:")
print(rules)
# 过滤事故后果相关的规则并打印
for consequence in ['R8', 'R9', 'R10', 'R11', 'R12']:
#for consequence in ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12']:
print(f"\n关联到 {consequence} 的规则:")
filtered_rules = rules[rules['consequents'].apply(lambda x: consequence in x)]
print(filtered_rules)
3 实现代码2
items
"A13,C22,D13,D22,D32,F12,F13,R1"
"A32,A13,A33,D13,D22,D32,F12,F13,R1"
"A33,D11,A13,A33,F12,F11,R3"
"A33,D11,A13,F12,A13,F12,A22,F11,R3"
"B21,A51,F12,A13,F14,R1"
"A33,D11,A13,F13,F14,R3"
"A21,B21,F14,F15,R1"
"A11,B22,D14,A13,A33,A32,F14,F11,R3,R10"
"A51,A13,F12,F11,F13,A12,R1"
"A51,A13,A12,F12,R1"
"A32,A13,F12,F13,F14,R1"
"C12,D11,F12,F14,F15,R3"
"D11,C12,A33,A13,A31,F12,F14,F15,R3"
"B15,A11,A13,F12,F14,F15,R4"
"B13,E21,A13,F15,F14,R1"
"D11,B22,F15,F14,F12,R3"
"D11,A33,B14,A13,F13,F12,F14,R3"
"B22,A11,F15,F13,F12,F14,R4"
"A11,F15,F12,F14,F13,R2"
"B14,A32,F15,F12,F13,D22,R4"
"B15,D11,F15,F14,R2,R11"
"B22,D11,F14,F12,R4"
"A33,D11,F12,F13,R2"
"D11,F12,F13,R3"
"D11,C12,F12,F13,R3"
"B22,A11,F15,F12,F14,R4"
"A32,B22,F13,F12,F14,R3"
"A32,B21,F14,F12,F13,R1"
"D11,A51,F14,F15,R3"
"B21,F11,F13,F14,R1"
"B22,F15,R2"
"E13,F14,R1"
"E13,F14,R1"
"D11,C12,F14,R3"
"E13,F14,R1"
"E11,F12,F14,R8"
"D11,B14,F15,R3"
"B22,D11,F12,F14,R2"
"E11,A21,F12,F13,R8"
"B22,D11,F15,F14,R5"
"B21,A51,F14,F13,R1"
"D15,C11,F12,F14,R11"
"E11,A13,F12,F13,R8"
"D15,F14,F15,R5"
"D15,F15,F14,R3"
"D11,A51,F12,F14,R3"
"D11,A51,F15,F14,R3"
"B21,A51,F14,F13,R1"
"A51,F14,F12,R5"
"R5,A51,F14,F12,R2,R3"
"B21,A51,F14,F13,R1"
"A52,F12,F14,R10"
"D11,C12,F14,F12,F15,R3"
"B22,F12,F14,R2"
"A51,A11,F14,F12,F15,R4"
"A32,A11,F14,F12,F13,R3"
"A32,A11,F14,F15,F12,R2"
"A51,A11,F14,F12,R4"
"A32,A11,F14,F12,F13,R3"
"A33,A51,F14,F12,F13,R1"
"B21,D11,A51,F14,F12,F13,R1"
"D11,A32,A51,F14,F12,F13,R3"
"E13,A32,B21,F14,F12,F13,R1"
"A32,B21,F14,F12,F13,R1"
"A33,B21,F14,F13,R1"
"B21,D32,F14,F13,R1"
"B22,D11,F14,F12,R2"
"A32,B22,F14,F12,R1"
"D11,B21,F14,F12,R1"
"D11,D32,F14,F12,F15,R3"
"D11,A33,F12,F14,R3"
"D11,A33,F14,F12,F13,R3"
"E13,F14,R1"
"D15,F15,R12"
"D14,B22,D11,F12,F14,R2"
"B11,F14,F12,F13,R1"
"B14,D11,F15,F14,R3"
"B14,B13,F14,F15,R6"
"D11,C12,F14,F13,R3"
"B21,A51,F14,F13,R1"
"D11,C12,F14,F13,R3"
"D11,A33,F14,F13,R3"
"A51,F12,F14,R5"
"B23,E21,F15,R5"
"A51,B14,B22,F15,R4"
"F15,B23,F13,F12,R6"
"A51,B14,F15,R4"
"D11,C12,F14,R3"
"D11,C12,F12,R3"
"A51,F14,F12,R4"
"A51,A32,B11,F14,F12,R12"
"A52,A32,C21,A51,F14,F12,R1"
"A51,A52,B21,F14,R1"
"A32,A51,F14,F12,R1"
"A33,A51,B21,F14,F12,F13,R1"
"A51,B21,A52,A33,F14,R1"
"A52,D11,B24,F14,R1"
"B25,B24,A51,A14,F15,F14,R1"
"A13,A51,A33,B21,A11,F14,R1"
"B11,B12,F13,F14,F15,R1"
"B24,F14,R1"
"A12,A13,A51,B25,F11,F12,D43,R1,R7"
"A51,A52,B11,B25,F14,F12,F13,R1,R3"
"A51,B11,F14,F13,F12,C24,R1"
"B11,A51,F14,D43,F13,F12,R1"
"B11,B21,D11,C22,C12,F13,F12,F14,F15,R1"
"B11,B21,C22,D11,F14,F12,F13,F11,F15,R1"
"A11,B14,B23,A33,C25,F12,F14,F13,R3"
"B21,B24,F12,A51,F14,A13,R1"
"B21,B24,A51,F14,A13,R1"
"B21,B24,A51,F14,A13,R1"
"B21,B24,A11,F14,A51,R1"
"B21,B24,F12,F14,R1"
"B21,B24,F12,F14,R1"
"B21,B23,B24,F12,F14,R1"
"B21,B23,B24,F12,F14,R1"
"B21,B23,B24,F12,F14,R1"
"B21,A51,F14,R1"
"B24,F14,R1"
"B24,A51,B25,F14,R1"
"B21,A51,A13,F14,R1"
"A33,A51,B11,A13,F14,R1"
"B21,B11,A33,A51,A13,F14,R1"
"A33,A51,B11,B21,C11,C13,C22,C25,D12,F14,F15,R1"
"A51,B21,C22,A13,F14,R1"
"A51,B21,C22,A13,F14,R1"
"B11,B15,B21,A13,F14,F15,R1"
"B21,C25,A13,A51,F14,R1"
"A51,B21,C25,A13,F13,R1,R3"
"B21,C25,A13,A51,F12,R1"
"A14,B21,A33,A52,F14,R1"
"A51,B21,D32,E15,A13,A14,A33,B24,F14,R1"
"A51,B21,B24,C22,A13,A14,A33,F12,F14,R1"
"A51,B21,C22,A33,A13,F14,F12,R1"
"A51,C22,C13,B21,A13,A14,F12,F14,R1"
"C22,A52,F14,A13,F12,F14,R1"
"B21,B11,F14,F12,D15,R1"
"B21,B24,A13,F12,R1"
"B21,A51,A13,F12,R1"
"B21,C25,A13,F12,R1"
"E15,B21,F14,F11,R3,R12"
"E15,C25,D41,F12,F14,R3"
"E15,A51,F14,F12,R1"
"A51,B24,F12,F14,R1"
"A51,B24,C12,A13,B25,R1,R3"
"A51,B24,A13,C33,R1"
"A32,B14,B23,A11,A13,F12,R3,R5"
"B22,B23,A13,A32,F12,R3"
"C24,B22,A11,A13,F14,R2"
"C24,B23,A13,F15,R3,R2"
"C25,D32,F15,F12,F13,R2"
"A13,A51,C24,C25,F14,F12,F13,R3"
"A32,A33,A51,F12,F13,F14,R1"
"A11,A32,A13,B21,B24,F12,F13,F14,F15,R1"
"A11,A13,A32,B21,B24,F12,F13,R1"
"A11,A13,B21,B24,F12,F14,R1"
"A11,A32,B24,F12,F14,R1"
"A11,A32,A33,F12,F14,R1"
"A32,B21,F12,F13,F14,R1"
"A32,B21,F12,F14,R1"
"A32,B21,A13,F12,R1"
"A32,B21,A13,F12,R1"
"D11,A33,A13,F12,R3"
"B14,A33,A13,F15,R3"
"D11,B25,C12,A13,F15,R3"
"B21,B11,C13,A13,F14,R1"
"B21,B11,C13,A13,F15,R1"
"B15,C11,A13,F15,E14,R2,R3"
"A51,B24,A13,F15,E13,R1"
"A51,B15,B24,F13,F14,R1"
"A33,D11,A13,F12,R3"
"A33,D11,A13,F12,R3"
"A33,D11,A13,F12,R3"
"B24,A13,F13,R1"
"E23,B21,B24,A13,F14,R1"
"A51,B21,C13,F14,F12,R1"
"C24,C25,F14,R2"
"C24,F14,R2"
"C24,A51,F12,F14,R2"
"A33,A51,B21,A13,F12,F13,R1"
"A51,B21,B24,A13,F12,F14,R1"
"A31,A51,B21,A13,F12,F14,R1"
"A51,A33,B21,A13,F12,F14,R1"
"A51,B24,A12,A13,F11,F12,R1"
"A51,B15,A13,A12,F15,R1"
"A51,A13,R8"
"A51,A13,F12,F14,R3"
"B14,B25,F12,F14,R5"
"A11,A13,A33,B21,B24,C25,F12,F14,R1"
"A11,A51,A13,F12,F14,R3"
"A51,A33,B24,A13,F12,F14,R4"
"A11,A13,B21,E15,F13,F12,F14,R4"
"A51,B24,B21,F12,F14,F15,R1"
"B24,E15,A51,F12,F14,F15,R3"
"A13,A52,A33,B21,F14,F11,F12,F13,R1"
"A31,A32,B25,C24,A13,F11,F12,F14,R2"
"A11,F12,F14,A52,R12"
"A32,B23,C11,F12,F13,R3"
"E13,B24,F12,F13,R1"
"D13,B21,A12,F11,F12,F14,R1"
"D11,B23,F12,F15,R3"
"A11,A12,A33,A51,B12,F13,F15,R3,R12"
"B21,A12,F11,F12,F14,R1"
"A11,A13,A33,B21,B24,F12,F14,R1"
"A11,A12,A51,B14,F12,F14,R2"
"D11,A11,A13,A33,A51,F14,F15,R3"
"B13,B15,B21,E22,F15,R12"
"B14,D11,F15,F14,R3"
"B22,D11,F12,F14,R2"
"B14,D11,F15,F14,R3"
"B22,D14,F12,F14,R2"
"B22,D11,F12,F14,R2"
"B22,D11,F15,F14,R2"
"E23,F15,F14,R5"
"B14,D11,F15,F14,F12,R5"
"B21,B11,F14,C13,F13,R1"
"B22,B14,A51,F15,F14,R4"
"B22,F12,F15,F14,R2"
"E11,B14,F13,F15,F12,R5"
"A12,F12,B14,F13,F15,F14,R3"
"C21,E15,F13,F14,R4"
"D11,A33,F13,F12,R3"
"D11,C12,F13,F12,B25,R3"
"D11,B14,F13,F12,F15,R3"
"B21,A33,F13,F12,F14,R1"
"B21,A51,F13,F12,F14,R1"
"D11,C25,F13,F12,F15,R3"
"B22,B23,B25,D11,A51,F14,F15,R3"
"B21,E13,A51,F14,F12,R1"
"B22,B23,D14,A51,F15,F14,R3"
"D11,C12,A51,F14,F12,F15,R3"
"B21,D11,A51,F14,F12,R1"
"B22,B23,A51,F14,F15,R5"
"B14,B22,A51,F14,F15,R4"
"B14,B22,A51,F14,R3"
"B21,E13,A51,F14,F12,R1"
"D11,B22,A51,F14,F12,R3"
"A21,A14,F14,F12,F13,R3"
"B22,B23,F15,F14,F12,R5"
"B21,D11,A51,F14,F12,R1,R3"
"B23,F15,F14,F12,R5"
"B21,E13,A51,F14,F12,R1"
"B21,E13,A51,F14,F12,A33,R1"
"D11,C12,A51,F14,F12,F13,R3"
"B22,D11,B14,A51,F12,R3"
"B21,E13,A51,F14,F12,A33,R1"
"D11,C12,A51,F12,F14,R3"
"D11,B14,A51,F12,F14,C12,R3"
"B22,B14,A51,F12,F14,C24,R4"
"D11,C25,B22,F12,F14,F13,R3"
"B22,B14,F12,F15,F14,R5"
"B21,B25,C11,A13,A14,F15,F12,R1"
"B22,B23,C11,F14,A11,A13,F15,F12,R5"
"B22,B23,E15,C11,F14,F12,D32,R3"
"D11,E15,C12,B25,A11,A13,F12,F13,R3"
"E11,E15,F14,F15,A11,A13,A14,F12,R8"
"B22,B14,C24,E21,A11,A13,F15,F13,R2"
"B14,B15,F14,F15,A13,F12,F13,R11"
"B22,B14,D11,F14,F12,A13,F15,A11,A14,R5"
"D11,B23,C21,F14,A13,F12,F13,A11,R3"
"D11,B23,C21,F15,A13,F12,F13,A11,R3"
"B23,C21,D11,A13,F13,F12,R3"
"A33,D11,B23,C12,A13,F12,F13,F14,R3"
"A33,D11,B23,C12,A13,A51,F14,R3"
"B11,B22,B23,B15,A11,F14,F15,R3"
"B23,E15,C21,A13,F14,F12,R3"
"B14,B11,B22,B25,F14,F15,R2"
"B14,B11,B22,B25,A33,F14,F15,F12,C21,R2"
"B15,B14,B25,D14,F15,F14,F12,R3,R4"
"A51,A52,B15,B14,F14,F12,F15,R4"
"A51,B15,A32,F12,F13,F14,R4"
"B21,B15,A51,A13,B11,F12,F13,F15,R12"
"A32,B21,C14,C21,A52,F12,F14,R12"
"A33,B22,C11,D14,F12,F13,R2"
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# 从CSV文件加载数据
df = pd.read_csv('transactions.csv')
# 将数据转换为列表格式
transactions = df['items'].apply(lambda x: x.split(','))
# 使用TransactionEncoder将数据转换为One-Hot编码格式
te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df_encoded = pd.DataFrame(te_ary, columns=te.columns_)
# 使用Apriori算法生成频繁项集
frequent_itemsets = apriori(df_encoded, min_support=0.004, use_colnames=True)
# 生成关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
# 只保留有意义的规则(例如,置信度大于0.2的规则)
rules = rules[rules['confidence'] > 0.2]
# 定义目标后果标签
consequences = ['R1', 'R2', 'R3', 'R4', 'R5','R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12']
consequences1 = ['R8', 'R9', 'R10', 'R11', 'R12']
# 筛选包含目标后果标签的频繁项集
print("与 R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12 相关的频繁项集:")
for consequence in consequences1:
frequent_itemsets_with_consequence = frequent_itemsets[frequent_itemsets['itemsets'].apply(lambda x: consequence in x)]
print(f"\n包含 {consequence} 的频繁项集:")
print(frequent_itemsets_with_consequence)
# 筛选关联到目标后果标签的规则
print("\n与 R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12 相关的关联规则:")
for consequence in consequences1:
print(f"\n关联到 {consequence} 的规则:")
filtered_rules = rules[rules['consequents'].apply(lambda x: consequence in x)]
print(filtered_rules)