【问题标题】:apyori apriori broken itemset outputapyori apriori 损坏的项目集输出
【发布时间】:2020-06-08 19:00:21
【问题描述】:

我正在尝试使用 apyori 模块运行关联规则。 我的“项目”是各种手术(行 = 患者病例),正如您在下面的数据框示例中看到的那样。 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

frozenset 坏了... 这是我的输入 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 项的组合。

frozenset 发生了什么?

谢谢

【问题讨论】:

    标签: python apriori


    【解决方案1】:

    您需要将输入数据放在列表列表中,其中每个列表是一对组合在一起的事物。我编了一些数据:

    # 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)]),...]
    

    【讨论】:

    • 效果很好。这有点令人困惑,因为 MLXtend 库想要一个 1/0 的数据帧(单热编码),每一列都是项目......
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多