{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### In this notebook, I explore the data included in the Histopathologic Cancer Detection competition. This will provide a better understanding going into the model building and subsequent submission to the competition. The end goal is to identify metastatic cancer cells within the images. 0 indicates non-cancerous images and 1 indicates cancerous images. ","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:29.149302Z","iopub.execute_input":"2024-08-25T22:03:29.149682Z","iopub.status.idle":"2024-08-25T22:03:29.537702Z","shell.execute_reply.started":"2024-08-25T22:03:29.149651Z","shell.execute_reply":"2024-08-25T22:03:29.536674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Exploration and Visualization","metadata":{}},{"cell_type":"code","source":"# Create df and identify path to train images\n# Review df head\ntrain = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\ntrain_path = ('/kaggle/input/histopathologic-cancer-detection/train/')\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:29.539871Z","iopub.execute_input":"2024-08-25T22:03:29.540309Z","iopub.status.idle":"2024-08-25T22:03:29.822814Z","shell.execute_reply.started":"2024-08-25T22:03:29.540278Z","shell.execute_reply":"2024-08-25T22:03:29.821685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Review the details of the df\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:29.824168Z","iopub.execute_input":"2024-08-25T22:03:29.824473Z","iopub.status.idle":"2024-08-25T22:03:29.859953Z","shell.execute_reply.started":"2024-08-25T22:03:29.824446Z","shell.execute_reply":"2024-08-25T22:03:29.858908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The dataset contains 220025 images, with two classes and columns named 'id' and 'label'. ","metadata":{}},{"cell_type":"code","source":"# Evaluate the label distribution\n(train.label.value_counts() / len(train)).to_frame()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:29.861167Z","iopub.execute_input":"2024-08-25T22:03:29.861478Z","iopub.status.idle":"2024-08-25T22:03:29.884007Z","shell.execute_reply.started":"2024-08-25T22:03:29.861451Z","shell.execute_reply":"2024-08-25T22:03:29.882901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The dataset is imbalanced-close to a 60/40 split, with the non-cancerous images having the majority over cancerous images. ","metadata":{}},{"cell_type":"code","source":"# Plot the label distribution using a bar chart \n\nplt.figure(figsize=(6, 4))\nclass_dist = train.groupby('label').size().sort_values(ascending=True)\ncolors = plt.cm.viridis(np.linspace(0, len(class_dist)))\nclass_dist.plot(kind='bar', color=colors)  \nplt.title('Label Distribution', fontsize=20)\nplt.xlabel('Label', fontsize=16)\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:29.886363Z","iopub.execute_input":"2024-08-25T22:03:29.886722Z","iopub.status.idle":"2024-08-25T22:03:30.191238Z","shell.execute_reply.started":"2024-08-25T22:03:29.886675Z","shell.execute_reply":"2024-08-25T22:03:30.189973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for missing values\nmissing = train.isnull().sum()\nmissing","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:30.193063Z","iopub.execute_input":"2024-08-25T22:03:30.193396Z","iopub.status.idle":"2024-08-25T22:03:30.223130Z","shell.execute_reply.started":"2024-08-25T22:03:30.193366Z","shell.execute_reply":"2024-08-25T22:03:30.221754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### No missing values are present in the dataset. ","metadata":{}},{"cell_type":"code","source":"# Check for duplicate values \nduplicate = train.id.duplicated()\ntrain[duplicate]","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:30.224714Z","iopub.execute_input":"2024-08-25T22:03:30.225182Z","iopub.status.idle":"2024-08-25T22:03:30.274288Z","shell.execute_reply.started":"2024-08-25T22:03:30.225141Z","shell.execute_reply":"2024-08-25T22:03:30.273206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### No duplicate id's are present in the dataset. ","metadata":{}},{"cell_type":"markdown","source":"## Cancerous and non-cancerous images","metadata":{}},{"cell_type":"code","source":"# Plot 25 sample images\n\nsample = train.sample(n=25).reset_index(drop=True)\n\nplt.figure(figsize=(9,9))\n\nfor i, row in sample.iterrows():\n    image_path = os.path.join(train_path, str(train['id'].iloc[i]) + '.tif')\n\n    img = mpimg.imread(image_path)    \n    label = row.label\n\n    plt.subplot(5,5,i+1)\n    plt.imshow(img)\n    plt.text(0, -5, f'Class {label}', color='k')\n        \n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T22:03:30.275709Z","iopub.execute_input":"2024-08-25T22:03:30.276811Z","iopub.status.idle":"2024-08-25T22:03:31.598733Z","shell.execute_reply.started":"2024-08-25T22:03:30.276768Z","shell.execute_reply":"2024-08-25T22:03:31.597443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The image format is .tif, size is 96 x 96, channels = 3. ","metadata":{}}]}