【问题标题】:Pandas One hot encoding: Bundling together less frequent categoriesPandas One 热门编码:将不太常见的类别捆绑在一起
【发布时间】:2017-09-06 03:35:40
【问题描述】:

我正在对具有大约 18 种不同值的分类列进行一次热编码。我想仅为那些出现超过某个阈值(比如说 1%)的值创建新列,并创建另一个名为 other values 的列,如果值不是那些频繁值,则该列具有 1。

我正在使用 Pandas 和 Sci-kit learn。我探索了 pandas get_dummies 和 sci-kit learn 的 one hot encoder,但不知道如何将频率较低的值捆绑到一列中。

【问题讨论】:

    标签: python pandas scikit-learn one-hot-encoding


    【解决方案1】:
    pip install siuba 
    #( in python or anaconda prompth shell)
    
    #use library as:
    from siuba.dply.forcats import fct_lump, fct_reorder 
    
    #just like fct_lump of R :
    
    df['Your_column'] = fct_lump(df['Your_column'], n= 10)
    
    df['Your_column'].value_counts() # check your levels
    
    #it reduces the level to 10, lumps all the others as 'Other'
    

    R 有一个很好的函数 fct_lump 用于此目的,现在它被复制到 python,只需选择要保留的级别数,所有其他级别将被捆绑为“其他”。

    【讨论】:

      【解决方案2】:

      计划

      • pd.get_dummies 正常进行一次热编码
      • sum() < threshold 识别被聚合的列
        • 我使用pd.value_counts 和参数normalize=True 来获取出现百分比。
      • join

      def hot_mess2(s, thresh):
          d = pd.get_dummies(s)
          f = pd.value_counts(s, sort=False, normalize=True) < thresh
          if f.sum() == 0:
              return d
          else:
              return d.loc[:, ~f].join(d.loc[:, f].sum(1).rename('other'))
      

      考虑pd.Seriess

      s = pd.Series(np.repeat(list('abcdef'), range(1, 7)))
      
      s
      
      0     a
      1     b
      2     b
      3     c
      4     c
      5     c
      6     d
      7     d
      8     d
      9     d
      10    e
      11    e
      12    e
      13    e
      14    e
      15    f
      16    f
      17    f
      18    f
      19    f
      20    f
      dtype: object
      

      hot_mess(s, 0)

          a  b  c  d  e  f
      0   1  0  0  0  0  0
      1   0  1  0  0  0  0
      2   0  1  0  0  0  0
      3   0  0  1  0  0  0
      4   0  0  1  0  0  0
      5   0  0  1  0  0  0
      6   0  0  0  1  0  0
      7   0  0  0  1  0  0
      8   0  0  0  1  0  0
      9   0  0  0  1  0  0
      10  0  0  0  0  1  0
      11  0  0  0  0  1  0
      12  0  0  0  0  1  0
      13  0  0  0  0  1  0
      14  0  0  0  0  1  0
      15  0  0  0  0  0  1
      16  0  0  0  0  0  1
      17  0  0  0  0  0  1
      18  0  0  0  0  0  1
      19  0  0  0  0  0  1
      20  0  0  0  0  0  1
      

      hot_mess(s, .1)

          c  d  e  f  other
      0   0  0  0  0      1
      1   0  0  0  0      1
      2   0  0  0  0      1
      3   1  0  0  0      0
      4   1  0  0  0      0
      5   1  0  0  0      0
      6   0  1  0  0      0
      7   0  1  0  0      0
      8   0  1  0  0      0
      9   0  1  0  0      0
      10  0  0  1  0      0
      11  0  0  1  0      0
      12  0  0  1  0      0
      13  0  0  1  0      0
      14  0  0  1  0      0
      15  0  0  0  1      0
      16  0  0  0  1      0
      17  0  0  0  1      0
      18  0  0  0  1      0
      19  0  0  0  1      0
      20  0  0  0  1      0
      

      【讨论】:

        【解决方案3】:

        下面的内容怎么样:

        创建一个数据框

        df = pd.DataFrame(data=list('abbgcca'), columns=['x'])
        df
        
            x
        0   a
        1   b
        2   b
        3   g
        4   c 
        5   c
        6   a
        

        替换出现频率低于给定阈值的值。我将创建该列的副本,这样我就不会修改原始数据框。第一步是创建value_counts 的字典,然后用这些计数替换实际值,以便将它们与阈值进行比较。将低于该阈值的值设置为“其他值”,然后使用pd.get_dummies 获取虚拟变量

        #set the threshold for example 20%
        thresh = 0.2
        x = df.x.copy()
        #replace any values present less than the threshold with 'other values'
        x[x.replace(x.value_counts().to_dict()) < len(x)*thresh] = 'other values'
        #get dummies
        pd.get_dummies(x)
        
                a       b       c       other values
            0   1.0     0.0     0.0     0.0
            1   0.0     1.0     0.0     0.0
            2   0.0     1.0     0.0     0.0
            3   0.0     0.0     0.0     1.0
            4   0.0     0.0     1.0     0.0
            5   0.0     0.0     1.0     0.0
            6   1.0     0.0     0.0     0.0
        

        您也可以使用Counter,它可能会更干净一些

        from collections import Counter
        x[x.replace(Counter(x)) < len(x)*thresh] = 'other values'
        

        【讨论】:

          猜你喜欢
          • 2018-06-26
          • 1970-01-01
          • 2021-12-17
          • 2017-06-21
          • 1970-01-01
          • 2016-02-14
          • 2023-03-06
          • 2019-07-06
          • 2017-08-18
          相关资源
          最近更新 更多