{"cells":[{"metadata":{},"cell_type":"markdown","source":"Kflod spliting keeping entries of the same image at the same fold"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from sklearn import model_selection\nimport pandas as pd\n\nDIR_INPUT = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection'\nDIR_TRAIN = f'{DIR_INPUT}/train'\ntrain_df = pd.read_csv(f'{DIR_INPUT}/train.csv')\n\ntrain_df.fillna(0, inplace=True)\ntrain_df.loc[train_df[\"class_id\"] == 14, ['x_max', 'y_max']] = 1.0\n\nunique_images = train_df[\"image_id\"].unique()\ndf_split = pd.DataFrame(unique_images, columns = ['unique_images']) \n\n\n\ndf_split[\"kfold\"] = -1\ndf_split = df_split.sample(frac=1).reset_index(drop=True)\ny = df_split.unique_images.values\n\nkf = model_selection.GroupKFold(n_splits=5)\n\nfor f, (t_, v_) in enumerate(kf.split(X=df_split, y=y, groups=df_split.unique_images.values)):\n    df_split.loc[v_, \"kfold\"] = f\ndf_split\n# For now during training we are feeding an image with all the anotated boxes of that image as one input to the model.\n# So makes sense to have same \"image id\" should have same fold.\n\ntrain_df[\"kfold\"] = -1\nfor ind in train_df.index: \n     train_df[\"kfold\"][ind] = df_split.loc[ df_split[\"unique_images\"] ==  train_df[\"image_id\"][ind]][\"kfold\"]\n    \n\ntrain_df.set_index('image_id', inplace=True)\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}