{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"},{"sourceId":25383,"databundleVersionId":2684322,"sourceType":"competition"}],"dockerImageVersionId":30123,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nfrom sklearn import datasets\nfrom sklearn import model_selection","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-10-10T14:10:44.378472Z","iopub.execute_input":"2025-10-10T14:10:44.378815Z","iopub.status.idle":"2025-10-10T14:10:45.590392Z","shell.execute_reply.started":"2025-10-10T14:10:44.378728Z","shell.execute_reply":"2025-10-10T14:10:45.589214Z"},"trusted":true},"outputs":[],"execution_count":1},{"cell_type":"code","source":"def create_folds(data, num_splits):\n    data[\"kfold\"] = -1\n    num_bins = int(np.floor(1 + np.log2(len(data))))\n    print('num_bins: ',num_bins)\n\n    data.loc[:, \"bins\"] = pd.cut(data[\"target\"], bins=num_bins, labels=False)\n\n    kf = model_selection.StratifiedKFold(n_splits=num_splits, shuffle=True, random_state=42)\n    \n    for f, (t_, v_) in enumerate(kf.split(X=data, y=data.bins.values)):\n        data.loc[v_, 'kfold'] = f\n#     print(data.head())\n    data = data.drop(\"bins\", axis=1)\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2025-10-10T14:10:45.591867Z","iopub.execute_input":"2025-10-10T14:10:45.592185Z","iopub.status.idle":"2025-10-10T14:10:45.599695Z","shell.execute_reply.started":"2025-10-10T14:10:45.592156Z","shell.execute_reply":"2025-10-10T14:10:45.598658Z"},"trusted":true},"outputs":[],"execution_count":2},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\")\n\ndf_5 = create_folds(df, num_splits=5)\ndf_10 = create_folds(df, num_splits=10)\ndf_5.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T14:10:45.601724Z","iopub.execute_input":"2025-10-10T14:10:45.602013Z","iopub.status.idle":"2025-10-10T14:10:45.793469Z","shell.execute_reply.started":"2025-10-10T14:10:45.601983Z","shell.execute_reply":"2025-10-10T14:10:45.792295Z"}},"outputs":[{"name":"stdout","text":"num_bins:  16\nnum_bins:  16\n","output_type":"stream"},{"execution_count":3,"output_type":"execute_result","data":{"text/plain":"     image_name  patient_id     sex  age_approx anatom_site_general_challenge  \\\n0  ISIC_2637011  IP_7279968    male        45.0                     head/neck   \n1  ISIC_0015719  IP_3075186  female        45.0               upper extremity   \n2  ISIC_0052212  IP_2842074  female        50.0               lower extremity   \n3  ISIC_0068279  IP_6890425  female        45.0                     head/neck   \n4  ISIC_0074268  IP_8723313  female        55.0               upper extremity   \n\n  diagnosis benign_malignant  target  kfold  \n0   unknown           benign       0      4  \n1   unknown           benign       0      1  \n2     nevus           benign       0      2  \n3   unknown           benign       0      1  \n4   unknown           benign       0      2  ","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>image_name</th>\n      <th>patient_id</th>\n      <th>sex</th>\n      <th>age_approx</th>\n      <th>anatom_site_general_challenge</th>\n      <th>diagnosis</th>\n      <th>benign_malignant</th>\n      <th>target</th>\n      <th>kfold</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>ISIC_2637011</td>\n      <td>IP_7279968</td>\n      <td>male</td>\n      <td>45.0</td>\n      <td>head/neck</td>\n      <td>unknown</td>\n      <td>benign</td>\n      <td>0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>ISIC_0015719</td>\n      <td>IP_3075186</td>\n      <td>female</td>\n      <td>45.0</td>\n      <td>upper extremity</td>\n      <td>unknown</td>\n      <td>benign</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>ISIC_0052212</td>\n      <td>IP_2842074</td>\n      <td>female</td>\n      <td>50.0</td>\n      <td>lower extremity</td>\n      <td>nevus</td>\n      <td>benign</td>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>ISIC_0068279</td>\n      <td>IP_6890425</td>\n      <td>female</td>\n      <td>45.0</td>\n      <td>head/neck</td>\n      <td>unknown</td>\n      <td>benign</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>ISIC_0074268</td>\n      <td>IP_8723313</td>\n      <td>female</td>\n      <td>55.0</td>\n      <td>upper extremity</td>\n      <td>unknown</td>\n      <td>benign</td>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}],"execution_count":3},{"cell_type":"code","source":"df_5.to_csv(\"train_5folds.csv\", index=False)\ndf_10.to_csv(\"train_10folds.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2025-10-10T14:10:45.794688Z","iopub.execute_input":"2025-10-10T14:10:45.794942Z","iopub.status.idle":"2025-10-10T14:10:46.045711Z","shell.execute_reply.started":"2025-10-10T14:10:45.794886Z","shell.execute_reply":"2025-10-10T14:10:46.044446Z"},"trusted":true},"outputs":[],"execution_count":4},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}