afa*_*rap 2 machine-learning dataset data-processing pandas tensorflow
我有一个非常大的数据集:7.9 GB的CSV文件。其中80%作为培训数据,其余20%作为测试数据。当我加载训练数据(6.2 GB)时,我MemoryError处于第80次迭代(第80个文件)。这是我用于加载数据的脚本:
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)
Run Code Online (Sandbox Code Playgroud)
6.2 GB的CSV文件中有130个文件。
我支持 Nyps 的回答,我只是没有足够的声誉来添加评论。此外,打开任务管理器或等效程序并在运行时观察系统的已用内存可能会很有趣。我猜当你的 RAM 完全填满时,那就是你收到错误的时候。
TensorFlow 支持队列,它允许您一次只读取部分数据,以免耗尽您的内存。这方面的例子在 Nyps 链接的文档中。此外,TensorFlow 最近在TensorFlow Dataset docs 中添加了一种处理输入数据集的新方法。
此外,我建议将您的所有数据转换为 TensorFlow 的 TFRecord 格式,因为与在训练时将 CSV 文件转换为张量相比,它可以节省空间,并且可以将数据访问速度提高 100 倍以上。
对于大型数据集-我们可能已经算出6.2GB了-一次读取所有数据可能不是最好的主意。无论如何,您将要逐批训练网络,仅加载需要用于下一步的数据就足够了。
该tensorflow文档提供了有关如何实现数据读取管道一个很好的概述。根据所链接文档的阶段为:
- 文件名列表
- 可选的文件名混排
- 可选的时期限制
- 文件名队列
- 用于文件格式的阅读器
- 读取器读取记录的解码器
- 可选的预处理
- 队列示例