{"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\n\nfrom sklearn.model_selection import StratifiedGroupKFold\nfrom typing import Counter","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-03T14:19:49.99775Z","iopub.execute_input":"2023-10-03T14:19:49.998175Z","iopub.status.idle":"2023-10-03T14:19:50.003706Z","shell.execute_reply.started":"2023-10-03T14:19:49.998143Z","shell.execute_reply":"2023-10-03T14:19:50.002401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\n\n\ndef prepare_folds(data_path=\"../input/\", k=4):\n    \"\"\"\n    Prepare data folds for cross-validation.\n    MultilabelStratifiedKFold is used.\n\n    Args:\n        data_path (str, optional): Path to the data directory. Defaults to \"../input/\".\n        k (int, optional): Number of cross-validation folds. Defaults to 4.\n\n    Returns:\n        pandas DataFrame: DataFrame containing the patient IDs and their respective fold assignments.\n    \"\"\"\n    cols = [\n        'bowel_injury', 'extravasation_injury', 'kidney_low',\n        'kidney_high', 'liver_low', 'liver_high', 'spleen_low', 'spleen_high'\n    ]\n\n    df = pd.read_csv(data_path + \"train.csv\")\n\n    from iterstrat.ml_stratifiers import MultilabelStratifiedKFold\n    mskf = MultilabelStratifiedKFold(n_splits=k, shuffle=True, random_state=42)\n    splits = mskf.split(df, y=df[cols])\n\n    df['fold'] = -1\n    for i, (_, val_idx) in enumerate(splits):\n        df.loc[val_idx, \"fold\"] = i\n\n    df_folds = df[[\"patient_id\", \"fold\"]]\n    df_folds.to_csv(data_path + f\"folds_{k}.csv\", index=False)\n    return df_folds\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T14:33:47.631916Z","iopub.execute_input":"2023-10-03T14:33:47.63237Z","iopub.status.idle":"2023-10-03T14:33:47.641113Z","shell.execute_reply.started":"2023-10-03T14:33:47.632323Z","shell.execute_reply":"2023-10-03T14:33:47.640025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T14:23:47.964602Z","iopub.execute_input":"2023-10-03T14:23:47.965047Z","iopub.status.idle":"2023-10-03T14:23:48.808946Z","shell.execute_reply.started":"2023-10-03T14:23:47.965014Z","shell.execute_reply":"2023-10-03T14:23:48.807753Z"},"trusted":true},"execution_count":null,"outputs":[]}]}