{"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":"markdown","source":"**Apache Arrow is a development platform for in-memory analytics. It contains a set of technologies that enable big data systems to store, process and move data fast.**","metadata":{}},{"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)\nimport pyarrow as pa\nimport pyarrow.parquet as pq\nimport pickle\n#Apache Arrow is a development platform for in-memory analytics. \n#It contains a set of technologies that enable big data systems to store, process and move data\n#fast\nimport scipy\nimport time\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-09-14T20:56:51.967965Z","iopub.execute_input":"2022-09-14T20:56:51.968425Z","iopub.status.idle":"2022-09-14T20:56:52.033091Z","shell.execute_reply.started":"2022-09-14T20:56:51.968292Z","shell.execute_reply":"2022-09-14T20:56:52.032402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/amex-default-prediction/train_data.csv'\ntest_path = '/kaggle/input/amex-default-prediction/test_data.csv'\nparquet_file_train = 'train_amex.parquet'\nparquet_file_test = 'test_amex.parquet'\npickle_file_train = 'train_amex_pickle.pkl'\npickle_file_test = 'test_amex_pickle.pkl'\ntrain = pd.read_csv(train_path, nrows=1)\nfeatures = train.columns.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:56:55.071221Z","iopub.execute_input":"2022-09-14T20:56:55.071568Z","iopub.status.idle":"2022-09-14T20:56:55.106262Z","shell.execute_reply.started":"2022-09-14T20:56:55.071531Z","shell.execute_reply":"2022-09-14T20:56:55.105204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_rows', None)\nfeatures","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:56:55.719281Z","iopub.execute_input":"2022-09-14T20:56:55.721983Z","iopub.status.idle":"2022-09-14T20:56:55.737832Z","shell.execute_reply.started":"2022-09-14T20:56:55.721932Z","shell.execute_reply":"2022-09-14T20:56:55.736714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n#### categorical variables\n['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n\n* B_30 -> uint8   (0 to 2)\n* B_38 -> uint8   (1 to 7)\n* D_114 -> binary (0 or 1)\n* D_116 -> binary (0 or 1)\n* D_117 -> int8   (-1 to 7)\n* D_120 -> binary (0 or 1)\n* D_126 -> int8 (-1 to 1)\n* D_63 -> object\n* D_64 -> object\n* D_66 -> binary (proper) (1) has 70% missing value\n* D_68 -> unint8 (1 to 6)\n\n**Other columns** \n* customer_ID - object\n* S_2 - object\n* B_31(int64) - binary\n\n**Rest all columns change float64 to float32**\n","metadata":{}},{"cell_type":"code","source":"dtypes = {'customer_ID': 'str',\n       'S_2': 'str',\n       'D_63': 'str',\n       'D_64': 'str'\n                  }\nfor col in features:\n    if col not in dtypes.keys():\n        dtypes[col] = 'float32'","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:57:04.477666Z","iopub.execute_input":"2022-09-14T20:57:04.477973Z","iopub.status.idle":"2022-09-14T20:57:04.483505Z","shell.execute_reply.started":"2022-09-14T20:57:04.477943Z","shell.execute_reply":"2022-09-14T20:57:04.482442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Why float32 is better than float16, check it out here**\nhttps://discourse.julialang.org/t/massive-performance-penalty-for-float16-compared-to-float32/6864/3","metadata":{}},{"cell_type":"code","source":"def write_parquet(path, feats, save_loc, chunk_size=100000):\n    schema = ''\n    writer = ''\n    if len(feats) > 0:\n        for i,chunk in enumerate(pd.read_csv(path, dtype=dtypes, iterator=True, chunksize=100000)):\n            if i == 0:\n                schema = pa.Table.from_pandas(df=chunk).schema\n                writer = pq.ParquetWriter(save_loc, schema, compression='snappy')\n            table = pa.Table.from_pandas(chunk, schema=schema)\n            writer.write_table(table)\n    return None","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:57:09.364321Z","iopub.execute_input":"2022-09-14T20:57:09.364634Z","iopub.status.idle":"2022-09-14T20:57:09.372059Z","shell.execute_reply.started":"2022-09-14T20:57:09.364602Z","shell.execute_reply":"2022-09-14T20:57:09.371152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef write_pickle(path, feats,save_loc, chunck_size = 100000):\n    with open(save_loc, 'wb') as f:\n        for i,chunk in enumerate(pd.read_csv(path, dtype=dtypes, iterator=True, chunksize=100000)):\n            pickle.dump(chunk, f)\n                ","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:57:10.450968Z","iopub.execute_input":"2022-09-14T20:57:10.452215Z","iopub.status.idle":"2022-09-14T20:57:10.459001Z","shell.execute_reply.started":"2022-09-14T20:57:10.452169Z","shell.execute_reply":"2022-09-14T20:57:10.457859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_train = time.time()\nwrite_parquet(train_path, features, parquet_file_train)\nend_train = time.time()\nstart_test = time.time()\nwrite_parquet(test_path, features, parquet_file_test)\nend_test = time.time()\nminutes, seconds = divmod(end_train-start_train, 60)\nprint(f\"Time taken for converting train.csv to parquet: {minutes} minutes and {seconds} seconds....\")\nminutes_test, seconds_test = divmod(end_test-start_test, 60)\nprint(f\"Time taken for converting train.csv to parquet: {minutes_test} minutes and {seconds_test} seconds....\")","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:51:37.100764Z","iopub.execute_input":"2022-09-14T19:51:37.101696Z","iopub.status.idle":"2022-09-14T20:16:24.055283Z","shell.execute_reply.started":"2022-09-14T19:51:37.101631Z","shell.execute_reply":"2022-09-14T20:16:24.052080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%%time\n#write_pickle(train_path,features,pickle_file_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:32:34.399931Z","iopub.execute_input":"2022-09-14T20:32:34.400264Z","iopub.status.idle":"2022-09-14T20:39:03.705775Z","shell.execute_reply.started":"2022-09-14T20:32:34.400228Z","shell.execute_reply":"2022-09-14T20:39:03.702836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%%time\n#write_pickle(test_path,features,pickle_file_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T20:57:17.858275Z","iopub.execute_input":"2022-09-14T20:57:17.859239Z","iopub.status.idle":"2022-09-14T21:09:37.204343Z","shell.execute_reply.started":"2022-09-14T20:57:17.859176Z","shell.execute_reply":"2022-09-14T21:09:37.202961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}