{"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for 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-11-29T13:49:07.44311Z","iopub.execute_input":"2022-11-29T13:49:07.444396Z","iopub.status.idle":"2022-11-29T13:49:07.467336Z","shell.execute_reply.started":"2022-11-29T13:49:07.444293Z","shell.execute_reply":"2022-11-29T13:49:07.466469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The data is imbalanced and we have multiple entries per patient. Therefore the following fold creation code may be appropriate.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn import model_selection\nimport pathlib\n\n\ndef create_kfold(train_csv_file, train_csv_with_fold_file):\n    df = pd.read_csv(train_csv_file)\n    gkf = model_selection.GroupKFold(n_splits=5)\n    df['fold']=-1\n    for group, df_group in df.groupby('cancer'):\n        indices = list(df_group.index)\n        for f, (t, v) in enumerate(gkf.split(X=df_group, groups=df_group.patient_id)):\n            the_indices = np.array(indices)[v]\n            df.loc[the_indices, 'fold']=f\n    df.to_csv(train_csv_with_fold_file, index=False)\n\n    \npath = pathlib.Path(\"/kaggle/input/rsna-breast-cancer-detection\")\ncreate_kfold(path / \"train.csv\", \"train_kfold.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:53:25.795806Z","iopub.execute_input":"2022-11-29T13:53:25.796201Z","iopub.status.idle":"2022-11-29T13:53:26.153614Z","shell.execute_reply.started":"2022-11-29T13:53:25.796164Z","shell.execute_reply":"2022-11-29T13:53:26.152602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"train_kfold.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:53:56.889302Z","iopub.execute_input":"2022-11-29T13:53:56.889708Z","iopub.status.idle":"2022-11-29T13:53:56.946166Z","shell.execute_reply.started":"2022-11-29T13:53:56.889675Z","shell.execute_reply":"2022-11-29T13:53:56.944888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby(['fold', 'cancer'])['patient_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:54:25.169293Z","iopub.execute_input":"2022-11-29T13:54:25.169681Z","iopub.status.idle":"2022-11-29T13:54:25.192972Z","shell.execute_reply.started":"2022-11-29T13:54:25.169643Z","shell.execute_reply":"2022-11-29T13:54:25.191803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example usage below","metadata":{}},{"cell_type":"code","source":"train_df = df.loc[lambda d: d.fold != 0, :]\neval_df = df.loc[lambda d: d.fold == 0, :]","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:56:15.451925Z","iopub.execute_input":"2022-11-29T13:56:15.452278Z","iopub.status.idle":"2022-11-29T13:56:15.465762Z","shell.execute_reply.started":"2022-11-29T13:56:15.452237Z","shell.execute_reply":"2022-11-29T13:56:15.464555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:56:53.786693Z","iopub.execute_input":"2022-11-29T13:56:53.787062Z","iopub.status.idle":"2022-11-29T13:56:53.794329Z","shell.execute_reply.started":"2022-11-29T13:56:53.787032Z","shell.execute_reply":"2022-11-29T13:56:53.793039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-29T13:56:59.651394Z","iopub.execute_input":"2022-11-29T13:56:59.651766Z","iopub.status.idle":"2022-11-29T13:56:59.658524Z","shell.execute_reply.started":"2022-11-29T13:56:59.651734Z","shell.execute_reply":"2022-11-29T13:56:59.65757Z"},"trusted":true},"execution_count":null,"outputs":[]}]}