【问题标题】:UpSetPlot from actual sets来自实际集合的 UpSetPlot
【发布时间】:2021-01-29 19:38:50
【问题描述】:

鉴于我拥有的实际集合,我想使用UpSetPlot,但我找不到任何以这种方式使用它的示例。标准示例是这样的:

from upsetplot import generate_counts, plot
example = generate_counts()
plot(example, orientation='vertical')

其中生成的exampleSeries,如下所示。

cat0   cat1   cat2 
False  False  False      56
              True      283
       True   False    1279
              True     5882
True   False  False      24
              True       90
       True   False     429
              True     1957
Name: value, dtype: int64

有没有办法从cat0cat1cat2 类别中的实际元素自动生成这种计数结构?

【问题讨论】:

    标签: python plot set upsetplot


    【解决方案1】:

    在另一个答案中使用@StupidWolf 的提示,这是我自己问题的答案。给定3套

    set1 = {0,1,2,3,4,5}
    set2 = {3,4,5,6,10}
    set3 = {0,5,6,7,8,9}
    

    这里是为这三个集合绘制一个不规则图的代码:

    import pandas as pd
    from upsetplot import plot
    set_names = ['set1', 'set2', 'set3']
    all_elems = set1.union(set2).union(set3)
    df = pd.DataFrame([[e in set1, e in set2, e in set3] for e in all_elems], columns = set_names)
    df_up = df.groupby(set_names).size()
    plot(df_up, orientation='horizontal')
    

    这里是第 4 行和第 5 行,将上面的代码概括为一组集合,比如sets = [set1, set2, set3]

    all_elems = list(set().union(*sets))
    df = pd.DataFrame([[e in st for st in sets] for e in all_elems], columns = set_names)
    

    【讨论】:

      【解决方案2】:

      在我看来,它就像是 pandas 的产品:

      import numpy as np
      import pandas as pd
      
      from upsetplot import generate_counts, plot
      example = generate_counts()
      type(example)
      
      pandas.core.series.Series
      
      example.index
      
      MultiIndex([(False, False, False),
                  (False, False,  True),
                  (False,  True, False),
                  (False,  True,  True),
                  ( True, False, False),
                  ( True, False,  True),
                  ( True,  True, False),
                  ( True,  True,  True)],
                 names=['cat0', 'cat1', 'cat2'])
      

      所以如果你的数据框是这样的:

      df = pd.DataFrame(np.random.choice([True,False],(100,3)),
                        columns=['cat0','cat1','cat2'])
      

      你可以这样做:

      example = df.groupby(['cat0','cat1','cat2']).size()
      plot(example, orientation='vertical')
      

      我认为限制是 cat0、cat1、cat2 中的元素必须是布尔值。

      【讨论】:

        【解决方案3】:

        有几种方法可以使用集合来表示类别成员资格。为了帮助将集合转换为upsetplot 所需的格式,您将找到帮助程序from_membershipsfrom_contentsfrom_indicators

        另请参阅Data Format Guide

        【讨论】:

          猜你喜欢
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-17
          • 1970-01-01
          相关资源
          最近更新 更多