{"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\nimport numpy as np\nfrom sklearn.model_selection import StratifiedGroupKFold,GroupKFold,StratifiedGroupKFold","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-06T18:22:13.104745Z","iopub.execute_input":"2022-07-06T18:22:13.105227Z","iopub.status.idle":"2022-07-06T18:22:13.110932Z","shell.execute_reply.started":"2022-07-06T18:22:13.105189Z","shell.execute_reply":"2022-07-06T18:22:13.109684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_seed = 42\nn_fold = 5\ndf = pd.read_csv(\"../input/feedback-prize-effectiveness/train.csv\")\ndf['grp_var'] = df['discourse_type'] + df['discourse_effectiveness']\n\ncv = StratifiedGroupKFold(n_splits=n_fold)\nidxs = np.arange(len(df))\nfolds = list(cv.split(idxs,df.grp_var,df.essay_id))\ndef get_val_fold(folds,fold_num):\n    trn,val = folds[fold_num]\n    return val\nfor fold in range(n_fold):\n    val_idx = get_val_fold(folds,fold)\n    df.loc[val_idx,'kfold'] = fold\ndf.drop(['grp_var'],axis=1,inplace = True)\ndf.to_csv(\"./\" + f'{n_fold}fold.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:22:13.159335Z","iopub.execute_input":"2022-07-06T18:22:13.159722Z","iopub.status.idle":"2022-07-06T18:22:13.164788Z","shell.execute_reply.started":"2022-07-06T18:22:13.159691Z","shell.execute_reply":"2022-07-06T18:22:13.163619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''checks'''\nx=0\nfor i in range(n_fold):\n    x += df[df['kfold']==i]['essay_id'].nunique()\nprint(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:25:54.028028Z","iopub.execute_input":"2022-07-06T18:25:54.028928Z","iopub.status.idle":"2022-07-06T18:25:54.055819Z","shell.execute_reply.started":"2022-07-06T18:25:54.028885Z","shell.execute_reply":"2022-07-06T18:25:54.054485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}