2017-05-06 49 views
0

當我在像熊貓的數據幀:大熊貓/ DASK計算百分比爲多個列 - 列並行操作

raw_data = { 
     'subject_id': ['1', '2', '3', '4', '5'], 
     'name': ['A', 'B', 'C', 'D', 'E'], 
     'nationality': ['DE', 'AUT', 'US', 'US', 'US'], 
     'alotdifferent': ['x', 'y', 'z', 'x', 'a'], 
     'target': [0,0,0,1,1], 
     'age_group' : [1, 2, 1, 3, 1]} 
df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'name', 'nationality', 'alotdifferent','target','age_group']) 
df_a.nationality = df_a.nationality.astype('category') 
df_a.alotdifferent = df_a.alotdifferent.astype('category') 
df_a.name = df_a.name.astype('category') 

目前,我使用:

FACTOR_FIELDS = df_a.select_dtypes(include=['category']).columns 
columnsToDrop = ['alotdifferent'] 
columnsToBias_keep = FACTOR_FIELDS[~FACTOR_FIELDS.isin(columnsToDrop)] 
target = 'target' 

def quotients_slow(df_a): 
    # parallelism = 8 
    # original = dd.from_pandas(df.copy()) 
    original = df_a.copy() 
    output_df = original 
    ratio_weights = {} 

    for colname in columnsToBias_keep.union(columnsToDrop): 
     # group only a single time 
     grouped = original.groupby([colname, target]).size() 
     # calculate first ratio 
     df = grouped/original[target].sum() 
     nameCol = "pre_" + colname 
     grouped_res = df.reset_index(name=nameCol) 
     grouped_res = grouped_res[grouped_res[target] == 1] 
     grouped_res = grouped_res.drop(target, 1) 
     # todo persist the result in dict for transformer 
     result_1 = grouped_res 

     # calculate second ratio 
     df = (grouped/grouped.groupby(level=0).sum()) 
     nameCol_2 = "pre2_" + colname 
     grouped = df.reset_index(name=nameCol_2) 
     grouped_res = grouped[grouped[target] == 1] 
     grouped_res = grouped_res.drop(target, 1) 
     result_2 = grouped_res 

     # persist the result in dict for transformer 
     # this is required to separate fit and transform stage (later on in a sklearn transformer) 
     ratio_weights[nameCol] = result_1 
     ratio_weights[nameCol_2] = result_2 

     # retrieve results 
     res_1 = ratio_weights['pre_' + colname] 
     res_2 = ratio_weights['pre2_' + colname] 
     # merge ratio_weight with original dataframe 
     output_df = pd.merge(output_df, res_1, on=colname, how='left') 
     output_df = pd.merge(output_df, res_2, on=colname, how='left') 
     output_df.loc[(output_df[nameCol].isnull()), nameCol] = 0 
     output_df.loc[(output_df[nameCol_2].isnull()), nameCol_2] = 0 

     if colname in columnsToDrop: 
      output_df = output_df.drop(colname, 1) 

    return output_df 


quotients_slow(df_a) 

計算的比率每個組以target:1爲每個(分類)列以兩種方式。因爲我想對多個列執行這個操作,所以我無意中迭代了所有這些操作。但是這個操作非常緩慢。 此處示例:10 loops, best of 3: 37 ms per loop。對於我的約500000行和100列左右的真實數據集,這確實需要一段時間。

不應該在dask或pandas中加速(列並行方式,平凡並行化)嗎?有沒有可能在大熊貓中更有效地實施它?是否可以減少計算商的數據通過次數?

編輯

當試圖在使用dask.delayed for循環來實現對列並行,我無法弄清楚如何建立圖在列,因爲我需要調用計算得到元組。

delayed_res_name = delayed(compute_weights)(df_a, 'name') 
a,b,c,d = delayed_res_name.compute() 
ratio_weights = {} 
ratio_weights[c] = a 
ratio_weights[d] = b 
+0

也許單程可以類似於這裏演示:https://jcrist.github.io/dask-sklearn-part-3.html –

+0

「目標」colu的百分比任何其他專欄的mn ...「你的計算在這裏得出一個不尋常的比例。例如,5個觀察值中有1個出現'name:A' /'target:0'組合。但是你在'target'中將'1'的值除以'1'值的總和。想象一下,如果你有'name:A' /'target:0'的3個條目,但'target'中仍然只有兩個'1'值。 「name:A' /'target:0'比例是1.5還是150%? –

+0

您可能是對的,我需要考慮這一點,但重點是我想*並行/有效地實施這種劃分*(某種百分比)。而實際上,'target:0'是無關緊要的。我只對'target:1'感興趣,或者以不同的方式指出:每個列每個組的'target:1/allRecords'的比例。也許這是一個更好的表述。 –

回答

1

下面是使用熊貓的第一個商的合理快速解決方案。它假定您對subject_id的計算比例不感興趣。我還爲您的示例添加了一些數據以涵蓋更多邊緣案例。

首先,生成採樣數據:

raw_data = { 
    'subject_id': ['1', '2', '3', '4', '5', '6','7'], 
    'name': ['A', 'B', 'C', 'D', 'E', 'A','A'], 
    'nationality': ['DE', 'AUT', 'US', 'US', 'US', 'DE','DE'], 
    'alotdifferent': ['x', 'y', 'z', 'x', 'a','x','z'], 
    'target': [0,0,0,1,1,0,1], 
    'age_group' : [1, 2, 1, 3, 1, 2,1]} 

df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'name', 'nationality', 'alotdifferent','target','age_group']) 

現在計算的比例和測量速度:

def compute_prop(group): 
    return group.sum()/float(group.count()) 

def build_master(df): 
    master = df.copy() 
    fields = df.drop(['subject_id','target'],1).columns 

    for field in fields: 
     master = (pd.merge(master, df.groupby(field, as_index=False) 
            .agg({'target':compute_prop}) 
            .rename(columns={'target':'pre_{}'.format(field)}), 
          on=field) 
      ) 

    master.sort_values('subject_id') 
    return master 

%timeit master = build_master(df_a) 
10 loops, best of 3: 17.1 ms per loop 

輸出:

subject_id name nationality alotdifferent target age_group pre_name \ 
0   1 A   DE    x  0   1 0.333333 
5   2 B   AUT    y  0   2 0.000000 
2   3 C   US    z  0   1 0.000000 
6   4 D   US    x  1   3 1.000000 
3   5 E   US    a  1   1 1.000000 
4   6 A   DE    x  0   2 0.333333 
1   7 A   DE    z  1   1 0.333333 

    pre_nationality pre_alotdifferent pre_age_group 
0   0.333333   0.333333   0.5 
5   0.000000   0.000000   0.0 
2   0.666667   0.500000   0.5 
6   0.666667   0.333333   1.0 
3   0.666667   1.000000   0.5 
4   0.333333   0.333333   0.0 
1   0.333333   0.500000   0.5 
+0

是否有可能添加第二種情況/在一次傳遞中計算兩個聚合?我認爲只需要添加下面的行'df =(groupped/groupped.groupby(level = 0).sum())'是否有可能將第二個函數傳遞給'agg'? –

+0

但我真的不知道與我的初始解決方案有什麼不同。在那裏,我已經遍歷所有的字段並聚合數據。你只需結合這些步驟,即代碼更清潔。但是,這可以產生任何性能效率?最後,它需要是一個sklearn變壓器,它可以存儲筆記本中列出的*重量*。所以你使用的合併和組合可能需要分開。 –