【问题标题】:How to make comparison each with each (combination of 1, 2, 3 factors)如何进行比较(1、2、3 个因素的组合)
【发布时间】:2020-10-06 15:33:57
【问题描述】:

您能帮我自动计算“我们的”客户在 1、2、3 个因素的组合中所占的份额吗?

我有一个客户和功能数据集。所有的客户都有标签:

  • 1 - “我们的”
  • 0 - 'not_ours'
clients ours car        house         boat     plane         bike
client1 1     1         0             1         1             1
client2 0     0         0             1         1             1
client3 1     0         0             0         1             1
client4 1     1         0             1         1             1
client5 0     0         0             1         1             1
client6 1     0         0             0         1             1
clientN 0     0         1             0         1             1

我想做 3 个实验:

  1. 了解我们在每个 1 因子值中的数量份额。理想的结果:

     factor_value    1                 1                 0                 0
     calculation     quantity of ours  share of ours (%) quantity of ours  share of ours (%)
     car             2                 100%              2                 40%
     house           0                 0%                4                 67%
     boat            2                 50%               2                 67%
     plane           4                 67%               0                 0%
     bike            4                 67%               0                 0%
    

    我们的份额 = 我们在因子价值中的份额。例如。 car' value = 0。比我们的要多 40%,因为 5 个客户没有汽车,其中有 2 个是我们的。

  2. 相同的计算,但检查每个因素的 2 个组合:

     car + house
     car + boat
     car  + plane
     car + bike
     house + boat
     house + plane
     house + bike
     boat + plane
     boat + bike
    
  3. 考虑 3 个因素的所有可能组合:

     car + house + boat
     car + house + plane
     car + house + bike
     car + boat + plane
     car + boat + bike
     car + plane + bike
    

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    以下是步骤(以 3 个因素为例):

    #create container for 3 factors combinations
    xgb3 = pd.DataFrame([('i', 'j', 'k')], columns = ['factor1', 'factor2', 'factor3'], index=[0])
    
    #take the previous  table with combinations of 2 factors: (res3)
    for i in range(len(res3)) :
        a = res3.iloc[i:i + 1,:]['factor1'].values[0]
        b = res3.iloc[i:i + 1,:]['factor2'].values[0]
    #add the third factor
        for j in df.iloc[:,5:].columns.values:
    #sort - to drop duplicates (e.g. a,c,b and a,b,c)
            to_sort = sorted([a, b, j])
            new_row3 = {'factor1':to_sort[0], 'factor2':to_sort[1], 'factor3':to_sort[2]}
            xgb3 = xgb3.append(new_row3, ignore_index=True)
            
    xgb3 = xgb3.drop_duplicates()
    #additional drop of duplicates inside the row (e.g. a,a,b). All the items in the row must be unique
    xgb3 = xgb3[(xgb3['factor1'] != xgb3['factor3']) & 
                (xgb3['factor2'] != xgb3['factor3']) & 
                (xgb3['factor1'] != xgb3['factor2'])].reset_index(drop=True)
    
    
    #create container for results
    result3 = pd.DataFrame([('','','',0,0,.1,0,0,.1)], columns = ['factor1','factor2','factor3','quantity_inside_combination', 'quantity_of_ours_inside_combination', '%_of_ours_inside_combination', 'quantity_outside_combination', 'quantity_of_ours_outside_combination', '%_of_ours_outside_combination'], index=[0])
    
    for i in range(1, len(xgb3)): #range begins with 1 to skip the first row with technical information
        f = xgb3.iloc[i:i+1,:]['factor1'].values[0]
        s = xgb3.iloc[i:i+1,:]['factor2'].values[0]
        x = xgb3.iloc[i:i+1,:]['factor3'].values[0]
        
        m = df['stl'][(df[f] == 1) & (df[s] == 1) & (df[x] == 1)].count()
        n = df['stl'][(df[f] == 1) & (df[s] == 1) & (df[x] == 1)].sum()
        o = df['stl'][(df[f] == 0) & (df[s] == 0) & (df[x] == 0)].count()
        p = df['stl'][(df[f] == 0) & (df[s] == 0) & (df[x] == 0)].sum()
        
        new_row3 = {'factor1':f, 
                   'factor2':s,
                    'factor3': x,
                   'quantity_inside_combination': m,
                   'quantity_of_ours_inside_combination': n,
                   '%_of_ours_inside_combination': 1. * n / m,
                   'quantity_outside_combination': o,
                   'quantity_of_ours_outside_combination': p,
                   '%_of_ours_outside_combination': 1. * p / o
                  }
        result3 = result3.append(new_row3, ignore_index=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2021-12-25
      相关资源
      最近更新 更多