我正在尝试使用apyori模块运行关联规则。我的“项目”是各种类型的手术(row = patient case),正如您在下面的dataframe示例中看到的那样。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个项目。
冰柜怎么了??
谢谢
发布于 2020-06-08 11:24:08
您需要将输入数据放在列表列表中,其中每个列表都是一对组合在一起的东西。我编了一些数据:
# 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)]),...]
https://stackoverflow.com/questions/62269385
复制相似问题