亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

在Pandas中使用大型數據集的所有可能結果并對結果進行排序

在Pandas中使用大型數據集的所有可能結果并對結果進行排序

MMTTMM 2022-09-27 10:41:38
我有一個數據幀,有700行和5100列。每行都包含真或假。使用此df,我想測試列的所有可能組合,并使用結果測試每行是否等于True。前幾天,我在這個帖子中得到了一位用戶的極好幫助:如何在python中測試所有可能的組合與真/假陳述?,建議我使用itertools和“產品”中的“組合”。這對于小型數據集工作正常。但是,當將此方法應用于我的(大得多的)數據集時,在測試超過2的組合時,內存不足。我想要的輸出與下面的示例類似,但我不會耗盡內存。感謝您的任何幫助。小數據集的建議方法:import pandas as pdfrom itertools import combinationsdf1 = pd.DataFrame({"Main1": [True, False, False, False, False, True, True],                    "Main2": [False, False, True, False, True, True, False],                    "Main3": [True, False, True, True, True, True, False]})df2 = pd.DataFrame({"Sub1": [False, False, True, False, True, False, True],                    "Sub2": [False, True, False, False, True, False, True],                    "Sub3": [True, False, True, False, False, False, True]})df3 = df1.join(df2)all_combinations = list(combinations(df3.columns, 2)) + \                   list(combinations(df3.columns, 3))for combination in all_combinations:   df3["".join(list(combination))] = df3[list(combination)].product(axis=1).astype(bool)df3.drop(labels=["Main1", "Main2", "Main3", "Sub1", "Sub2", "Sub3"], axis=1, inplace=True)df3   Main1Main2  Main1Main3  ...  Main3Sub2Sub3  Sub1Sub2Sub30       False        True  ...          False         False1       False       False  ...          False         False2       False       False  ...          False         False3       False       False  ...          False         False4       False       False  ...          False         False5        True        True  ...          False         False6       False       False  ...          False          True
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

所以,我并不為這個感到驕傲,但也許它有機會獲勝 :)......


我認為你需要離開數據幀,因為它不能長得足夠大,無法正確保存你的結果。如果您的結果可預測地稀疏,則可以使用替代結構,如下所示。


請注意,對于您正在做的事情來說,這將是一個很長的循環,22B x數據幀的長度,因此超過一萬億次命中,但是如果您只需要做一次,誰在乎。中的組合函數是一個生成器,因此它將具有內存效率。itertools


我認為您正在尋找上面“全部為 True”的結果,因為您正在使用產品運算符。我在評論中說錯了。


如果完成,您可以在下面添加第二個循環來覆蓋大小2的組合!:)


import pandas as pd

from itertools import combinations


df = pd.DataFrame({ "Main1": [True, False, False, False, False, True, True],

                    "Main2": [False, False, True, False, True, True, False],

                    "Main3": [True, False, True, True, True, True, False],

                    "Sub1": [False, False, True, False, True, False, True],

                    "Sub2": [False, True, False, False, True, False, True],

                    "Sub3": [True, False, True, False, False, False, True]})


print(df)

data = df.to_dict('index')


# test to see if it looks right for row 0

print(data[0])


# now the data is in a nested dictionary, which should be more "iterable"

results = []


for combo in combinations(df.columns, 3):

    for key in data:  # iterate through the rows in the data...  index is key.

        values = set(data[key][col] for col in combo)

        if all(values):

            results.append((key, combo))


# inspect results...

for result in results:

    print(f'row: {result[0]} columns: {results[1]} product is TRUE')

收益 率:


   Main1  Main2  Main3   Sub1   Sub2   Sub3

0   True  False   True  False  False   True

1  False  False  False  False   True  False

2  False   True   True   True  False   True

3  False  False   True  False  False  False

4  False   True   True   True   True  False

5   True   True   True  False  False  False

6   True  False  False   True   True   True

{'Main1': True, 'Main2': False, 'Main3': True, 'Sub1': False, 'Sub2': False, 'Sub3': True}

row: 5 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 0 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE

[Finished in 0.6s]


查看完整回答
反對 回復 2022-09-27
  • 1 回答
  • 0 關注
  • 81 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號