{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-13T09:27:32.999074Z","iopub.execute_input":"2022-06-13T09:27:33.001227Z","iopub.status.idle":"2022-06-13T09:27:33.040352Z","shell.execute_reply.started":"2022-06-13T09:27:33.001114Z","shell.execute_reply":"2022-06-13T09:27:33.039499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AMEX Competition - Reduce data size by optimal dtypes\n\nWe will use the parquet format of the dataset created by @odins0n for some data exploration. Parquet format is faster, more compressed, and saves the dtypes of each column when we read and write.","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport pandas as pd\nimport numpy as np\n\n\nTRAIN_FILE = \"/kaggle/input/amex-parquet/train_data.parquet\"\nTEST_FILE = \"/kaggle/input/amex-parquet/test_data.parquet\"","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:27:33.041720Z","iopub.execute_input":"2022-06-13T09:27:33.042000Z","iopub.status.idle":"2022-06-13T09:27:33.046220Z","shell.execute_reply.started":"2022-06-13T09:27:33.041975Z","shell.execute_reply":"2022-06-13T09:27:33.045351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Total train data size is ~4GB","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_parquet(TRAIN_FILE)\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:27:40.818256Z","iopub.execute_input":"2022-06-13T09:27:40.818706Z","iopub.status.idle":"2022-06-13T09:28:19.271646Z","shell.execute_reply.started":"2022-06-13T09:27:40.818673Z","shell.execute_reply":"2022-06-13T09:28:19.270366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68','target']\ndate_cols = ['S_2']\nnumeric_cols = set(train_df.columns) - set(date_cols) - set(categorical_cols) - set(['customer_ID'])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:33:18.979499Z","iopub.execute_input":"2022-06-13T09:33:18.980319Z","iopub.status.idle":"2022-06-13T09:33:18.985486Z","shell.execute_reply.started":"2022-06-13T09:33:18.980264Z","shell.execute_reply":"2022-06-13T09:33:18.984617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a data frame with numerical data types and their ranges","metadata":{}},{"cell_type":"code","source":"num_types = [np.int8, np.int16, np.int32, np.int64,\n             np.uint8, np.uint16, np.uint32, np.uint64, \n             np.float16, np.float32, np.float64, np.float128]\nnum_types = [[np_type.__name__\n              , 'integer' if np.issubdtype(np_type, np.integer) else 'float'] \n             for np_type in num_types]\ntypes_df = pd.DataFrame(data=num_types, columns=['class_type','class_subtype'])\ntypes_df['min_value'] = types_df.apply(lambda row: np.iinfo(row.class_type).min \n                                       if row.class_subtype == 'integer' \n                                       else np.finfo(row.class_type).min, axis=1)\ntypes_df['max_value'] = types_df.apply(lambda row: np.iinfo(row.class_type).max \n                                       if row.class_subtype == 'integer' \n                                       else np.finfo(row.class_type).max, axis=1)\n\ntypes_df['range'] = types_df['max_value'] - types_df['min_value']\ntypes_df.sort_values(by='range', inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:33:21.913225Z","iopub.execute_input":"2022-06-13T09:33:21.913806Z","iopub.status.idle":"2022-06-13T09:33:21.927443Z","shell.execute_reply.started":"2022-06-13T09:33:21.913761Z","shell.execute_reply":"2022-06-13T09:33:21.926575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"schema = {}\n\nfor col in numeric_cols:\n    col_min = train_df[col].min()\n    col_max = train_df[col].max()\n    col_subtype = 'float'\n    if np.issubdtype(train_df[col].dtype, np.integer):\n        col_subtype = 'integer'\n\n    temp = types_df[(types_df['min_value'] <= col_min) \n                    & (types_df['max_value'] >= col_max)\n                    & (types_df['class_subtype'] == col_subtype)\n                   ]\n    optimized_class = temp.loc[temp['range'].idxmin(), 'class_type']\n    schema[col] = optimized_class\nfor col in categorical_cols:\n    schema[col] = 'category'\nfor col in date_cols:\n    schema[col] = 'datetime64[ns]'","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:33:55.351287Z","iopub.execute_input":"2022-06-13T09:33:55.351692Z","iopub.status.idle":"2022-06-13T09:34:02.287448Z","shell.execute_reply.started":"2022-06-13T09:33:55.351653Z","shell.execute_reply":"2022-06-13T09:34:02.286650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert dataframe to optimal dtypes","metadata":{}},{"cell_type":"code","source":"train_df = train_df.astype(schema)\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:35:00.301906Z","iopub.execute_input":"2022-06-13T09:35:00.302649Z","iopub.status.idle":"2022-06-13T09:35:09.342711Z","shell.execute_reply.started":"2022-06-13T09:35:00.302611Z","shell.execute_reply":"2022-06-13T09:35:09.341708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train dataset size reduced to 2GB","metadata":{}},{"cell_type":"markdown","source":"Saving this dataframe to parquet fails as parquet files do not support 'float16'.\nSee: https://issues.apache.org/jira/browse/PARQUET-1647","metadata":{}},{"cell_type":"markdown","source":"Using float16 dtype with Pandas isn't recommended, but we'll set this type to be able to read data easily then we can adjust when manipulating the dataset.\n\nGithub Issue for float16 with Pandas: https://github.com/pandas-dev/pandas/issues/9220","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}