场景:很多时候,我们需要读取多个文件数据,导致内存增大,甚至报错。
解决方案,在我们读取之后,做一些数据类型的判断,减少内存占用:
import pandas as pd
import numpy as np
from tqdm import tqdm
tqdm.pandas()
# 压缩内存函数,文件大小没变化,占用内存减小
def reduce_mem_usage(df):
start_memory = df.memory_usage().sum() / 1024 ** 2
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
for col in tqdm(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 :", round(end_mem,3))
print((start_memory - end_mem) / start_memory * 100)
return df
以上代码的核心思路是将数据类型做匹配。
从一本机器学习竞赛的书中看到的,觉得很不错,在公司的比赛中,还帮到了我。分享给大家~
其中tqdm库是用来显示读取的进度,很方便,有兴趣的可以了解一下。
需要注意的是,这里只是数据读入内存之后的大小变化了,但是文件本身是没有变化的。