我有一个非常大的数据集:7.9 GB的CSV文件。其中80%作为训练数据,其余20%作为测试数据。加载训练数据(6.2 GB)时,在第80次迭代(第8​​0个文件)中具有MemoryError。这是我用于加载数据的脚本:

import pandas as pd
import os

col_names = ['duration', 'service', 'src_bytes', 'dest_bytes', 'count', 'same_srv_rate',
        'serror_rate', 'srv_serror_rate', 'dst_host_count', 'dst_host_srv_count',
        'dst_host_same_src_port_rate', 'dst_host_serror_rate', 'dst_host_srv_serror_rate',
        'flag', 'ids_detection', 'malware_detection', 'ashula_detection', 'label', 'src_ip_add',
        'src_port_num', 'dst_ip_add', 'dst_port_num', 'start_time', 'protocol']

# create a list to store the filenames
files = []

# create a dataframe to store the contents of CSV files
df = pd.DataFrame()

# get the filenames in the specified PATH
for (dirpath, dirnames, filenames) in os.walk(path):
    ''' Append to the list the filenames under the subdirectories of the <path> '''
    files.extend(os.path.join(dirpath, filename) for filename in filenames)

for file in files:
    df = df.append(pd.read_csv(filepath_or_buffer=file, names=col_names, engine='python'))
    print('Appending file : {file}'.format(file=files[index]))

pd.set_option('display.max_colwidth', -1)
print(df)


6.2 GB的CSV文件中有130个文件。

最佳答案

对于大型数据集-我们可能已经算出6.2GB了-一次读取所有数据可能不是最好的主意。无论如何,您将要逐批训练网络,仅加载需要用于下一步的数据就足够了。

tensorflow documentation很好地概述了如何实现数据读取管道。根据所链接文档的阶段为:


  
  文件名列表
  可选的文件名混排
  可选的时期限制
  文件名队列
  用于文件格式的阅读器
  读取器读取记录的解码器
  可选的预处理
  队列示例

07-24 09:53
查看更多