{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"},{"sourceId":13229715,"sourceType":"datasetVersion","datasetId":8385883},{"sourceId":13229732,"sourceType":"datasetVersion","datasetId":8385893},{"sourceId":38667125,"sourceType":"kernelVersion"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n        print(dirname)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:19:59.994537Z","iopub.execute_input":"2026-01-07T07:19:59.995053Z","iopub.status.idle":"2026-01-07T07:22:04.382141Z","shell.execute_reply.started":"2026-01-07T07:19:59.995027Z","shell.execute_reply":"2026-01-07T07:22:04.381493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nfrom tqdm import tqdm_notebook as tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly\nimport plotly.graph_objects as go\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:04.382799Z","iopub.execute_input":"2026-01-07T07:22:04.383020Z","iopub.status.idle":"2026-01-07T07:22:05.612683Z","shell.execute_reply.started":"2026-01-07T07:22:04.382987Z","shell.execute_reply":"2026-01-07T07:22:05.612154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_image_names(dataframe) : \n    image_names = dataframe[\"image_name\"].values\n    image_names = image_names + \".jpg\"\n    return image_names","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.614005Z","iopub.execute_input":"2026-01-07T07:22:05.614382Z","iopub.status.idle":"2026-01-07T07:22:05.618798Z","shell.execute_reply.started":"2026-01-07T07:22:05.614364Z","shell.execute_reply":"2026-01-07T07:22:05.618227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_info(image_names) : \n    image_names = np.array(image_names)\n    \n    print(\"Length = \", len(image_names))\n    print(\"Type = \", type(image_names))\n    print(\"Shape = \", image_names.shape)\n    \n    return image_names","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.619387Z","iopub.execute_input":"2026-01-07T07:22:05.619591Z","iopub.status.idle":"2026-01-07T07:22:05.634561Z","shell.execute_reply.started":"2026-01-07T07:22:05.619568Z","shell.execute_reply":"2026-01-07T07:22:05.634047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import skew\n\ndef extract_information(image_names, directory) : \n    image_statistics = pd.DataFrame(index = np.arange(len(image_names)),\n                                    columns = [\"image_name\", \"path\", \"rows\", \"columns\", \"channels\", \n                                              \"image_mean\", \"image_standard_deviation\", \"image_skewness\",\n                                              \"mean_red_value\", \"mean_green_value\", \"mean_blue_value\"])\n    i = 0 \n    for name in tqdm(image_names) : \n        path = os.path.join(directory, name)\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        image_statistics.iloc[i][\"image_name\"] = name\n        image_statistics.iloc[i][\"path\"] = path\n        image_statistics.iloc[i][\"rows\"] = image.shape[0]\n        image_statistics.iloc[i][\"columns\"] = image.shape[1]\n        image_statistics.iloc[i][\"channels\"] = image.shape[2]\n        image_statistics.iloc[i][\"image_mean\"] = np.mean(image.flatten())\n        image_statistics.iloc[i][\"image_standard_deviation\"] = np.std(image.flatten())\n        image_statistics.iloc[i][\"image_skewness\"] = skew(image.flatten())\n        image_statistics.iloc[i][\"mean_red_value\"] = np.mean(image[:,:,0])\n        image_statistics.iloc[i][\"mean_green_value\"] = np.mean(image[:,:,1])\n        image_statistics.iloc[i][\"mean_blue_value\"] = np.mean(image[:,:,2])\n        \n        i = i + 1\n        del image\n        \n    return image_statistics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.635371Z","iopub.execute_input":"2026-01-07T07:22:05.635563Z","iopub.status.idle":"2026-01-07T07:22:05.650350Z","shell.execute_reply.started":"2026-01-07T07:22:05.635542Z","shell.execute_reply":"2026-01-07T07:22:05.649668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = \"/kaggle/input/siim-isic-melanoma-classification/jpeg/train/\"\ntrain = pd.DataFrame(pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\"))\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.650964Z","iopub.execute_input":"2026-01-07T07:22:05.651169Z","iopub.status.idle":"2026-01-07T07:22:05.751208Z","shell.execute_reply.started":"2026-01-07T07:22:05.651154Z","shell.execute_reply":"2026-01-07T07:22:05.750539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_names = get_image_names(train)\nimage_names = get_info(image_names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.751943Z","iopub.execute_input":"2026-01-07T07:22:05.752249Z","iopub.status.idle":"2026-01-07T07:22:05.760523Z","shell.execute_reply.started":"2026-01-07T07:22:05.752232Z","shell.execute_reply":"2026-01-07T07:22:05.759968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir = \"/kaggle/input/siim-isic-melanoma-classification/jpeg/test/\"\ntest = pd.DataFrame(pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/test.csv\"))\ntest.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.761270Z","iopub.execute_input":"2026-01-07T07:22:05.761899Z","iopub.status.idle":"2026-01-07T07:22:05.797183Z","shell.execute_reply.started":"2026-01-07T07:22:05.761873Z","shell.execute_reply":"2026-01-07T07:22:05.796538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_names = get_image_names(test)\nimage_names = get_info(image_names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.799554Z","iopub.execute_input":"2026-01-07T07:22:05.799934Z","iopub.status.idle":"2026-01-07T07:22:05.805540Z","shell.execute_reply.started":"2026-01-07T07:22:05.799902Z","shell.execute_reply":"2026-01-07T07:22:05.804978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.DataFrame(pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\"))\ntest = pd.DataFrame(pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/test.csv\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.806347Z","iopub.execute_input":"2026-01-07T07:22:05.806588Z","iopub.status.idle":"2026-01-07T07:22:05.889638Z","shell.execute_reply.started":"2026-01-07T07:22:05.806567Z","shell.execute_reply":"2026-01-07T07:22:05.889098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.890336Z","iopub.execute_input":"2026-01-07T07:22:05.890543Z","iopub.status.idle":"2026-01-07T07:22:05.895589Z","shell.execute_reply.started":"2026-01-07T07:22:05.890527Z","shell.execute_reply":"2026-01-07T07:22:05.894797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.896741Z","iopub.execute_input":"2026-01-07T07:22:05.897469Z","iopub.status.idle":"2026-01-07T07:22:05.916192Z","shell.execute_reply.started":"2026-01-07T07:22:05.897450Z","shell.execute_reply":"2026-01-07T07:22:05.915558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.916865Z","iopub.execute_input":"2026-01-07T07:22:05.917064Z","iopub.status.idle":"2026-01-07T07:22:05.934565Z","shell.execute_reply.started":"2026-01-07T07:22:05.917050Z","shell.execute_reply":"2026-01-07T07:22:05.933958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.935216Z","iopub.execute_input":"2026-01-07T07:22:05.935473Z","iopub.status.idle":"2026-01-07T07:22:05.974381Z","shell.execute_reply.started":"2026-01-07T07:22:05.935450Z","shell.execute_reply":"2026-01-07T07:22:05.973835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.974995Z","iopub.execute_input":"2026-01-07T07:22:05.975307Z","iopub.status.idle":"2026-01-07T07:22:05.985286Z","shell.execute_reply.started":"2026-01-07T07:22:05.975289Z","shell.execute_reply":"2026-01-07T07:22:05.984640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train[\"patient_id\"].unique()), len(test[\"patient_id\"].unique())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:05.986015Z","iopub.execute_input":"2026-01-07T07:22:05.986232Z","iopub.status.idle":"2026-01-07T07:22:06.002263Z","shell.execute_reply.started":"2026-01-07T07:22:05.986218Z","shell.execute_reply":"2026-01-07T07:22:06.001718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train[\"target\"].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.002843Z","iopub.execute_input":"2026-01-07T07:22:06.003033Z","iopub.status.idle":"2026-01-07T07:22:06.016956Z","shell.execute_reply.started":"2026-01-07T07:22:06.003018Z","shell.execute_reply":"2026-01-07T07:22:06.016305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"malignant = len(train[train[\"target\"] == 1])\nbenign = len(train[train[\"target\"] == 0])\n\nlabels = [\"Malignant\", \"Benign\"] \nsize = [malignant, benign]\n\nplt.figure(figsize = (8, 8))\nplt.pie(size, labels = labels, shadow = True, startangle = 90, colors = [\"r\", \"g\"])\nplt.title(\"Malignant VS Benign Cases\")\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.017625Z","iopub.execute_input":"2026-01-07T07:22:06.017825Z","iopub.status.idle":"2026-01-07T07:22:06.274407Z","shell.execute_reply.started":"2026-01-07T07:22:06.017810Z","shell.execute_reply":"2026-01-07T07:22:06.273665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_males = len(train[train[\"sex\"] == \"male\"])\ntrain_females  = len(train[train[\"sex\"] == \"female\"])\n\ntest_males = len(test[test[\"sex\"] == \"male\"])\ntest_females  = len(test[test[\"sex\"] == \"female\"])\n\nlabels = [\"Males\", \"Female\"] \n\nsize = [train_males, train_females]\nexplode = [0.1, 0.0]\n\nplt.figure(figsize = (16, 16))\nplt.subplot(1,2,1)\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90, colors = [\"b\", \"g\"])\nplt.title(\"Male VS Female Training Set Count\", fontsize = 18)\nplt.legend()\n\nprint(\"Number of males in training set = \", train_males)\nprint(\"Number of females in training set= \", train_females)\n\nsize = [test_males, test_females]\n\nplt.subplot(1,2,2)\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90, colors = [\"b\", \"g\"])\nplt.title(\"Male VS Female Test Set Count\", fontsize = 18)\nplt.legend()\n\nprint(\"Number of males in testing set = \", test_males)\nprint(\"Number of females in testing set= \", test_females)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.275289Z","iopub.execute_input":"2026-01-07T07:22:06.275869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_malignant  = train[train[\"target\"] == 1]\ntrain_malignant_males = len(train_malignant[train_malignant[\"sex\"] == \"male\"])\ntrain_malignant_females  = len(train_malignant[train_malignant[\"sex\"] == \"female\"])\n\nlabels = [\"Malignant Male Cases\", \"Malignant Female Cases\"] \nsize = [train_malignant_males, train_malignant_females]\nexplode = [0.1, 0.0]\n\nplt.figure(figsize = (10, 10))\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90, colors = [\"r\", \"c\"])\nplt.title(\"Malignant Male VS Female Cases\", fontsize = 18)\nplt.legend()\nprint(\"Malignant Male Cases = \", train_malignant_males)\nprint(\"Malignant Female Cases = \", train_malignant_females)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.561823Z","iopub.execute_input":"2026-01-07T07:22:06.562252Z","iopub.status.idle":"2026-01-07T07:22:06.819078Z","shell.execute_reply.started":"2026-01-07T07:22:06.562232Z","shell.execute_reply":"2026-01-07T07:22:06.818491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_benign  = train[train[\"target\"] == 0]\n\ntrain_benign_males = len(train_benign[train_benign[\"sex\"] == \"male\"])\ntrain_benign_females  = len(train_benign[train_benign[\"sex\"] == \"female\"]) \n\nlabels = [\"Benign Male Cases\", \"Benign Female Cases\"] \nsize = [train_benign_males, train_benign_females]\nexplode = [0.1, 0.0]\n\nplt.figure(figsize = (10, 10))\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90, colors = [\"g\", \"y\"])\nplt.title(\"Benign Male VS Benign Female Cases\", fontsize = 18)\nplt.legend()\nprint(\"Benign Male Cases = \", train_benign_males)\nprint(\"Benign Female Cases = \", train_benign_females)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.819777Z","iopub.execute_input":"2026-01-07T07:22:06.820058Z","iopub.status.idle":"2026-01-07T07:22:06.994695Z","shell.execute_reply.started":"2026-01-07T07:22:06.820033Z","shell.execute_reply":"2026-01-07T07:22:06.994139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_versus_sex = train.groupby([\"benign_malignant\", \"sex\"]).size()\nprint(cancer_versus_sex)\nprint(type(cancer_versus_sex))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:06.995410Z","iopub.execute_input":"2026-01-07T07:22:06.995743Z","iopub.status.idle":"2026-01-07T07:22:07.006736Z","shell.execute_reply.started":"2026-01-07T07:22:06.995719Z","shell.execute_reply":"2026-01-07T07:22:07.006114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_versus_sex = cancer_versus_sex.unstack(level = 1) / len(train) * 100\nprint(cancer_versus_sex)\nprint(type(cancer_versus_sex))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:07.007480Z","iopub.execute_input":"2026-01-07T07:22:07.007748Z","iopub.status.idle":"2026-01-07T07:22:07.030216Z","shell.execute_reply.started":"2026-01-07T07:22:07.007725Z","shell.execute_reply":"2026-01-07T07:22:07.029341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.set(style='whitegrid')\nsns.set_context(\"paper\", rc={\"font.size\":12,\"axes.titlesize\":20,\"axes.labelsize\":18})   \n\nplt.figure(figsize = (10, 6))\nsns.heatmap(cancer_versus_sex, annot=True, cmap=\"icefire\", cbar=True)\nplt.title(\"Cancer VS Sex Heatmap Analysis Normalized\", fontsize = 18)\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:07.031166Z","iopub.execute_input":"2026-01-07T07:22:07.031801Z","iopub.status.idle":"2026-01-07T07:22:07.287857Z","shell.execute_reply.started":"2026-01-07T07:22:07.031783Z","shell.execute_reply":"2026-01-07T07:22:07.287158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train\ntrain_torso = len(train[train[\"anatom_site_general_challenge\"] == \"torso\"])\ntrain_lower_extremity = len(train[train[\"anatom_site_general_challenge\"] == \"lower extremity\"])\ntrain_upper_extremity = len(train[train[\"anatom_site_general_challenge\"] == \"upper extremity\"])\ntrain_head_neck = len(train[train[\"anatom_site_general_challenge\"] == \"head/neck\"])\ntrain_palms_soles = len(train[train[\"anatom_site_general_challenge\"] == \"palms/soles\"])\ntrain_oral_genital = len(train[train[\"anatom_site_general_challenge\"] == \"oral/genital\"])\n\n# test\ntest_torso = len(test[test[\"anatom_site_general_challenge\"] == \"torso\"])\ntest_lower_extremity = len(test[test[\"anatom_site_general_challenge\"] == \"lower extremity\"])\ntest_upper_extremity = len(test[test[\"anatom_site_general_challenge\"] == \"upper extremity\"])\ntest_head_neck = len(test[test[\"anatom_site_general_challenge\"] == \"head/neck\"])\ntest_palms_soles = len(test[test[\"anatom_site_general_challenge\"] == \"palms/soles\"])\ntest_oral_genital = len(test[test[\"anatom_site_general_challenge\"] == \"oral/genital\"])\n\nlabels = [\"Torso\", \"Lower Extremity\", \"Upper Extremity\", \"Head/Neck\", \"Palms/Soles\", \"Oral/Genital\"] \n\nplt.figure(figsize = (16, 16))\n\nplt.subplot(1,2,1)\nsize = [train_torso, train_lower_extremity, train_upper_extremity, train_head_neck, train_palms_soles, train_oral_genital]\nexplode = [0.05, 0.05, 0.05, 0.05, 0.05, 0.1]\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90)\nplt.title(\"Anatomy Sites In Training Set\", fontsize = 18)\nplt.legend()\n\nplt.subplot(1,2,2)\nsize = [test_torso, test_lower_extremity, test_upper_extremity, test_head_neck, test_palms_soles, test_oral_genital]\nexplode = [0.05, 0.05, 0.05, 0.05, 0.05, 0.1]\nplt.pie(size, labels = labels, explode = explode, shadow = True, startangle = 90)\nplt.title(\"Anatomy Sites In Testing Set\", fontsize = 18)\nplt.legend()\n\n# Automatically adjust subplot parameters to give specified padding.\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:07.288751Z","iopub.execute_input":"2026-01-07T07:22:07.289117Z","iopub.status.idle":"2026-01-07T07:22:07.824690Z","shell.execute_reply.started":"2026-01-07T07:22:07.289094Z","shell.execute_reply":"2026-01-07T07:22:07.823894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ages_benign = train.loc[train[\"target\"] == 0, \"age_approx\"]\ntrain_ages_malignant = train.loc[train[\"target\"] == 1 , \"age_approx\"]\n\nplt.figure(figsize = (10, 8))\nsns.kdeplot(train_ages_benign, label = \"Benign\", shade = True, legend = True, cbar = True)\nsns.kdeplot(train_ages_malignant, label = \"Malignant\", shade = True, legend = True, cbar = True)\nplt.grid(True)\nplt.xlabel(\"Age Of The Patients\", fontsize = 18)\nplt.ylabel(\"Probability Density\", fontsize = 18)\nplt.grid(which = \"minor\", axis = \"both\")\nplt.title(\"Probabilistic Age Distribution In Training Set\", fontsize = 18)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:07.825602Z","iopub.execute_input":"2026-01-07T07:22:07.826083Z","iopub.status.idle":"2026-01-07T07:22:08.359647Z","shell.execute_reply.started":"2026-01-07T07:22:07.826059Z","shell.execute_reply":"2026-01-07T07:22:08.358749Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_stats_01 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_01\"))\ntrain_image_stats_02 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_02\"))\ntrain_image_stats_03 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_03\"))\ntrain_image_stats_04 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_04\"))\ntrain_image_stats_05 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_05\"))\ntrain_image_stats_06 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_06\"))\n\nprint(train_image_stats_01.shape)\nprint(train_image_stats_02.shape)\nprint(train_image_stats_03.shape)\nprint(train_image_stats_04.shape)\nprint(train_image_stats_05.shape)\nprint(train_image_stats_06.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.364439Z","iopub.execute_input":"2026-01-07T07:22:08.364847Z","iopub.status.idle":"2026-01-07T07:22:08.505679Z","shell.execute_reply.started":"2026-01-07T07:22:08.364830Z","shell.execute_reply":"2026-01-07T07:22:08.504962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics = pd.concat([train_image_stats_01, train_image_stats_02, train_image_stats_03,\n                                   train_image_stats_04, train_image_stats_05, train_image_stats_06],\n                                  ignore_index = True)\ntrain_image_statistics.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.506574Z","iopub.execute_input":"2026-01-07T07:22:08.507240Z","iopub.status.idle":"2026-01-07T07:22:08.513052Z","shell.execute_reply.started":"2026-01-07T07:22:08.507222Z","shell.execute_reply":"2026-01-07T07:22:08.512393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.513988Z","iopub.execute_input":"2026-01-07T07:22:08.514200Z","iopub.status.idle":"2026-01-07T07:22:08.532586Z","shell.execute_reply.started":"2026-01-07T07:22:08.514185Z","shell.execute_reply":"2026-01-07T07:22:08.531975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_stats_01 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled-test/melanoma_image_statistics_compiled_test_01\"))\ntest_image_stats_02 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled-test/melanoma_image_statistics_compiled_test_02\"))\n\nprint(test_image_stats_01.shape)\nprint(test_image_stats_02.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.534071Z","iopub.execute_input":"2026-01-07T07:22:08.534240Z","iopub.status.idle":"2026-01-07T07:22:08.582227Z","shell.execute_reply.started":"2026-01-07T07:22:08.534227Z","shell.execute_reply":"2026-01-07T07:22:08.581683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_statistics = pd.concat([test_image_stats_01, test_image_stats_02], ignore_index = True)\n\ntest_image_statistics.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.583008Z","iopub.execute_input":"2026-01-07T07:22:08.583262Z","iopub.status.idle":"2026-01-07T07:22:08.588871Z","shell.execute_reply.started":"2026-01-07T07:22:08.583240Z","shell.execute_reply":"2026-01-07T07:22:08.588185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_statistics.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.589564Z","iopub.execute_input":"2026-01-07T07:22:08.589788Z","iopub.status.idle":"2026-01-07T07:22:08.607210Z","shell.execute_reply.started":"2026-01-07T07:22:08.589769Z","shell.execute_reply":"2026-01-07T07:22:08.606550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.607913Z","iopub.execute_input":"2026-01-07T07:22:08.608225Z","iopub.status.idle":"2026-01-07T07:22:08.623257Z","shell.execute_reply.started":"2026-01-07T07:22:08.608210Z","shell.execute_reply":"2026-01-07T07:22:08.622739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_statistics.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.623895Z","iopub.execute_input":"2026-01-07T07:22:08.624145Z","iopub.status.idle":"2026-01-07T07:22:08.643278Z","shell.execute_reply.started":"2026-01-07T07:22:08.624122Z","shell.execute_reply":"2026-01-07T07:22:08.642733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_names = train_image_statistics[\"image_name\"].values\nrandom_images = [np.random.choice(image_names) for i in range(4)] # Generates a random sample from a given 1-D array\nrandom_images ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.643937Z","iopub.execute_input":"2026-01-07T07:22:08.644157Z","iopub.status.idle":"2026-01-07T07:22:08.655673Z","shell.execute_reply.started":"2026-01-07T07:22:08.644138Z","shell.execute_reply":"2026-01-07T07:22:08.655085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = \"/kaggle/input/siim-isic-melanoma-classification/jpeg/train/\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.656340Z","iopub.execute_input":"2026-01-07T07:22:08.656658Z","iopub.status.idle":"2026-01-07T07:22:08.668932Z","shell.execute_reply.started":"2026-01-07T07:22:08.656636Z","shell.execute_reply":"2026-01-07T07:22:08.668357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nfor i in range(4) : \n    plt.subplot(2, 2, i + 1) \n    image = cv2.imread(os.path.join(train_dir, random_images[i]))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image, cmap = \"gray\")\n    plt.grid(True)\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:08.669590Z","iopub.execute_input":"2026-01-07T07:22:08.670210Z","iopub.status.idle":"2026-01-07T07:22:21.625154Z","shell.execute_reply.started":"2026-01-07T07:22:08.670187Z","shell.execute_reply":"2026-01-07T07:22:21.624237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"benign_mean_red_value = []\nbenign_mean_green_value = []\nbenign_mean_blue_value = []\n\nmalignant_mean_red_value = []\nmalignant_mean_green_value = []\nmalignant_mean_blue_value = []\n\nfor image_name in tqdm(train_image_statistics[\"image_name\"]) : \n    name = image_name[0:len(image_name)-4] \n    extracted_section = train[train[\"image_name\"] == name]\n    r = int(train_image_statistics[train_image_statistics[\"image_name\"] == image_name][\"mean_red_value\"])\n    g = int(train_image_statistics[train_image_statistics[\"image_name\"] == image_name][\"mean_green_value\"])\n    b = int(train_image_statistics[train_image_statistics[\"image_name\"] == image_name][\"mean_blue_value\"])\n    if int(extracted_section[\"target\"]) == 0 : # benign\n        benign_mean_red_value.append(r)\n        benign_mean_green_value.append(g)\n        benign_mean_blue_value.append(b)\n    else:\n        malignant_mean_red_value.append(r)\n        malignant_mean_green_value.append(g)\n        malignant_mean_blue_value.append(b)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:22:21.626140Z","iopub.execute_input":"2026-01-07T07:22:21.626357Z","iopub.status.idle":"2026-01-07T07:28:17.518268Z","shell.execute_reply.started":"2026-01-07T07:22:21.626340Z","shell.execute_reply":"2026-01-07T07:28:17.517487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"range_of_spread = max(benign_mean_red_value) - min(benign_mean_red_value)\n\nplt.figure(figsize = (12, 8))\nplt.rc(\"font\", weight = \"bold\")\nsns.set_style(\"whitegrid\")\nfig = sns.distplot(benign_mean_red_value, hist = True, kde = True, label = \"Mean Red Channel Intensities\", color = \"r\")\nfig.set(xlabel = \"Mean red channel intensities observed in each image\",\n        ylabel = \"Probability Density\")\nplt.title(\"Spread Of Red Channel In Benign Cases\", fontsize = 18)\nplt.legend()\nprint(\"The range of spread = {:.2f}\".format(range_of_spread))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:17.519341Z","iopub.execute_input":"2026-01-07T07:28:17.519736Z","iopub.status.idle":"2026-01-07T07:28:18.086486Z","shell.execute_reply.started":"2026-01-07T07:28:17.519706Z","shell.execute_reply":"2026-01-07T07:28:18.085726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"range_of_spread = max(benign_mean_green_value) - min(benign_mean_green_value)\n\nplt.figure(figsize = (12, 8))\nplt.rc(\"font\", weight = \"bold\")\nsns.set_style(\"whitegrid\")\nfig = sns.distplot(benign_mean_green_value, hist = True, kde = True, label = \"Mean Green Channel Intensities\", color = \"g\")\nfig.set(xlabel = \"Mean green channel intensities observed in each image\",\n        ylabel = \"Probability Density\") \nplt.title(\"Spread Of Green Channel In Benign Cases\", fontsize = 18)\nplt.legend()\nprint(\"The range of spread = {:.2f}\".format(range_of_spread))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:18.087342Z","iopub.execute_input":"2026-01-07T07:28:18.087615Z","iopub.status.idle":"2026-01-07T07:28:18.629096Z","shell.execute_reply.started":"2026-01-07T07:28:18.087597Z","shell.execute_reply":"2026-01-07T07:28:18.628387Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"range_of_spread = max(benign_mean_blue_value) - min(benign_mean_blue_value)\n\nplt.figure(figsize = (12, 8))\nplt.rc(\"font\", weight = \"bold\")\nsns.set_style(\"whitegrid\")\nfig = sns.distplot(benign_mean_blue_value, hist = True, kde = True, label = \"Mean Blue Channel Intensities\", color = \"b\")\nfig.set(xlabel = \"Mean blue channel intensities observed in each image\",\n        ylabel = \"Probability Density\") \nplt.title(\"Spread Of Blue Channel In Benign Cases\", fontsize = 18)\nplt.legend()\nprint(\"The range of spread = {:.2f}\".format(range_of_spread))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:18.630010Z","iopub.execute_input":"2026-01-07T07:28:18.630582Z","iopub.status.idle":"2026-01-07T07:28:19.171610Z","shell.execute_reply.started":"2026-01-07T07:28:18.630563Z","shell.execute_reply":"2026-01-07T07:28:19.170890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nplt.rc(\"font\", weight = \"bold\")\nsns.set_style(\"whitegrid\")\nfig = sns.distplot(benign_mean_blue_value, hist = False, kde = True, label = \"Mean Blue Channel Intensities\", color = \"b\")\nfig = sns.distplot(benign_mean_red_value, hist = False, kde = True, label = \"Mean Red Channel Intensities\", color = \"r\")\nfig = sns.distplot(benign_mean_green_value, hist = False, kde = True, label = \"Mean Green Channel Intensities\", color = \"g\")\n\nfig.set(xlabel = \"Mean channel intensities observed in each image\",\n        ylabel = \"Probability Density\") \nplt.title(\"Spread Of Channels In Benign Cases\", fontsize = 18)\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:19.172398Z","iopub.execute_input":"2026-01-07T07:28:19.172714Z","iopub.status.idle":"2026-01-07T07:28:20.021590Z","shell.execute_reply.started":"2026-01-07T07:28:19.172696Z","shell.execute_reply":"2026-01-07T07:28:20.020872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del benign_mean_red_value\ndel benign_mean_green_value\ndel benign_mean_blue_value","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.022352Z","iopub.execute_input":"2026-01-07T07:28:20.022995Z","iopub.status.idle":"2026-01-07T07:28:20.026603Z","shell.execute_reply.started":"2026-01-07T07:28:20.022968Z","shell.execute_reply":"2026-01-07T07:28:20.025911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.027343Z","iopub.execute_input":"2026-01-07T07:28:20.027571Z","iopub.status.idle":"2026-01-07T07:28:20.142497Z","shell.execute_reply.started":"2026-01-07T07:28:20.027548Z","shell.execute_reply":"2026-01-07T07:28:20.141941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nplt.rc(\"font\", weight = \"bold\")\nsns.set_style(\"whitegrid\")\nfig = sns.distplot(malignant_mean_blue_value, hist = False, kde = True, label = \"Mean Blue Channel Intensities\", color = \"b\")\nfig = sns.distplot(malignant_mean_red_value, hist = False, kde = True, label = \"Mean Red Channel Intensities\", color = \"r\")\nfig = sns.distplot(malignant_mean_green_value, hist = False, kde = True, label = \"Mean Green Channel Intensities\", color = \"g\")\n\nfig.set(xlabel = \"Mean channel intensities observed in each image\",\n        ylabel = \"Probability Density\") \nplt.title(\"Spread Of Channels In Malignant Cases\", fontsize = 18)\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.143143Z","iopub.execute_input":"2026-01-07T07:28:20.143459Z","iopub.status.idle":"2026-01-07T07:28:20.555190Z","shell.execute_reply.started":"2026-01-07T07:28:20.143439Z","shell.execute_reply":"2026-01-07T07:28:20.554447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.555990Z","iopub.execute_input":"2026-01-07T07:28:20.556237Z","iopub.status.idle":"2026-01-07T07:28:20.642718Z","shell.execute_reply.started":"2026-01-07T07:28:20.556217Z","shell.execute_reply":"2026-01-07T07:28:20.642107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.643464Z","iopub.execute_input":"2026-01-07T07:28:20.643708Z","iopub.status.idle":"2026-01-07T07:28:20.664299Z","shell.execute_reply.started":"2026-01-07T07:28:20.643687Z","shell.execute_reply":"2026-01-07T07:28:20.663708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing = len(train[train[\"sex\"].isna() == True])\navailable = len(train[train[\"sex\"].isna() == False])\n\nx = [\"Availabe data\", \"Unavailable data\"]\ny = [np.log(available), np.log(missing)]\n\nprint(\"Count of missing data = \", missing)\nprint(\"Count of available data = \", available)\n\nplt.figure(figsize = (12, 8))\nplt.subplot(1,1,1)\nplt.barh(x, y, color = \"m\")\nplt.grid(True)\nplt.title(\"Data On Patient's Sex\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.665004Z","iopub.execute_input":"2026-01-07T07:28:20.665618Z","iopub.status.idle":"2026-01-07T07:28:20.898168Z","shell.execute_reply.started":"2026-01-07T07:28:20.665601Z","shell.execute_reply":"2026-01-07T07:28:20.897419Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['sex'].fillna('male', inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.898961Z","iopub.execute_input":"2026-01-07T07:28:20.899184Z","iopub.status.idle":"2026-01-07T07:28:20.905569Z","shell.execute_reply.started":"2026-01-07T07:28:20.899168Z","shell.execute_reply":"2026-01-07T07:28:20.904833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing =  len(train[train[\"age_approx\"].isna() == True]) \navailable = len(train[train[\"age_approx\"].isna() == False]) \n\nprint(\"Missing age values = \", missing)\nprint(\"Available age data = \", available)\n\nx = [\"Availabe data\", \"Unavailable data\"]\ny = [np.log(available), np.log(missing)] \n\nplt.figure(figsize = (12, 8))\nplt.subplot(1,1,1)\nplt.barh(x, y, color = \"y\")\nplt.grid(True)\nplt.title(\"Data On Patient's Age\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:20.906169Z","iopub.execute_input":"2026-01-07T07:28:20.907054Z","iopub.status.idle":"2026-01-07T07:28:21.109142Z","shell.execute_reply.started":"2026-01-07T07:28:20.907036Z","shell.execute_reply":"2026-01-07T07:28:21.108486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train\nanatomy_sites = [\"torso\", \"upper extremity\", \"lower extremity\"]\n\nrelevant_dataframe_part = train[(train[\"sex\"] == \"male\") &\n                     (train[\"anatom_site_general_challenge\"].isin(anatomy_sites)) &\n                     (train[\"target\"] == 0)]\n\nmedian_value = relevant_dataframe_part[\"age_approx\"].median()\n\nprint(\"Median value = \", median_value)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:21.109965Z","iopub.execute_input":"2026-01-07T07:28:21.110245Z","iopub.status.idle":"2026-01-07T07:28:21.122232Z","shell.execute_reply.started":"2026-01-07T07:28:21.110222Z","shell.execute_reply":"2026-01-07T07:28:21.121498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"age_approx\"].fillna(median_value, inplace = True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:21.122980Z","iopub.execute_input":"2026-01-07T07:28:21.123909Z","iopub.status.idle":"2026-01-07T07:28:21.133749Z","shell.execute_reply.started":"2026-01-07T07:28:21.123888Z","shell.execute_reply":"2026-01-07T07:28:21.133100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"anatom_site_general_challenge\"].fillna(\"torso\", inplace = True)\ntest[\"anatom_site_general_challenge\"].fillna(\"torso\", inplace = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:21.134488Z","iopub.execute_input":"2026-01-07T07:28:21.135083Z","iopub.status.idle":"2026-01-07T07:28:21.151278Z","shell.execute_reply.started":"2026-01-07T07:28:21.135063Z","shell.execute_reply":"2026-01-07T07:28:21.150582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:21.151983Z","iopub.execute_input":"2026-01-07T07:28:21.152411Z","iopub.status.idle":"2026-01-07T07:28:21.178501Z","shell.execute_reply.started":"2026-01-07T07:28:21.152394Z","shell.execute_reply":"2026-01-07T07:28:21.177899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.info()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:28:21.179152Z","iopub.execute_input":"2026-01-07T07:28:21.179345Z","iopub.status.idle":"2026-01-07T07:28:21.193248Z","shell.execute_reply.started":"2026-01-07T07:28:21.179330Z","shell.execute_reply":"2026-01-07T07:28:21.192502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_stats_01 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_01\"))\ntrain_image_stats_02 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_02\"))\ntrain_image_stats_03 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_03\"))\ntrain_image_stats_04 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_04\"))\ntrain_image_stats_05 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_05\"))\ntrain_image_stats_06 = pd.DataFrame(pd.read_csv(\"/kaggle/input/compiled/melanoma_image_statistics_compiled_06\"))\n\nprint(train_image_stats_01.shape)\nprint(train_image_stats_02.shape)\nprint(train_image_stats_03.shape)\nprint(train_image_stats_04.shape)\nprint(train_image_stats_05.shape)\nprint(train_image_stats_06.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:47.001485Z","iopub.execute_input":"2026-01-07T07:29:47.001771Z","iopub.status.idle":"2026-01-07T07:29:47.109874Z","shell.execute_reply.started":"2026-01-07T07:29:47.001751Z","shell.execute_reply":"2026-01-07T07:29:47.109310Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics = pd.concat([train_image_stats_01, train_image_stats_02, train_image_stats_03,\n                                   train_image_stats_04, train_image_stats_05, train_image_stats_06],\n                                  ignore_index = True)\ntrain_image_statistics.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:47.911426Z","iopub.execute_input":"2026-01-07T07:29:47.912053Z","iopub.status.idle":"2026-01-07T07:29:47.919153Z","shell.execute_reply.started":"2026-01-07T07:29:47.912029Z","shell.execute_reply":"2026-01-07T07:29:47.918549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:48.067008Z","iopub.execute_input":"2026-01-07T07:29:48.067263Z","iopub.status.idle":"2026-01-07T07:29:48.079067Z","shell.execute_reply.started":"2026-01-07T07:29:48.067245Z","shell.execute_reply":"2026-01-07T07:29:48.078316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_statistics.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:48.176889Z","iopub.execute_input":"2026-01-07T07:29:48.177187Z","iopub.status.idle":"2026-01-07T07:29:48.189567Z","shell.execute_reply.started":"2026-01-07T07:29:48.177165Z","shell.execute_reply":"2026-01-07T07:29:48.188957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = \"/kaggle/input/siim-isic-melanoma-classification/jpeg/train/\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:48.302262Z","iopub.execute_input":"2026-01-07T07:29:48.302975Z","iopub.status.idle":"2026-01-07T07:29:48.306111Z","shell.execute_reply.started":"2026-01-07T07:29:48.302951Z","shell.execute_reply":"2026-01-07T07:29:48.305413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_names = train_image_statistics[\"image_name\"].values\nrandom_images = [np.random.choice(image_names) for i in range(4)] # Generates a random sample from a given 1-D array\nrandom_images ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:48.419164Z","iopub.execute_input":"2026-01-07T07:29:48.419353Z","iopub.status.idle":"2026-01-07T07:29:48.424548Z","shell.execute_reply.started":"2026-01-07T07:29:48.419339Z","shell.execute_reply":"2026-01-07T07:29:48.424039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nfor i in range(4) : \n    plt.subplot(2, 2, i + 1) \n    image = cv2.imread(os.path.join(train_dir, random_images[i]))\n    # cv2 reads images in BGR format. Hence we convert it to RGB\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image, cmap = \"gray\")\n    plt.grid(True)\n# Automatically adjust subplot parameters to give specified padding.\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:48.527356Z","iopub.execute_input":"2026-01-07T07:29:48.527554Z","iopub.status.idle":"2026-01-07T07:29:59.841578Z","shell.execute_reply.started":"2026-01-07T07:29:48.527539Z","shell.execute_reply":"2026-01-07T07:29:59.840798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def non_local_means_denoising(image) : \n    denoised_image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)\n    return denoised_image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:59.842602Z","iopub.execute_input":"2026-01-07T07:29:59.842810Z","iopub.status.idle":"2026-01-07T07:29:59.846628Z","shell.execute_reply.started":"2026-01-07T07:29:59.842794Z","shell.execute_reply":"2026-01-07T07:29:59.845869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_image = cv2.imread(os.path.join(train_dir, random_images[0]))\n# cv2 reads images in BGR format. Hence we convert it to RGB\nsample_image = cv2.cvtColor(sample_image, cv2.COLOR_BGR2RGB)\ndenoised_image = non_local_means_denoising(sample_image)\n\n\nplt.figure(figsize = (12, 8))\nplt.subplot(1,2,1)\nplt.imshow(sample_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Normal Image\")\n\nplt.subplot(1,2,2)  \nplt.imshow(denoised_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Denoised image\")    \n# Automatically adjust subplot parameters to give specified padding.\nplt.tight_layout() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:29:59.847615Z","iopub.execute_input":"2026-01-07T07:29:59.847956Z","iopub.status.idle":"2026-01-07T07:30:03.695094Z","shell.execute_reply.started":"2026-01-07T07:29:59.847930Z","shell.execute_reply":"2026-01-07T07:30:03.694408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def hair_removal(image):\n    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n\n    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17,17))\n    blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)\n\n    _, thresh = cv2.threshold(blackhat, 10, 255, cv2.THRESH_BINARY)\n    thresh = cv2.dilate(thresh, None, iterations=2)\n    thresh = cv2.erode(thresh, None, iterations=2)\n\n    dst = cv2.inpaint(image, thresh, 3, cv2.INPAINT_TELEA)\n\n    return dst, thresh","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:03.696772Z","iopub.execute_input":"2026-01-07T07:30:03.697055Z","iopub.status.idle":"2026-01-07T07:30:03.702235Z","shell.execute_reply.started":"2026-01-07T07:30:03.697031Z","shell.execute_reply":"2026-01-07T07:30:03.701586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hair_removed, hair_mask = hair_removal(sample_image)\n\nplt.figure(figsize=(15,5))\n\nplt.subplot(1,3,1)\nplt.imshow(sample_image)\nplt.title(\"Original Image\")\nplt.axis(\"off\")\n\nplt.subplot(1,3,2)\nplt.imshow(hair_mask, cmap=\"gray\")\nplt.title(\"Detected Hair Mask\")\nplt.axis(\"off\")\n\nplt.subplot(1,3,3)\nplt.imshow(hair_removed)\nplt.title(\"Hair Removed Image\")\nplt.axis(\"off\")\n\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:03.703030Z","iopub.execute_input":"2026-01-07T07:30:03.703233Z","iopub.status.idle":"2026-01-07T07:30:05.470647Z","shell.execute_reply.started":"2026-01-07T07:30:03.703216Z","shell.execute_reply":"2026-01-07T07:30:05.470000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def histogram_equalization(image) : \n    image_ycrcb = cv2.cvtColor(image, cv2.COLOR_RGB2YCR_CB)\n    y_channel = image_ycrcb[:,:,0] # apply local histogram processing on this channel\n    cr_channel = image_ycrcb[:,:,1]\n    cb_channel = image_ycrcb[:,:,2]\n    \n    # Local histogram equalization\n    clahe = cv2.createCLAHE(clipLimit = 2.0, tileGridSize=(8,8))\n    equalized = clahe.apply(y_channel)\n    equalized_image = cv2.merge([equalized, cr_channel, cb_channel])\n    equalized_image = cv2.cvtColor(equalized_image, cv2.COLOR_YCR_CB2RGB)\n    return equalized_image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:05.471597Z","iopub.execute_input":"2026-01-07T07:30:05.471852Z","iopub.status.idle":"2026-01-07T07:30:05.477832Z","shell.execute_reply.started":"2026-01-07T07:30:05.471830Z","shell.execute_reply":"2026-01-07T07:30:05.476966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Denoise\ndenoised_image = non_local_means_denoising(sample_image)\n\n# 2. Hair Removal\nhair_removed, hair_mask = hair_removal(denoised_image)\n\n# 3. Histogram Equalization (dùng ảnh đã xóa lông)\nequalized_image = histogram_equalization(hair_removed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:05.478800Z","iopub.execute_input":"2026-01-07T07:30:05.479016Z","iopub.status.idle":"2026-01-07T07:30:08.454169Z","shell.execute_reply.started":"2026-01-07T07:30:05.478992Z","shell.execute_reply":"2026-01-07T07:30:08.453341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nplt.subplot(1,4,1)\nplt.imshow(sample_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Normal img\", fontsize = 14)\n\nplt.subplot(1,4,2)  \nplt.imshow(denoised_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"denoised img\", fontsize = 14)\n\nplt.subplot(1,4,3)  \nplt.imshow(hair_removed, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"img after hair removal\", fontsize = 14)\n\nplt.subplot(1,4,4)  \nplt.imshow(equalized_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Histogram equalized img\", fontsize = 14)\n# Automatically adjust subplot parameters to give specified padding.\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:08.455236Z","iopub.execute_input":"2026-01-07T07:30:08.455451Z","iopub.status.idle":"2026-01-07T07:30:10.272634Z","shell.execute_reply.started":"2026-01-07T07:30:08.455435Z","shell.execute_reply":"2026-01-07T07:30:10.271982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def segmentation(image, k, attempts) : \n    vectorized = np.float32(image.reshape((-1, 3)))\n    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)\n    res , label , center = cv2.kmeans(vectorized, k, None, criteria, attempts, cv2.KMEANS_PP_CENTERS)\n    center = np.uint8(center)\n    res = center[label.flatten()]\n    segmented_image = res.reshape((image.shape))\n    return segmented_image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:10.273564Z","iopub.execute_input":"2026-01-07T07:30:10.273836Z","iopub.status.idle":"2026-01-07T07:30:10.279570Z","shell.execute_reply.started":"2026-01-07T07:30:10.273811Z","shell.execute_reply":"2026-01-07T07:30:10.278732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nplt.subplot(1,1,1)\nplt.imshow(hair_removed, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"hair remove Image\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:10.282013Z","iopub.execute_input":"2026-01-07T07:30:10.282218Z","iopub.status.idle":"2026-01-07T07:30:11.098592Z","shell.execute_reply.started":"2026-01-07T07:30:10.282196Z","shell.execute_reply":"2026-01-07T07:30:11.097956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\nsegmented_image = segmentation(hair_removed, 3, 10) # k = 3, attempt = 10\nplt.subplot(1,3,1)\nplt.imshow(segmented_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Segmented Img k = 3\")\n\nsegmented_image = segmentation(hair_removed, 4, 10) # k = 4, attempt = 10\nplt.subplot(1,3,2)\nplt.imshow(segmented_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Segmented Img k = 4\")\n\nsegmented_image = segmentation(hair_removed, 5, 10) # k = 5, attempt = 10\nplt.subplot(1,3,3)\nplt.imshow(segmented_image, cmap = \"gray\")\nplt.grid(False)\nplt.title(\"Segmented Img k = 5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:11.099340Z","iopub.execute_input":"2026-01-07T07:30:11.099564Z","iopub.status.idle":"2026-01-07T07:30:21.421808Z","shell.execute_reply.started":"2026-01-07T07:30:11.099540Z","shell.execute_reply":"2026-01-07T07:30:21.421154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SHAPE = (224, 224, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:21.422535Z","iopub.execute_input":"2026-01-07T07:30:21.422741Z","iopub.status.idle":"2026-01-07T07:30:21.426170Z","shell.execute_reply.started":"2026-01-07T07:30:21.422724Z","shell.execute_reply":"2026-01-07T07:30:21.425645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def resize(image, shape) : \n    image = cv2.resize(image, (shape[0], shape[1]))\n    return image   ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:21.426989Z","iopub.execute_input":"2026-01-07T07:30:21.427602Z","iopub.status.idle":"2026-01-07T07:30:21.441359Z","shell.execute_reply.started":"2026-01-07T07:30:21.427584Z","shell.execute_reply":"2026-01-07T07:30:21.440695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dùng cột benign_malignant làm nhãn\ntrain[\"label\"] = train[\"benign_malignant\"].astype(str)\n\nprint(train[\"label\"].value_counts())  # kiểm tra số lượng\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\naugment_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split=0.2\n)\n\n# Thêm đuôi .jpg vào image_name\ntrain[\"image_name\"] = train[\"image_name\"].astype(str) + \".jpg\"\n\nprint(train[\"image_name\"].head())  # kiểm tra lại tên file\n\n\n# ✅ Train generator\ntrain_generator = augment_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=train_dir,\n    x_col=\"image_name\",\n    y_col=\"label\",          # <--- dùng label mới\n    target_size=(224,224),\n    class_mode=\"binary\",\n    subset=\"training\",\n    batch_size=32,\n    shuffle=True\n)\n\n# ✅ Validation generator\nval_generator = augment_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=train_dir,\n    x_col=\"image_name\",\n    y_col=\"label\",          # <--- dùng label mới\n    target_size=(224,224),\n    class_mode=\"binary\",\n    subset=\"validation\",\n    batch_size=32,\n    shuffle=False\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:30:21.442097Z","iopub.execute_input":"2026-01-07T07:30:21.442628Z","iopub.status.idle":"2026-01-07T07:31:48.948605Z","shell.execute_reply.started":"2026-01-07T07:30:21.442611Z","shell.execute_reply":"2026-01-07T07:31:48.947905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Đếm trước khi cân bằng\ncounts_before = train[\"label\"].value_counts()\n\n# ✅ Oversampling malignant cho cân bằng\nbenign_df = train[train[\"label\"] == \"benign\"]\nmalignant_df = train[train[\"label\"] == \"malignant\"]\n\n# Nhân malignant lên cho gần bằng benign\nmalignant_oversampled = malignant_df.sample(len(benign_df), replace=True, random_state=42)\n\n# Ghép lại\nbalanced_train = pd.concat([benign_df, malignant_oversampled], axis=0).reset_index(drop=True)\n\n# Đếm sau khi cân bằng\ncounts_after = balanced_train[\"label\"].value_counts()\n\n# ✅ Vẽ biểu đồ so sánh\nfig, axes = plt.subplots(1, 2, figsize=(12, 5))\n\n# Trước khi cân bằng\naxes[0].bar(counts_before.index, counts_before.values, color=[\"skyblue\", \"salmon\"])\naxes[0].set_title(\"Before balanced\")\naxes[0].set_ylabel(\"Number of the img\")\n\n# Sau khi cân bằng\naxes[1].bar(counts_after.index, counts_after.values, color=[\"skyblue\", \"salmon\"])\naxes[1].set_title(\"After balanced\")\n\nplt.suptitle(\"Comparing to Benign vs Malignant\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-07T07:31:48.949430Z","iopub.execute_input":"2026-01-07T07:31:48.950066Z","iopub.status.idle":"2026-01-07T07:31:49.218443Z","shell.execute_reply.started":"2026-01-07T07:31:48.950046Z","shell.execute_reply":"2026-01-07T07:31:49.217683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}