{"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":"<a id=\"table\"></a>\n<h1 style=\"background-color:lightpink;font-family:newtimeroman;font-size:350%;text-align:center;border-radius: 15px 50px;\">Table of Content</h1>\n\n* [1. IMPORTING LIBRARIES](#1)\n\n* [2. CONFIG](#2)    \n\n* [3. LOADING DATASET](#3)\n  \n* [4. STRATIFIED GROUP K-FOLD](#4)\n\n* [5. SAVE DATASET](#5)\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Importing Libraries</p>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import StratifiedGroupKFold","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:38.179094Z","iopub.execute_input":"2022-11-29T21:22:38.179527Z","iopub.status.idle":"2022-11-29T21:22:38.71205Z","shell.execute_reply.started":"2022-11-29T21:22:38.179434Z","shell.execute_reply":"2022-11-29T21:22:38.71081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Config</p>","metadata":{}},{"cell_type":"code","source":"class CFG:\n    n_splits=5\n    shuffle=True\n    random_state=42\n    base_path=\"../input/rsna-breast-cancer-detection/\"","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:38.713362Z","iopub.execute_input":"2022-11-29T21:22:38.713693Z","iopub.status.idle":"2022-11-29T21:22:38.720342Z","shell.execute_reply.started":"2022-11-29T21:22:38.713663Z","shell.execute_reply":"2022-11-29T21:22:38.719144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Loading Dataset</p>","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(f\"{CFG.base_path}/train.csv\")\n\nprint(f\"Shape train_merge = {train_data.shape}\")\ntrain_data.sample(3)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:38.721983Z","iopub.execute_input":"2022-11-29T21:22:38.722581Z","iopub.status.idle":"2022-11-29T21:22:38.831716Z","shell.execute_reply.started":"2022-11-29T21:22:38.722529Z","shell.execute_reply":"2022-11-29T21:22:38.83047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Stratified Group K-Fold</p>","metadata":{}},{"cell_type":"code","source":"train_data['fold']= -1\ncv = StratifiedGroupKFold(n_splits=CFG.n_splits, shuffle=CFG.shuffle, random_state=CFG.random_state)\n\nfor fold, (_, valid_idx) in enumerate(cv.split(X=train_data[\"image_id\"], y=train_data[\"cancer\"], groups=train_data[\"patient_id\"])):\n    train_data.loc[valid_idx, \"fold\"] = fold\n    \nassert train_data.groupby(['fold', 'cancer']).size().sum() == train_data.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:38.835874Z","iopub.execute_input":"2022-11-29T21:22:38.836301Z","iopub.status.idle":"2022-11-29T21:22:43.653625Z","shell.execute_reply.started":"2022-11-29T21:22:38.836267Z","shell.execute_reply":"2022-11-29T21:22:43.652388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the result\ntrain_data.groupby(['fold', 'cancer']).size()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:43.655374Z","iopub.execute_input":"2022-11-29T21:22:43.655731Z","iopub.status.idle":"2022-11-29T21:22:43.670061Z","shell.execute_reply.started":"2022-11-29T21:22:43.655699Z","shell.execute_reply":"2022-11-29T21:22:43.66872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:43.671747Z","iopub.execute_input":"2022-11-29T21:22:43.672212Z","iopub.status.idle":"2022-11-29T21:22:43.696582Z","shell.execute_reply.started":"2022-11-29T21:22:43.672182Z","shell.execute_reply":"2022-11-29T21:22:43.695324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <p style=\"padding:10px;background-color:lightpink;margin:0;color:black;font-family:newtimeroman;font-size:100%;text-align:center;border-radius: 15px 50px;overflow:hidden;font-weight:500\">Save Dataset</p>","metadata":{}},{"cell_type":"code","source":"# Save updated dataset\ntrain_data.to_csv(f'{CFG.n_splits}_folds_data.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:22:59.95589Z","iopub.execute_input":"2022-11-29T21:22:59.956416Z","iopub.status.idle":"2022-11-29T21:23:00.173808Z","shell.execute_reply.started":"2022-11-29T21:22:59.956374Z","shell.execute_reply":"2022-11-29T21:23:00.172389Z"},"trusted":true},"execution_count":null,"outputs":[]}]}