为了完成我的毕业论文,我需要创建一个扑克 Action 数据集来测试模型。我写了一个函数来读取带有 information about the hand 的文本文件并返回一个 list,我将其附加到 pandas 数据框。
我有大约 1500 个文件,每个文件包含 1500~3000 手需要传递给这个函数,所以我的主脚本看起来像这样。
import os
os.chdir("C:/Users/jctda/OneDrive/Documentos/TCC/Programa")
import pandas as pd
from datagen import DataGenerator, EmptyLine
from poker.room.pokerstars import PokerStarsHandHistory
from functions import FindFold, GetFiles, GetShowers
#IMPORT DATAGEN AQUI
database = pd.DataFrame()
files = GetFiles('hand_texts')
for hand_text in files:
text=open('hand_texts/' + hand_text)
b=text.read()
hands=b.split("\n\n\n\n\n")
text.close()
for i in range(1,len(hands)):
try:
hh = PokerStarsHandHistory(unicode(hands[i]))
hh.parse()
fold = FindFold(hh)
if fold == 'showdown':
for shower in GetShowers(hh):
database = database.append(DataGenerator(hh,shower,hand_text,i))
print('Success in parsing iteration ' + str(i) + ' from file' + hand_text)
except:
print('PARSER ERROR ON ITERATION [[' + str(i) + ']] FROM FILE [[' + hand_text + ']]')
database = database.append(EmptyLine(hand_text,i))
database.to_csv('database2.csv')
问题是运行几个小时后它变得非常慢。第一个文件大约需要 20 秒,但每次都变慢,运行 8 小时后,每个文件开始花费一个多小时。我刚开始为这个项目学习 python,所以我可能在某个地方犯了一个大错误,导致它花费的时间比需要的多得多,但我找不到它。
另一件困扰我的事情是,当它在 16GB 的机器上运行时,它消耗的 RAM 不到 1GB。我考虑过强制它使用更多内存,但显然 python 没有内存限制,所以我猜这只是错误的代码
谁能帮我弄清楚该怎么做?
请您参考如下方法:
如 here 中所述,不要附加到循环内的数据框,因为它非常低效。而是做这样的事情:
files = GetFiles('hand_texts')
database = []
for hand_text in files:
# as a sidenote, with contexts are helpful for these:
with open('hand_texts/' + hand_text) as text:
b=text.read()
hands=b.split("\n\n\n\n\n")
for i in range(1,len(hands)):
try:
hh = PokerStarsHandHistory(unicode(hands[i]))
hh.parse()
fold = FindFold(hh)
if fold == 'showdown':
for shower in GetShowers(hh):
database.append(DataGenerator(hh,shower,hand_text,i))
print('Success in parsing iteration ' + str(i) + ' from file' + hand_text)
except:
print('PARSER ERROR ON ITERATION [[' + str(i) + ']] FROM FILE [[' + hand_text + ']]')
database.append(EmptyLine(hand_text,i))
pd.DataFrame(database).to_csv('database2.csv'))
