【发布时间】:2021-05-07 14:09:14
【问题描述】:
我们正在尝试加载 IDS-2018 dataset,它由 10 个 CSV 文件组成,总大小为 6.4 GB。当我们尝试在 32GB RAM 服务器中连接所有 CSV 文件时,它崩溃了(进程被终止)。
我们甚至尝试通过使用优化 pandas 数据帧中的存储空间,
def reduce_mem_usage(df):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
return df
但是没有用。服务器在连接每个 CSV 文件时仍然崩溃。我们使用pd.concat 连接了每个文件。整个代码是here。如何做到这一点,以便我们可以做进一步的处理?
【问题讨论】:
-
您的服务器上是否有任何限制特定进程的内存使用的限制?我想知道是否不是机器内存不足,而是操作系统杀死了进程,因为它不会让它拥有更多
-
我不认为有任何这样的限制,顺便说一句,Kaggle 内核也崩溃了。
-
reduce_mem_usage不起作用,因为您只转换列的值,并将新值分配给同一内存。 float64 和 float32 的主要区别不是数字范围而是精度。 -
@Daniel 如果可能的话,你能说一下这个问题的任何解决方法吗?不使用的时候不是python的垃圾回收器释放的内存吗?
标签: python pandas machine-learning out-of-memory