【问题标题】:how to recode the categorical variable to ordinal based on the event rate of another variable in python?如何根据python中另一个变量的事件率将分类变量重新编码为序数?
【发布时间】:2016-05-30 00:23:48
【问题描述】:

我在 python 中有一个包含许多分类变量的数据框,目标变量是二进制的。我想根据变量的每个类别上的目标变量事件率(与目标变量的平均值相同)的等级将分类变量转换为序数。例如,如果下面是我的原始数据集

对于 column1 上的每个类别,也就是“A”和“B”,我们有:

对于第 2 列中的每个类别,即“C”、“D”、“E”、“F”、“G”,我们有:

所以我希望能够像这样创建最终数据集:

我怎样才能创建一个这样的?

谢谢!!!!

【问题讨论】:

    标签: python pandas data-manipulation categorical-data


    【解决方案1】:

    你想做的是transform。让我们看看以下...

    col1 col2  target
    0    A    D       1
    1    A    A       0
    2    A    E       0
    3    B    A       0
    4    A    C       0
    5    A    D       1
    6    B    E       0
    7    A    C       0
    8    B    C       0
    9    B    B       0
    

    您可以使用groupby 进行转换:

    df.groupby('col1').transform(np.mean)
    
         target
    0  0.333333
    1  0.333333
    2  0.333333
    3  0.000000
    4  0.333333
    5  0.333333
    6  0.000000
    7  0.333333
    8  0.000000
    9  0.000000
    

    现在,你只需要变换的系列信息...

    df.groupby('col1').transform(np.mean)['target']
    0    0.333333
    1    0.333333
    2    0.333333
    3    0.000000
    4    0.333333
    5    0.333333
    6    0.000000
    7    0.333333
    8    0.000000
    9    0.000000
    

    pd.Series 可以通过几种不同的方式转换为因子。一种方法是使用pd.factorize()

    pd.factorize(df.groupby('col1').transform(np.mean)['target'])
    (array([0, 0, 0, 1, 0, 0, 1, 0, 1, 1]),
     Float64Index([0.333333333333, 0.0], dtype='float64'))
    

    您只在其中寻找因子值:

    pd.factorize(df.groupby('col1').transform(np.mean)['target'])[0]
    array([0, 0, 0, 1, 0, 0, 1, 0, 1, 1])
    

    现在,只需为其指定您选择的列名:)。

    希望这会有所帮助...

    【讨论】:

      【解决方案2】:

      只需计算每个grouped 类别值的meanrank 结果和map 类别值。示例数据:

      import string
      abc = string.ascii_uppercase
      df = pd.DataFrame({'target': np.random.randint(low=0, high=2, size=10), 'col1': np.random.choice(list(abc[:2]), size=10), 'col2': np.random.choice(list(abc[:5]), size=10)})
      
        col1 col2  target
      0    B    B       0
      1    A    E       0
      2    B    A       1
      3    B    E       1
      4    A    A       0
      5    A    E       0
      6    B    D       1
      7    A    E       0
      8    A    E       1
      9    B    B       0
      

      按照上述代码生成两列:

      for col in ['col1', 'col2']:
          df[col] = df.loc[:, col].map(df.groupby(col).target.mean().rank().astype(int))
      
         col1  col2  target
      0     2     1       0
      1     1     2       0
      2     2     3       1
      3     2     2       1
      4     1     3       0
      5     1     2       0
      6     2     4       1
      7     1     2       0
      8     1     2       1
      9     2     1       0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多