如何处理加载繁重CSV文件时的内存错误

2024-09-30 20:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个CSV文件来存储如下数据:

img = loadmat('test.mat')
a,b,c = np.shape(img)  # c is the no. of slices
for i in range(c):
    feature_1 = #kurtosis of image slice
    feature_2 = #entropy of image slice
    .
    .
    feature_8 = #skewness of image slice
    train_data = np.concatenate((feature_1,feature_2,...,feature_8),axis=1)
    #this loop will repeat for all the slices and result will be appended in another array
    train_data01.append(train_data)
    #this will give a list with all data in one axis.
feature_img = np.concatenate(train_data01,axis=0)
#this will give a array of size (mx8) where m is the number of pixels in image.
np.savetxt('savedata.csv',feature_img,fmt = '%4.2f') #save teh data in csv file

现在我试着像这样检索数据:

hg01 = []
with open('savedata.csv','rb') as f:
    reader = csv.reader(f)
    for row in reader:
        hg01.append(row)

hg01 = np.concatenate(hg01, axis = 0)
hg01_feature = np.empty(shape=(np.shape(hg01)[0],80), dtype='float32')

for i in range(np.shape(hg01)[0]):
     a = hg01[i]
     b = np.fromstring(a,dtype='float32',sep=' ')
     np.append(np.transpose(hg01_feature[i,:]),b,axis=1)

现在的问题是hg01的numpy阵列的实际尺寸是1520640x80,大约644MB。现在,在第6位患者我的系统关闭后,当加载第7位患者数据时,它在下面一行显示内存错误:

hg07 = np.concatenate(hg07, axis = 0)

所以伙计们,我有大约40个病人的数据,以及如何加载这些数据。 我的工作与随机森林图像分类。我的系统有8gbram。你知道吗


Tags: of数据inimageimgfordatanp
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:13

每一行都是一个字符串。我想你可以通过拆分每个字符串来获得你想要的行为。拆分有点复杂,因为我只大致正确:每一行实际上是一个包含单个字符串的单元素列表,因此必须遵从[0]元素。拆分应该如下所示:

new_feature = [ f[0].split() for f in feature ]

现在做np.形状在新的功能,希望这将给你想要的。你知道吗

相关问题 更多 >