{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-28T18:06:55.544391Z","iopub.execute_input":"2022-05-28T18:06:55.545396Z","iopub.status.idle":"2022-05-28T18:06:55.573098Z","shell.execute_reply.started":"2022-05-28T18:06:55.545286Z","shell.execute_reply":"2022-05-28T18:06:55.572250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Class for efficient numerical columns compression","metadata":{}},{"cell_type":"code","source":"class MemoryReducer:\n    def __init__(self,\n                 float_min_type=16,\n                 int_min_type=8):\n        self.int8_min = np.iinfo(np.int8).min\n        self.int8_max = np.iinfo(np.int8).max\n        self.int16_min = np.iinfo(np.int16).min\n        self.int16_max = np.iinfo(np.int16).max\n        self.int32_min = np.iinfo(np.int32).min\n        self.int32_max = np.iinfo(np.int32).max\n\n        self.uint8_max = np.iinfo(np.uint8).max\n        self.uint16_max = np.iinfo(np.uint16).max\n        self.uint32_max = np.iinfo(np.uint32).max\n\n        self.float16_min = np.finfo(np.float16).min\n        self.float16_max = np.finfo(np.float16).max\n        self.float32_min = np.finfo(np.float32).min\n        self.float32_max = np.finfo(np.float32).max\n        self.__float_min_type = float_min_type\n        self.__int_min_type = int_min_type\n\n    def shrink_column(self,\n                      col):\n        is_int = col.dtypes.name[:3] == 'int'\n        is_uint = col.dtypes.name[:3] == 'uin'\n        is_float = col.dtypes.name[:3] == 'flo'\n\n        if is_int:\n            c_min = col.min()\n            c_max = col.max()\n            if self.__int_min_type <= 8 and c_min > self.int8_min and c_max < self.int8_max:\n                col = col.astype(np.int8)\n            elif self.__int_min_type <= 16 and c_min > self.int16_min and c_max < self.int16_max:\n                col = col.astype(np.int16)\n            elif self.__int_min_type <= 32 and c_min > self.int32_min and c_max < self.int32_max:\n                col = col.astype(np.int32)\n        elif is_uint:\n            c_max = col.max()\n            if self.__int_min_type <= 8 and c_max < self.uint8_max:\n                col = col.astype(np.int8)\n            elif self.__int_min_type <= 16 and c_max < self.uint16_max:\n                col = col.astype(np.int16)\n            elif self.__int_min_type <= 32 and c_max < self.uint32_max:\n                col = col.astype(np.int32)\n        elif is_float:\n            c_min = col.min()\n            c_max = col.max()\n            if self.__float_min_type <= 16 and c_min > self.float16_min and c_max < self.float16_max:\n                col = col.astype(np.float16)\n            elif self.__float_min_type <= 32 and c_min > self.float32_min and c_max < self.float32_max:\n                col = col.astype(np.float32)\n        return col\n\n    def reduce(self,\n               df):            \n        for col in df.columns:\n            df[col] = self.shrink_column(df[col])\n\n        return df","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:06:55.584983Z","iopub.execute_input":"2022-05-28T18:06:55.585667Z","iopub.status.idle":"2022-05-28T18:06:55.608324Z","shell.execute_reply.started":"2022-05-28T18:06:55.585626Z","shell.execute_reply":"2022-05-28T18:06:55.607142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read data by chunks and compress them","metadata":{}},{"cell_type":"code","source":"%%time\ndata = []\nchunksize = 10 ** 6\nwith pd.read_csv('/kaggle/input/amex-default-prediction/train_data.csv', chunksize=chunksize) as reader:\n    for chunk in reader:\n        data += [MemoryReducer().reduce(chunk)]\ndata = pd.concat(data)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:06:55.610224Z","iopub.execute_input":"2022-05-28T18:06:55.610899Z","iopub.status.idle":"2022-05-28T18:19:46.296581Z","shell.execute_reply.started":"2022-05-28T18:06:55.610855Z","shell.execute_reply":"2022-05-28T18:19:46.295341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save data in pickle format","metadata":{}},{"cell_type":"code","source":"%%time\ndata.to_pickle('train.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:19:46.298496Z","iopub.execute_input":"2022-05-28T18:19:46.298906Z","iopub.status.idle":"2022-05-28T18:19:52.143217Z","shell.execute_reply.started":"2022-05-28T18:19:46.298873Z","shell.execute_reply":"2022-05-28T18:19:52.142129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read data from pickle format","metadata":{}},{"cell_type":"code","source":"%%time\ndata = pd.read_pickle('train.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:19:52.144341Z","iopub.execute_input":"2022-05-28T18:19:52.144694Z","iopub.status.idle":"2022-05-28T18:19:55.512240Z","shell.execute_reply.started":"2022-05-28T18:19:52.144664Z","shell.execute_reply":"2022-05-28T18:19:55.511042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Memory usage after optimization is: {:.2f} GB'.format(data.memory_usage().sum() / 1024 ** 3))","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:19:55.514113Z","iopub.execute_input":"2022-05-28T18:19:55.515078Z","iopub.status.idle":"2022-05-28T18:19:55.540228Z","shell.execute_reply.started":"2022-05-28T18:19:55.515034Z","shell.execute_reply":"2022-05-28T18:19:55.539185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:22:58.071695Z","iopub.execute_input":"2022-05-28T18:22:58.073760Z","iopub.status.idle":"2022-05-28T18:23:00.292526Z","shell.execute_reply.started":"2022-05-28T18:22:58.073672Z","shell.execute_reply":"2022-05-28T18:23:00.291038Z"},"trusted":true},"execution_count":null,"outputs":[]}]}