第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何自動將數(shù)據(jù)幀切片成批次以避免 python 中的 MemoryError

如何自動將數(shù)據(jù)幀切片成批次以避免 python 中的 MemoryError

森林海 2023-04-11 16:25:53
我有一個包含 270 萬行的數(shù)據(jù)框,如下所示 -dfOut[10]:          ClaimId  ServiceSubCodeKey  ClaimRowNumber  SscRowNumber0        1902659                183               1             11        1902659               2088               1             22        1902663               3274               2             13        1902674                 12               3             14        1902674                 23               3             2         ...                ...             ...           ...2793010  2563847               3109          603037             42793011  2563883               3109          603038             12793012  2564007               3626          603039             12793013  2564007               3628          603039             22793014  2564363               3109          603040             1[2793015 rows x 4 columns]我正在嘗試在下面的 python 中對此進行熱編碼,但最終出現(xiàn)內(nèi)存錯誤:import pandas as pdcolumns = (    pd.get_dummies(df["ServiceSubCodeKey"])    .reindex(range(df.ServiceSubCodeKey.min(),        df.ServiceSubCodeKey.max()+1), axis=1, fill_value=0)    # now it has all digits    .astype(str)    )# this will create codescodes_values = [int(''.join(r)) for r in columns.itertuples(index=False)]codes = pd.Series({'test': codes_values}).explode()codes.index = df.index# groupby and aggregate the values into listsdfg = codes.groupby(df.ClaimId).agg(list).reset_index()# sum the lists; doing this with a pandas function also does not work, so no .sum or .applysummed_lists = list()for r, v in dfg.iterrows():    summed_lists.append(str(sum(v[0])))# assign the list of strings to a columndfg['sums'] = summed_lists# perform the remainder of the functions on the sums columndfg['final'] = dfg.sums.str.pad(width=columns.shape[1], fillchar='0').str.rstrip('0')# merge df and dfg.finaldfm = pd.merge(df, dfg[['ClaimId', 'final']], on='ClaimId')dfm  File "pandas/_libs/lib.pyx", line 574, in pandas._libs.lib.astype_strMemoryError我如何在自動批處理中執(zhí)行此操作,以免出現(xiàn)內(nèi)存錯誤?
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

onehot = []

for groupi, group in df.groupby(df.index//1e5):

    # encode each group separately

    onehot.expand(group_onehot)

df = df.assign(onehot=onehot)

會給你 28 個小組單獨工作。


但是,查看您的代碼,該行:


codes_values = [int(''.join(r)) for r in columns.itertuples(index=False)]

integer正在創(chuàng)建一個可能長達 4k 位的字符串并嘗試在 10e4000 范圍內(nèi)創(chuàng)建一個字符串,這將導致溢出(請參閱https://numpy.org/devdocs/user/basics.types.html)


編輯


另一種編碼方法。從這個 df 開始:


df = pd.DataFrame({

    'ClaimId': [1902659, 1902659, 1902663, 1902674, 1902674, 2563847, 2563883,

        2564007, 2564007, 2564363],

    'ServiceSubCodeKey': [183, 2088, 3274, 12, 23, 3109, 3109, 3626, 3628, 3109]

    })

代碼:


scale = df.ServiceSubCodeKey.max() + 1

onehot = []

for claimid, ssc in df.groupby('ClaimId').ServiceSubCodeKey:

    ssc_list = ssc.to_list()

    onehot.append([claimid,

        ''.join(['1' if i in ssc_list else '0' for i in range(1, scale)])])

onehot = pd.DataFrame(onehot, columns=['ClaimId', 'onehot'])

print(onehot)

輸出


   ClaimId                                             onehot

0  1902659  0000000000000000000000000000000000000000000000...

1  1902663  0000000000000000000000000000000000000000000000...

2  1902674  0000000000010000000000100000000000000000000000...

3  2563847  0000000000000000000000000000000000000000000000...

4  2563883  0000000000000000000000000000000000000000000000...

5  2564007  0000000000000000000000000000000000000000000000...

6  2564363  0000000000000000000000000000000000000000000000...

這修復了您的方法中的溢出問題并避免調(diào)用pd.get_dummies()創(chuàng)建 600K x 4K 虛擬數(shù)據(jù)幀,具有迭代分組系列和在每個組上構(gòu)建列表理解的障礙(既不利用 pandas 的內(nèi)置 C 實現(xiàn)) .


從這里您可以:


推薦:繼續(xù)保持每個 one-hot 編碼的摘要ClaimId,或者

您要求的是:根據(jù)df需要合并,復制相同的編碼與ClaimId復制的次數(shù)一樣多df


df = df.merge(onehot, on='ClaimId')

輸出


   ClaimId  ServiceSubCodeKey                                             onehot

0  1902659                183  0000000000000000000000000000000000000000000000...

1  1902659               2088  0000000000000000000000000000000000000000000000...

2  1902663               3274  0000000000000000000000000000000000000000000000...

3  1902674                 12  0000000000010000000000100000000000000000000000...

4  1902674                 23  0000000000010000000000100000000000000000000000...

5  2563847               3109  0000000000000000000000000000000000000000000000...

6  2563883               3109  0000000000000000000000000000000000000000000000...

7  2564007               3626  0000000000000000000000000000000000000000000000...

8  2564007               3628  0000000000000000000000000000000000000000000000...

9  2564363               3109  0000000000000000000000000000000000000000000000...


查看完整回答
反對 回復 2023-04-11
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號