从numpy数组中删除元素 - 内存有效

时间:2017-08-10 20:25:42

标签: python numpy multidimensional-array memory-management

我目前正在制作一个需要培训和验证数据的机器学习模型。我首先将所有数据加载到image_set(输入)和array_set(输出)。我想要做的是编写一个python脚本,该脚本占用此数据的10%(最小值:1个数据点)并分别存储在validata_imagevalidata_array中。问题是,当加载这些图像时,我的计算机内存不足,所以我一直遇到内存错误。以下是此流程的代码:

# "image_set" and "array_set" initialized above
length = image_set.shape[0]
numValData = 1
if (math.floor(length * .1) > 1):
    numValData = math.floor(length * .1)
    #Have the Validation data take up 10% of total data

validata_image = np.zeros((numValData,1000,1000,3), dtype='float32')
validata_array = np.zeros((numValData,1000,1000,1), dtype='float32')
#Each image is 1000x1000x3 (R, G, and B channels), while each array is (1000x1000x1)
#This large size is the reason why the data takes so much memory.

for i in range(numValData):
    rand = random.randint(0, image_set.shape[0] - 1)
    a = image_set[rand]
    b = array_set[rand]
    image_set = np.delete(image_set, rand, axis = 0)
    array_set = np.delete(array_set, rand, axis = 0)
    validata_image[i] = a
    validata_array[i] = b

#Rest of program executes here...

我相信很多内存使用来自np.delete()行。 This问题类似于我的问题,但似乎完整的问题从未真正得到过。 deepcopy()函数看起来很有希望,但是没有提供从初始列表中删除元素的方法。转换为常规数组并使用“pop()”似乎在内存使用方面效率相当低,所以实际上不知道最好的方法是什么。如果有人能帮我解决这个问题,我会非常感激。

0 个答案:

没有答案