尝试将 csv 文件读入 python 时内存不足
out of memory while trying to read csv file into python
我的数据是 88200(行)*29403(列)(大约 14Gb)。数据已使用 dlmwrite 在 matlab 中创建。我尝试过使用以下方法读取python中的文件。在所有尝试中 我 运行 内存不足 :
我的系统:ubuntu 16.04,RAM 32Gb,Swap 20Gb
Python 2.7.12,pandas:0.19,海湾合作委员会 5.4.0
1> 使用 csv:
import csv
import numpy
filename = 'data.txt'
raw_data = open(filename, 'rb')
reader = csv.reader(raw_data, delimiter=',', quoting=csv.QUOTE_NONE)
x = list(reader)
data = numpy.array(x).astype('float')
2a> 使用 numpy loadtxt:
import numpy
filename = 'data.txt'
raw_data = open(filename, 'rb')
data = numpy.loadtxt(raw_data, delimiter=",")
2b> 使用 numpy genfromtxt:
import numpy
x=np.genfromtxt('vectorized_image_dataset.txt',skip_header=0,skip_footer=0,delimiter=',',dtype='float32')
3> 使用 pandas.read_csv:
from pandas import *
import numpy as np
tp = read_csv(filepath_or_buffer='data.txt', header=None, iterator=True, chunksize=1000)
df = concat(tp, ignore_index=True)
在上述所有方法中它 运行 内存不足。
已使用 dlmwrite (matlab) 创建数据文件。图像列表(list.txt)被一张一张读取,转换为浮点数,矢量化并使用 dlmwrite 存储。代码如下:
fileID = fopen('list.txt');
N=88200;
C = textscan(fileID,'%s');
fclose(fileID);
for i=1:N
A=imread(C{1}{i});
% convert the file to vector
B=A(:);
% convert the above vector to a row
D=B';
% divide by 256
%E=double(D)/double(256);
E=single(D)/single(256);
dlmwrite('vectorized_image_dataset.txt',E,'-append');
clear A;clear B;clear D;clear E;
end
def read_line_by_line(file_path: str):
with open(filepath) as file:
for line in file:
yield line
也许这个函数对你有帮助——我对Numpy/Pandas不是很熟悉,但你似乎想一次加载所有数据并将其存储在内存中。使用上面的函数,您将使用生成器一次只生成一行——无需将所有内容都存储在 RAM 中。
我使用 pandas.read_csv 解决了它。我将 data.txt 分成四段,每段 22050 行。然后我做了
tp1 = read_csv(filepath_or_buffer='data_first_22050.txt', header=None, iterator=True, chunksize=1000)
df1 = concat(tp1, ignore_index=True)
tp2 = read_csv(filepath_or_buffer='data_second_22050.txt', header=None, iterator=True, chunksize=1000)
df2 = concat(tp2, ignore_index=True)>>> frames=[df1,df2]
result=concat(frames)
del frames, df1, df2, tp1, tp2
tp3 = read_csv(filepath_or_buffer='data_third_22050.txt', header=None, iterator=True, chunksize=1000)
df3 = concat(tp3, ignore_index=True)
frames=[result,df3]
result2=concat(frames)
del frames, df3, tp3, result
tp4 = read_csv(filepath_or_buffer='data_fourth_22050.txt', header=None, iterator=True, chunksize=1000)
df4 = concat(tp4, ignore_index=True)
frames=[result2,df4]
result3=concat(frames)
del frames, tp4, df4, result2
A=result3.as_matrix()
A.shape
(88200, 29403)
我的数据是 88200(行)*29403(列)(大约 14Gb)。数据已使用 dlmwrite 在 matlab 中创建。我尝试过使用以下方法读取python中的文件。在所有尝试中 我 运行 内存不足 :
我的系统:ubuntu 16.04,RAM 32Gb,Swap 20Gb Python 2.7.12,pandas:0.19,海湾合作委员会 5.4.0
1> 使用 csv:
import csv
import numpy
filename = 'data.txt'
raw_data = open(filename, 'rb')
reader = csv.reader(raw_data, delimiter=',', quoting=csv.QUOTE_NONE)
x = list(reader)
data = numpy.array(x).astype('float')
2a> 使用 numpy loadtxt:
import numpy
filename = 'data.txt'
raw_data = open(filename, 'rb')
data = numpy.loadtxt(raw_data, delimiter=",")
2b> 使用 numpy genfromtxt:
import numpy
x=np.genfromtxt('vectorized_image_dataset.txt',skip_header=0,skip_footer=0,delimiter=',',dtype='float32')
3> 使用 pandas.read_csv:
from pandas import *
import numpy as np
tp = read_csv(filepath_or_buffer='data.txt', header=None, iterator=True, chunksize=1000)
df = concat(tp, ignore_index=True)
在上述所有方法中它 运行 内存不足。
已使用 dlmwrite (matlab) 创建数据文件。图像列表(list.txt)被一张一张读取,转换为浮点数,矢量化并使用 dlmwrite 存储。代码如下:
fileID = fopen('list.txt');
N=88200;
C = textscan(fileID,'%s');
fclose(fileID);
for i=1:N
A=imread(C{1}{i});
% convert the file to vector
B=A(:);
% convert the above vector to a row
D=B';
% divide by 256
%E=double(D)/double(256);
E=single(D)/single(256);
dlmwrite('vectorized_image_dataset.txt',E,'-append');
clear A;clear B;clear D;clear E;
end
def read_line_by_line(file_path: str):
with open(filepath) as file:
for line in file:
yield line
也许这个函数对你有帮助——我对Numpy/Pandas不是很熟悉,但你似乎想一次加载所有数据并将其存储在内存中。使用上面的函数,您将使用生成器一次只生成一行——无需将所有内容都存储在 RAM 中。
我使用 pandas.read_csv 解决了它。我将 data.txt 分成四段,每段 22050 行。然后我做了
tp1 = read_csv(filepath_or_buffer='data_first_22050.txt', header=None, iterator=True, chunksize=1000)
df1 = concat(tp1, ignore_index=True)
tp2 = read_csv(filepath_or_buffer='data_second_22050.txt', header=None, iterator=True, chunksize=1000)
df2 = concat(tp2, ignore_index=True)>>> frames=[df1,df2]
result=concat(frames)
del frames, df1, df2, tp1, tp2
tp3 = read_csv(filepath_or_buffer='data_third_22050.txt', header=None, iterator=True, chunksize=1000)
df3 = concat(tp3, ignore_index=True)
frames=[result,df3]
result2=concat(frames)
del frames, df3, tp3, result
tp4 = read_csv(filepath_or_buffer='data_fourth_22050.txt', header=None, iterator=True, chunksize=1000)
df4 = concat(tp4, ignore_index=True)
frames=[result2,df4]
result3=concat(frames)
del frames, tp4, df4, result2
A=result3.as_matrix()
A.shape
(88200, 29403)