{"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 os\nimport numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import KFold\nimport plotly.express as px","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-11T07:52:16.693902Z","iopub.execute_input":"2022-08-11T07:52:16.694347Z","iopub.status.idle":"2022-08-11T07:52:16.700572Z","shell.execute_reply.started":"2022-08-11T07:52:16.694311Z","shell.execute_reply":"2022-08-11T07:52:16.699389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/paddy-disease-classification/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:16.713018Z","iopub.execute_input":"2022-08-11T07:52:16.713414Z","iopub.status.idle":"2022-08-11T07:52:16.738779Z","shell.execute_reply.started":"2022-08-11T07:52:16.713377Z","shell.execute_reply":"2022-08-11T07:52:16.737805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val = df['label'].value_counts()\nx = df_val.index\ny = df_val.values\n\nflight_data = sns.load_dataset(\"flights\")\nplt.figure(figsize=(10, 6))\n\n# fig = sns.barplot(x=x, y=y, order=x)\nfig = sns.barplot(x=x, y=y )\nplt.xticks(rotation=90, fontsize=14);\nplt.yticks(fontsize=14);","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:16.740407Z","iopub.execute_input":"2022-08-11T07:52:16.740724Z","iopub.status.idle":"2022-08-11T07:52:16.998573Z","shell.execute_reply.started":"2022-08-11T07:52:16.740696Z","shell.execute_reply":"2022-08-11T07:52:16.997266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['k_fold'] = np.nan\n\nn_folds = 6\nreversed = False\n\nfor label in df.label.unique():\n    \n    folds = list(range(n_folds))\n    if reversed: folds.reverse()\n    \n    kf = KFold(n_splits=n_folds, random_state=42, shuffle=True)\n    \n    label_idxs = df[df.label==label].index\n    \n    kf.get_n_splits(label_idxs)\n\n    for _, valid_index in kf.split(label_idxs):\n\n        actual_fold = folds.pop(0)\n        df_index = label_idxs[valid_index]\n        df.loc[df_index, 'k_fold'] = actual_fold\n    reversed = not reversed\n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:17.000923Z","iopub.execute_input":"2022-08-11T07:52:17.001501Z","iopub.status.idle":"2022-08-11T07:52:17.064525Z","shell.execute_reply.started":"2022-08-11T07:52:17.001430Z","shell.execute_reply":"2022-08-11T07:52:17.063600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby(['label', 'k_fold']).count()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:17.065816Z","iopub.execute_input":"2022-08-11T07:52:17.066121Z","iopub.status.idle":"2022-08-11T07:52:17.095441Z","shell.execute_reply.started":"2022-08-11T07:52:17.066093Z","shell.execute_reply":"2022-08-11T07:52:17.094546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ngraphic = df.groupby(['label', 'k_fold']).size().reset_index()\ngraphic.columns = ['label', 'k_fold', 'count']\nfig = px.bar(\n    graphic, x=\"k_fold\", y=\"count\",\n    color='label', barmode='group',\n    height=400\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:17.097299Z","iopub.execute_input":"2022-08-11T07:52:17.098055Z","iopub.status.idle":"2022-08-11T07:52:17.199697Z","shell.execute_reply.started":"2022-08-11T07:52:17.098017Z","shell.execute_reply":"2022-08-11T07:52:17.198562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('df_kfold.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:17.201117Z","iopub.execute_input":"2022-08-11T07:52:17.202341Z","iopub.status.idle":"2022-08-11T07:52:17.232726Z","shell.execute_reply.started":"2022-08-11T07:52:17.202297Z","shell.execute_reply":"2022-08-11T07:52:17.231457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}