當我在像熊貓的數據幀:大熊貓/ 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
也許單程可以類似於這裏演示:https://jcrist.github.io/dask-sklearn-part-3.html –
「目標」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%? –
您可能是對的,我需要考慮這一點,但重點是我想*並行/有效地實施這種劃分*(某種百分比)。而實際上,'target:0'是無關緊要的。我只對'target:1'感興趣,或者以不同的方式指出:每個列每個組的'target:1/allRecords'的比例。也許這是一個更好的表述。 –