{"cells":[{"cell_type":"code","execution_count":1,"id":"0ab78c1f","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2025-10-11T15:47:46.626832Z","iopub.status.busy":"2025-10-11T15:47:46.626479Z","iopub.status.idle":"2025-10-11T15:47:50.580032Z","shell.execute_reply":"2025-10-11T15:47:50.578719Z"},"papermill":{"duration":3.959193,"end_time":"2025-10-11T15:47:50.582138","exception":false,"start_time":"2025-10-11T15:47:46.622945","status":"completed"},"tags":[]},"outputs":[],"source":"import numpy as np\nimport pandas as pd\n\nfrom sklearn import datasets\nfrom sklearn import model_selection"},{"cell_type":"code","execution_count":2,"id":"7848172b","metadata":{"execution":{"iopub.execute_input":"2025-10-11T15:47:50.588025Z","iopub.status.busy":"2025-10-11T15:47:50.587539Z","iopub.status.idle":"2025-10-11T15:47:50.594765Z","shell.execute_reply":"2025-10-11T15:47:50.593704Z"},"papermill":{"duration":0.011775,"end_time":"2025-10-11T15:47:50.596433","exception":false,"start_time":"2025-10-11T15:47:50.584658","status":"completed"},"tags":[]},"outputs":[],"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"},{"cell_type":"code","execution_count":3,"id":"ef29d646","metadata":{"execution":{"iopub.execute_input":"2025-10-11T15:47:50.601413Z","iopub.status.busy":"2025-10-11T15:47:50.601092Z","iopub.status.idle":"2025-10-11T15:47:50.775836Z","shell.execute_reply":"2025-10-11T15:47:50.774603Z"},"papermill":{"duration":0.179328,"end_time":"2025-10-11T15:47:50.777783","exception":false,"start_time":"2025-10-11T15:47:50.598455","status":"completed"},"tags":[]},"outputs":[{"name":"stdout","output_type":"stream","text":["num_bins:  16\n","num_bins:  16\n"]}],"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)"},{"cell_type":"code","execution_count":4,"id":"440508c6","metadata":{"execution":{"iopub.execute_input":"2025-10-11T15:47:50.783257Z","iopub.status.busy":"2025-10-11T15:47:50.782924Z","iopub.status.idle":"2025-10-11T15:47:51.053754Z","shell.execute_reply":"2025-10-11T15:47:51.052630Z"},"papermill":{"duration":0.275645,"end_time":"2025-10-11T15:47:51.055503","exception":false,"start_time":"2025-10-11T15:47:50.779858","status":"completed"},"tags":[]},"outputs":[],"source":"df_5.to_csv(\"train_5folds.csv\", index=False)\ndf_10.to_csv(\"train_10folds.csv\", index=False)"},{"cell_type":"code","execution_count":5,"id":"47ec2f85","metadata":{"execution":{"iopub.execute_input":"2025-10-11T15:47:51.060616Z","iopub.status.busy":"2025-10-11T15:47:51.060087Z","iopub.status.idle":"2025-10-11T15:47:51.085130Z","shell.execute_reply":"2025-10-11T15:47:51.084062Z"},"papermill":{"duration":0.029753,"end_time":"2025-10-11T15:47:51.087174","exception":false,"start_time":"2025-10-11T15:47:51.057421","status":"completed"},"tags":[]},"outputs":[{"data":{"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>"],"text/plain":["     image_name  patient_id     sex  age_approx anatom_site_general_challenge  \\\n","0  ISIC_2637011  IP_7279968    male        45.0                     head/neck   \n","1  ISIC_0015719  IP_3075186  female        45.0               upper extremity   \n","2  ISIC_0052212  IP_2842074  female        50.0               lower extremity   \n","3  ISIC_0068279  IP_6890425  female        45.0                     head/neck   \n","4  ISIC_0074268  IP_8723313  female        55.0               upper extremity   \n","\n","  diagnosis benign_malignant  target  kfold  \n","0   unknown           benign       0      4  \n","1   unknown           benign       0      1  \n","2     nevus           benign       0      2  \n","3   unknown           benign       0      1  \n","4   unknown           benign       0      2  "]},"execution_count":5,"metadata":{},"output_type":"execute_result"}],"source":"df_5.head()"}],"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"databundleVersionId":1222630,"sourceId":20270,"sourceType":"competition"},{"datasetId":8457105,"sourceId":13337432,"sourceType":"datasetVersion"}],"dockerImageVersionId":31153,"isGpuEnabled":false,"isInternetEnabled":false,"language":"python","sourceType":"notebook"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.13"},"papermill":{"default_parameters":{},"duration":10.386702,"end_time":"2025-10-11T15:47:51.810477","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-10-11T15:47:41.423775","version":"2.6.0"}},"nbformat":4,"nbformat_minor":5}