如何在python中处理大型数据集时解决分配内存问题?
我正在为30000行的数据集编写一个BOW代码。 我有一列X_列车,是(21000,2)。这两行是:title和description。 因此,我有以下代码:如何在python中处理大型数据集时解决分配内存问题?,python,memory,Python,Memory,我正在为30000行的数据集编写一个BOW代码。 我有一列X_列车,是(21000,2)。这两行是:title和description。 因此,我有以下代码: def text_to_bow(text: str) -> np.array: text = text.split() res = np.zeros(len(bow_vocabulary)) #bow_vocabulary includes 10000 most popular tokens for word
def text_to_bow(text: str) -> np.array:
text = text.split()
res = np.zeros(len(bow_vocabulary)) #bow_vocabulary includes 10000 most popular tokens
for word in text:
for i in range(len(bow_vocabulary)):
if word == bow_vocabulary[i]:
res[i] += 1
return res
def items_to_bow(items: np.array) -> np.array:
desc_index = 1
res = np.empty((0,k), dtype='uint8')
for i in range(len(items)):
description = items[i][desc_index]
temp = text_to_bow(description)
res = np.append(res, [temp], axis=0)
return np.array(res)
我的代码似乎工作正常,因为我的任务中有几个断言
所以,当我跑步时:
X_train_bow = items_to_bow(X_train)
我得到一个错误:
MemoryError: Unable to allocate 12.1 MiB for an array with shape (158,
10000) and data type float64
我已经在Ubuntu中将Overmit_memory设置为1,但没有帮助。我不想也使用64位python,因为模块可能有问题
我还尝试了另一个函数(使用正则数组):
但它似乎要工作一个小时左右,这并不方便
有什么办法解决这个问题吗?如果有任何可能的帮助,我们将不胜感激。进行分块。在pandas中,使用
chunksize
param进行此操作。读取数据块。处理数据。将输出附加到文件。确保已删除该区块。重复。提供样本数据。数据:
def items_to_bow(items: np.array) -> np.array:
desc_index = 1
res = []
for i in range(len(items)):
description = items[i][desc_index]
temp = text_to_bow(description)
res.append(temp)
if len(res)//1000 > 0:
print(len(res))
return np.array(res)