我正在尝试使用apyori模块运行关联规则。我的“项目”是各种手术(行=病人的情况),如您在下面的数据框示例中所见。Apyori无法捕获正确的标签,并且似乎用字母将标签切碎了。我以前已经见过这种行为。除非我丢失了某些内容,否则我的数据集的格式已正确设置,可以用于脓毒症。任何时候进行的手术不得超过2次。
这是我得到的例子:
RelationRecord(items=frozenset({'v', '_'}), support=0.10309278350515463, ordered_statistics=[OrderedStatistic(items_base=frozenset(), items_add=frozenset({'v', '_'}), confidence=0.10309278350515463, lift=1.0), OrderedStatistic(items_base=frozenset({'_'}), items_add=frozenset({'v'}), confidence=0.10638297872340426, lift=1.0319148936170213), OrderedStatistic(items_base=frozenset({'v'}), items_add=frozenset({'_'}), confidence=1.0, lift=1.0319148936170213)]) Support: 0.10309278350515463 Confidence: frozenset({'v', '_'}) Lift:
0.10309278350515463
冻结集已损坏...这是我的输入dataframe.head():
sm-to-sm_bowel_anastom small_bowel_incision_nec sm_bowel_exteriorization \
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 0
incisional_hernia_repair colonoscopy anal_anastomosis \
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 0
c.a.t._scan_of_abdomen open_sigmoidectomy_nec small_bowel_suture_nec \
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 0
lap_pt_ex_lrg_intest_nec ... abdperneal_res_rectm_nos \
0 0 ... 0
1 0 ... 0
2 0 ... 0
3 0 ... 0
4 0 ... 0
5 0 ... 0
6 0 ... 0
7 0 ... 0
8 0 ... 0
9 0 ... 0
ureteral_catheterization cv_cath_plcmt_w_guidance \
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
clos_large_bowel_biopsy lap_right_hemicolectomy continent_ileostomy \
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 1
insert_endotracheal_tube mult_seg_sm_bowel_excis \
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
small-to-large_bowel_nec opn_lft_hemicolectmy_nec
0 1 1
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 1 0
7 0 0
8 0 0
9 0 0
[10 rows x 97 columns]
我运行了这样的规则:
from apyori import apriori as ap
rulez = ap(ohe_df, min_support = 0.1, min_length = 2,use_colnames=True)
我仅同时进行2项手术,因此我不希望组合> 2项。
freezeset发生了什么?
谢谢
您需要将输入数据放在列表列表中,其中每个列表都是一对在一起的东西。我整理了一些数据:
# Replace 1's with the column name
df = df.replace(1, pd.Series(df.columns, df.columns))
# get a list of non-zero values per row into an array of lists
ops = df.apply(lambda x: [v for v in x.values if v!=0], axis=1).values
ops变量现在看起来不错:
array([list(['small_bowel_incision_nec', 'colonoscopy']),
list(['sm_bowel_exteriorization', 'colonoscopy']),
list(['sm-to-sm_bowel_anastom', 'small_bowel_suture_nec']),
list(['small_bowel_incision_nec', 'colonoscopy']),
list(['anal_anastomosis', 'open_sigmoidectomy_nec']),
list(['colonoscopy', 'c.a.t._scan_of_abdomen']),
list(['sm-to-sm_bowel_anastom', 'open_sigmoidectomy_nec']),
list(['c.a.t._scan_of_abdomen', 'small_bowel_suture_nec']),
list(['incisional_hernia_repair', 'small_bowel_suture_nec']),
list(['small_bowel_incision_nec', 'colonoscopy'])], dtype=object)
# Run apriori, getting them as a list
rulez = list(ap(ops, min_support = 0.1, min_length = 2,use_colnames=True))
样本输出
[RelationRecord(items=frozenset({'anal_anastomosis'}), support=0.1, ordered_statistics=[OrderedStatistic(items_base=frozenset(), items_add=frozenset({'anal_anastomosis'}), confidence=0.1, lift=1.0)]),
RelationRecord(items=frozenset({'c.a.t._scan_of_abdomen'}), support=0.2, ordered_statistics=[OrderedStatistic(items_base=frozenset(), items_add=frozenset({'c.a.t._scan_of_abdomen'}), confidence=0.2, lift=1.0)]),
RelationRecord(items=frozenset({'colonoscopy'}), support=0.5, ordered_statistics=[OrderedStatistic(items_base=frozenset(), items_add=frozenset({'colonoscopy'}), confidence=0.5, lift=1.0)]),...]