{"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 numpy as np\nimport pandas as pd \nfrom sklearn.model_selection import StratifiedKFold\n\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-10T02:09:38.625903Z","iopub.execute_input":"2022-02-10T02:09:38.626594Z","iopub.status.idle":"2022-02-10T02:09:38.631170Z","shell.execute_reply.started":"2022-02-10T02:09:38.626553Z","shell.execute_reply":"2022-02-10T02:09:38.630208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"../input/happy-whale-and-dolphin/train.csv\"\nNFOLD = 5\nTARGET = \"species\"\nPROJECT_NAME = \"happy_whale_and_dolphin\"","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:38.632637Z","iopub.execute_input":"2022-02-10T02:09:38.633431Z","iopub.status.idle":"2022-02-10T02:09:38.650044Z","shell.execute_reply.started":"2022-02-10T02:09:38.633383Z","shell.execute_reply":"2022-02-10T02:09:38.649289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(TRAIN_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:38.651900Z","iopub.execute_input":"2022-02-10T02:09:38.652616Z","iopub.status.idle":"2022-02-10T02:09:38.728507Z","shell.execute_reply.started":"2022-02-10T02:09:38.652564Z","shell.execute_reply":"2022-02-10T02:09:38.727469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def makeNFoldCSV(df,target_col,project_name,nFold=5,folder_col_name=\"Fold\",seed=2021,shuffle=True):\n    skf = StratifiedKFold(n_splits=nFold,random_state=seed, shuffle=shuffle)\n    skfSplit = skf.split(df, df[target_col])\n    fold = 1\n    for train_index, test_index in skfSplit:\n        df.loc[test_index,folder_col_name] = fold\n        fold = fold + 1\n\n    df = df.astype({folder_col_name: 'int64'})\n    csv_name = project_name + \"_\" + str(nFold) + \"folds.csv\"\n    df.to_csv(csv_name,index = False)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:38.730329Z","iopub.execute_input":"2022-02-10T02:09:38.730563Z","iopub.status.idle":"2022-02-10T02:09:38.737493Z","shell.execute_reply.started":"2022-02-10T02:09:38.730535Z","shell.execute_reply":"2022-02-10T02:09:38.736862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"makeNFoldCSV(train,TARGET,PROJECT_NAME,NFOLD)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:38.738724Z","iopub.execute_input":"2022-02-10T02:09:38.739476Z","iopub.status.idle":"2022-02-10T02:09:39.056372Z","shell.execute_reply.started":"2022-02-10T02:09:38.739406Z","shell.execute_reply":"2022-02-10T02:09:39.055700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NEW_CSV_FILE_PATH = PROJECT_NAME+ \"_\" + str(NFOLD) +\"folds.csv\"\nfold_csv = pd.read_csv(NEW_CSV_FILE_PATH)\nfold_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:39.057721Z","iopub.execute_input":"2022-02-10T02:09:39.058221Z","iopub.status.idle":"2022-02-10T02:09:39.136466Z","shell.execute_reply.started":"2022-02-10T02:09:39.058177Z","shell.execute_reply":"2022-02-10T02:09:39.135622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getFoldCount(df,col=\"Fold\"):\n    return sorted([( colValue,len(train.loc[df[col] == colValue]) ) for colValue in df[col].unique()])\n\ngetFoldCount(fold_csv)\nax = sns.countplot(data=fold_csv,y=\"Fold\")","metadata":{"execution":{"iopub.status.busy":"2022-02-10T02:09:39.137945Z","iopub.execute_input":"2022-02-10T02:09:39.138414Z","iopub.status.idle":"2022-02-10T02:09:39.345218Z","shell.execute_reply.started":"2022-02-10T02:09:39.138371Z","shell.execute_reply":"2022-02-10T02:09:39.344332Z"},"trusted":true},"execution_count":null,"outputs":[]}]}