{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# import os\n# for dirname, _, filenames in os.walk('/kaggle/input/train'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.711367Z","iopub.execute_input":"2023-08-31T17:00:01.711765Z","iopub.status.idle":"2023-08-31T17:00:01.716483Z","shell.execute_reply.started":"2023-08-31T17:00:01.711733Z","shell.execute_reply":"2023-08-31T17:00:01.715375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%config Completer.use_jedi = False\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport tensorflow.keras \nimport sklearn as sk\nimport skimage as sm\nimport cv2 as cv \nimport matplotlib.pyplot as plt\nimport torch\nimport torchvision.transforms as transforms\nfrom PIL import Image\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:07:33.007056Z","iopub.execute_input":"2023-08-31T18:07:33.007524Z","iopub.status.idle":"2023-08-31T18:07:33.027768Z","shell.execute_reply.started":"2023-08-31T18:07:33.007484Z","shell.execute_reply":"2023-08-31T18:07:33.026842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n<h1 style = \"font-size:30px; font-weight : bold; color : black; text-align: center; border-radius: 10px 15px;\">Breast X-Ray (Breast Cancer): Image Classification with Deep Learning</h1>\n<br>","metadata":{}},{"cell_type":"markdown","source":"Breast cancer image classification using Deep Learning (DL) is a popular research area that aims to improve the accuracy of breast cancer diagnosis. Deep Learning techniques, such as Convolutional Neural Networks (CNNs), have been used to classify breast cancer histopathology images and have shown promising results in achieving the correct classification of breast cancer. There are various deep learning models that have been investigated for this purpose, and ongoing research continues to explore new techniques and improve upon existing ones.\nOur approach is trying simple implementations.","metadata":{}},{"cell_type":"code","source":"physical_devices = tf.config.list_physical_devices('GPU')\nprint(\"Num GPUs:\", len(physical_devices))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.740160Z","iopub.execute_input":"2023-08-31T17:00:01.740607Z","iopub.status.idle":"2023-08-31T17:00:01.748487Z","shell.execute_reply.started":"2023-08-31T17:00:01.740561Z","shell.execute_reply":"2023-08-31T17:00:01.747310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for gpu in physical_devices:\n    tf.config.experimental.set_memory_growth(gpu, True)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.750125Z","iopub.execute_input":"2023-08-31T17:00:01.750519Z","iopub.status.idle":"2023-08-31T17:00:01.756964Z","shell.execute_reply.started":"2023-08-31T17:00:01.750487Z","shell.execute_reply":"2023-08-31T17:00:01.755673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the images 🖼️","metadata":{}},{"cell_type":"code","source":"import os\n\n# Define the root directory of the images\nroot_directory = '/kaggle/input/bioverse-hackathon-a/train'\n\n# Define a dictionary to map directory names to labels\nlabel_mapping = {\n    \"normal\": \"normal\",\n    \"malignant\": \"malignant\",\n    \"beneign\": \"beneign\"\n}\n\n# Initialize the data list\ndata = []\n\n# Traverse through the subdirectories within the root directory and collect image paths and labels\nfor label in label_mapping.keys():\n    label_directory = os.path.join(root_directory, label)\n    for dirname, _, filenames in os.walk(label_directory):\n        for filename in filenames:\n            image_path = os.path.join(dirname, filename)\n            data.append((image_path, label))\n\n# Print the generated data list\nfor image_path, label in data:\n    print(image_path, label)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.758909Z","iopub.execute_input":"2023-08-31T17:00:01.759706Z","iopub.status.idle":"2023-08-31T17:00:01.882444Z","shell.execute_reply.started":"2023-08-31T17:00:01.759670Z","shell.execute_reply":"2023-08-31T17:00:01.881433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.884428Z","iopub.execute_input":"2023-08-31T17:00:01.885043Z","iopub.status.idle":"2023-08-31T17:00:01.892435Z","shell.execute_reply.started":"2023-08-31T17:00:01.885008Z","shell.execute_reply":"2023-08-31T17:00:01.891375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\n\n# Define a dictionary to map labels to numerical values\nlabel_mapping = {\"normal\": 0, \"malignant\": 1, \"beneign\": 2}\n\n# Define lists to store the loaded images and corresponding labels\nimages = []\nlabels = []\n\n# Load and preprocess the images with labels\nfor path, label in data:\n    img = load_img(path, target_size=(224, 224))  \n    img_array = img_to_array(img) / 255.0  # Convert image to numpy array and normalize values\n    images.append(img_array)\n    labels.append(label_mapping[label])\n\n# Convert the lists of images and labels to numpy arrays\nimages_tensor = tf.convert_to_tensor(images)\nlabels_tensor = tf.convert_to_tensor(labels)\n\n# Convert the numerical labels to categorical format\ncategorical_labels = to_categorical(labels_tensor, num_classes=len(label_mapping))\n\n# Print shape of the images and labels arrays\nprint(\"Shape of images array:\", labels_tensor.shape)\nprint(\"Shape of labels array:\", categorical_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:00:01.894120Z","iopub.execute_input":"2023-08-31T17:00:01.894475Z","iopub.status.idle":"2023-08-31T17:01:14.317921Z","shell.execute_reply.started":"2023-08-31T17:00:01.894441Z","shell.execute_reply":"2023-08-31T17:01:14.316864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_array = np.array(images_tensor)\nlabels_array = np.array(labels_tensor)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:14.320877Z","iopub.execute_input":"2023-08-31T17:01:14.321242Z","iopub.status.idle":"2023-08-31T17:01:14.588410Z","shell.execute_reply.started":"2023-08-31T17:01:14.321208Z","shell.execute_reply":"2023-08-31T17:01:14.587333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Indices of images for different categories\nnormal_indices = np.where(labels_array == label_mapping[\"normal\"])[0][:2]\nbenign_indices = np.where(labels_array == label_mapping[\"beneign\"])[0][:2]\nmalignant_indices = np.where(labels_array == label_mapping[\"malignant\"])[0][:2]\nprint(normal_indices)\nprint(benign_indices)\nprint(malignant_indices)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:14.589944Z","iopub.execute_input":"2023-08-31T17:01:14.590322Z","iopub.status.idle":"2023-08-31T17:01:14.601076Z","shell.execute_reply.started":"2023-08-31T17:01:14.590288Z","shell.execute_reply":"2023-08-31T17:01:14.599406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Exploration 🌟","metadata":{}},{"cell_type":"markdown","source":"### Let us see how the data looks 👀","metadata":{}},{"cell_type":"code","source":"# Plotting the images\nfig, axes = plt.subplots(2, 3, figsize=(12, 8))\nfor i, indices in enumerate([normal_indices, benign_indices, malignant_indices]):\n    for j, idx in enumerate(indices):\n        ax = axes[j, i]\n        ax.imshow(images_array[idx])\n        ax.set_title(list(label_mapping.keys())[list(label_mapping.values()).index(labels_array[idx])])\n        ax.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:14.602775Z","iopub.execute_input":"2023-08-31T17:01:14.603249Z","iopub.status.idle":"2023-08-31T17:01:15.621981Z","shell.execute_reply.started":"2023-08-31T17:01:14.603212Z","shell.execute_reply":"2023-08-31T17:01:15.621133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### what about the distribution of the classes 🤔","metadata":{}},{"cell_type":"code","source":"# Plot histograms of class distribution\nplt.figure(figsize=(8, 6))\nplt.hist(labels_array, bins=np.arange(len(label_mapping) + 1) - 0.5, align='mid', rwidth=0.8, color='skyblue')\nplt.xticks(list(label_mapping.values()), list(label_mapping.keys()))\nplt.xlabel('Class')\nplt.ylabel('Count')\nplt.title('Class Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:15.622946Z","iopub.execute_input":"2023-08-31T17:01:15.623290Z","iopub.status.idle":"2023-08-31T17:01:15.882937Z","shell.execute_reply.started":"2023-08-31T17:01:15.623255Z","shell.execute_reply":"2023-08-31T17:01:15.881927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Is there any possible relations in the image intensities 🙈\nSeems that normal breasts have more uniform intensities that are not concentrated on one part of the graph!","metadata":{}},{"cell_type":"code","source":"plt.hist(images_array[6,:,:, 0].ravel(), bins=256, range=(0.2, 1), color='red');","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:15.884611Z","iopub.execute_input":"2023-08-31T17:01:15.885600Z","iopub.status.idle":"2023-08-31T17:01:16.589794Z","shell.execute_reply.started":"2023-08-31T17:01:15.885564Z","shell.execute_reply":"2023-08-31T17:01:16.588837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(images_array[236,:,:, 0].ravel(), bins=256, range=(0.2, 1), color='red');","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:16.591194Z","iopub.execute_input":"2023-08-31T17:01:16.591543Z","iopub.status.idle":"2023-08-31T17:01:17.278622Z","shell.execute_reply.started":"2023-08-31T17:01:16.591516Z","shell.execute_reply":"2023-08-31T17:01:17.277696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(images_array[140,:,:, 0].ravel(), bins=256, range=(0.2, 1), color='red');","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:17.283064Z","iopub.execute_input":"2023-08-31T17:01:17.283991Z","iopub.status.idle":"2023-08-31T17:01:17.977745Z","shell.execute_reply.started":"2023-08-31T17:01:17.283944Z","shell.execute_reply":"2023-08-31T17:01:17.976762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data preparation and preprocessing 🔨","metadata":{}},{"cell_type":"code","source":"num_images = images_array.shape[0]\nnum_pixels = images_array.shape[1] * images_array.shape[2] * images_array.shape[3]\nflattened_images = images_array.reshape(num_images, num_pixels)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:17.979338Z","iopub.execute_input":"2023-08-31T17:01:17.979982Z","iopub.status.idle":"2023-08-31T17:01:17.985731Z","shell.execute_reply.started":"2023-08-31T17:01:17.979934Z","shell.execute_reply":"2023-08-31T17:01:17.984659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test= train_test_split(flattened_images,labels_array, test_size=0.2, random_state=42,shuffle=True,stratify=labels_array)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:17.987403Z","iopub.execute_input":"2023-08-31T17:01:17.987896Z","iopub.status.idle":"2023-08-31T17:01:18.191502Z","shell.execute_reply.started":"2023-08-31T17:01:17.987861Z","shell.execute_reply":"2023-08-31T17:01:18.190170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential, load_model\nfrom keras.layers.core import Dense, Dropout, Activation\nfrom keras.utils import np_utils\nn_classes = 3\nprint(\"Shape before one-hot encoding: \", y_train.shape)\ny_train = np_utils.to_categorical(y_train, n_classes)\ny_test = np_utils.to_categorical(y_test, n_classes)\nprint(\"Shape after one-hot encoding: \", y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:18.193076Z","iopub.execute_input":"2023-08-31T17:01:18.193673Z","iopub.status.idle":"2023-08-31T17:01:18.203591Z","shell.execute_reply.started":"2023-08-31T17:01:18.193630Z","shell.execute_reply":"2023-08-31T17:01:18.202229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n    print(\"Name:\", gpu.name, \"  Type:\", gpu.device_type)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:18.205744Z","iopub.execute_input":"2023-08-31T17:01:18.206195Z","iopub.status.idle":"2023-08-31T17:01:18.217906Z","shell.execute_reply.started":"2023-08-31T17:01:18.206159Z","shell.execute_reply":"2023-08-31T17:01:18.216608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\n\ndevice_lib.list_local_devices()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:18.219571Z","iopub.execute_input":"2023-08-31T17:01:18.220092Z","iopub.status.idle":"2023-08-31T17:01:18.239449Z","shell.execute_reply.started":"2023-08-31T17:01:18.220056Z","shell.execute_reply":"2023-08-31T17:01:18.238180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.test.is_gpu_available()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:01:18.241391Z","iopub.execute_input":"2023-08-31T17:01:18.241801Z","iopub.status.idle":"2023-08-31T17:01:18.259541Z","shell.execute_reply.started":"2023-08-31T17:01:18.241746Z","shell.execute_reply":"2023-08-31T17:01:18.258409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Creation 🎭","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers, callbacks\n\n\nearly_stopping = callbacks.EarlyStopping(\n    min_delta=0.01, # minimium amount of change to count as an improvement\n    patience=20, # how many epochs to wait before stopping\n    restore_best_weights=True)\n\nmodel = keras.Sequential([\n    layers.BatchNormalization(input_shape=(X_train.shape[1],)),\n    layers.Dropout(0.6),\n    layers.Dense(1024, activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(512, activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.4),\n    layers.Dense(128, activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.4),\n    layers.Dense(3, activation='softmax'),\n])\n\n\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='SGD')\n# training the model and saving metrics in history\nhistory = model.fit(X_train, y_train,\n          batch_size=8, epochs=100,\n          verbose=2,\n          validation_data=(X_test, y_test),\n                   callbacks=[early_stopping])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T18:07:42.026004Z","iopub.execute_input":"2023-08-31T18:07:42.026429Z","iopub.status.idle":"2023-08-31T18:07:52.165265Z","shell.execute_reply.started":"2023-08-31T18:07:42.026394Z","shell.execute_reply":"2023-08-31T18:07:52.163723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Results 😶‍🌫️","metadata":{}},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\n\nhistory_df.loc[:, ['loss', 'val_loss']].plot()\nhistory_df.loc[:, ['accuracy', 'val_accuracy']].plot()\n\nprint((\"Best Validation Loss: {:0.4f}\" +\\\n      \"\\nBest Validation Accuracy: {:0.4f}\")\\\n      .format(history_df['val_loss'].min(), \n              history_df['val_accuracy'].max()))","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.281859Z","iopub.status.idle":"2023-08-31T17:57:40.282218Z","shell.execute_reply.started":"2023-08-31T17:57:40.282043Z","shell.execute_reply":"2023-08-31T17:57:40.282059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save Model 📝","metadata":{}},{"cell_type":"code","source":"# # saving the model\n# save_dir = \"/kaggle/working/\"\n# model_name = 'bioverse.h5'\n# model_path = os.path.join(save_dir, model_name)\n# model.save(model_path)\n# print('Saved trained model at %s ' % model_path)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.284466Z","iopub.status.idle":"2023-08-31T17:57:40.285499Z","shell.execute_reply.started":"2023-08-31T17:57:40.285203Z","shell.execute_reply":"2023-08-31T17:57:40.285247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_model = load_model('/kaggle/working/bioverse.h5')\n\nloss_and_metrics = cancer_model.evaluate(X_test, y_test, verbose=2)\n\nprint(\"Test Loss\", loss_and_metrics[0])\nprint(\"Test Accuracy\", loss_and_metrics[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.286765Z","iopub.status.idle":"2023-08-31T17:57:40.287575Z","shell.execute_reply.started":"2023-08-31T17:57:40.287328Z","shell.execute_reply":"2023-08-31T17:57:40.287360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\ny_pred = cancer_model.predict(X_test)\n\nmicro_f1 = f1_score(np.argmax(y_test,axis=1), np.argmax(y_pred,axis=1), average='macro')\n\nprint(\"Micro F1 Score:\", micro_f1)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.288966Z","iopub.status.idle":"2023-08-31T17:57:40.289765Z","shell.execute_reply.started":"2023-08-31T17:57:40.289530Z","shell.execute_reply":"2023-08-31T17:57:40.289555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Define the root directory of the images\nroot_directory = '/kaggle/input/bioverse-hackathon-a/test'\n\n\n\n# Initialize the data list\ndata = []\n    \nfor dirname, _, filenames in os.walk('/kaggle/input/bioverse-hackathon-a/test'):\n    for filename in filenames:\n            image_path = os.path.join(dirname, filename)\n            data.append((image_path, label))\n\n# Print the generated data list\nfor image_path, label in data:\n    print(image_path, label)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.291120Z","iopub.status.idle":"2023-08-31T17:57:40.291716Z","shell.execute_reply.started":"2023-08-31T17:57:40.291476Z","shell.execute_reply":"2023-08-31T17:57:40.291499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = []\n\n# Load and preprocess the images with labels\nfor path, label in data:\n    img = load_img(path, target_size=(224, 224))  \n    img_array = img_to_array(img) / 255.0  # Convert image to numpy array and normalize values\n    test.append(img_array)\n\ntest=np.array(test)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.294561Z","iopub.status.idle":"2023-08-31T17:57:40.295369Z","shell.execute_reply.started":"2023-08-31T17:57:40.295124Z","shell.execute_reply":"2023-08-31T17:57:40.295147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images = test.shape[0]\nnum_pixels = test.shape[1] * test.shape[2] * test.shape[3]\nflattened_test = test.reshape(num_images, num_pixels)\nflattened_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.296736Z","iopub.status.idle":"2023-08-31T17:57:40.297516Z","shell.execute_reply.started":"2023-08-31T17:57:40.297278Z","shell.execute_reply":"2023-08-31T17:57:40.297301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(filenames)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.298890Z","iopub.status.idle":"2023-08-31T17:57:40.299650Z","shell.execute_reply.started":"2023-08-31T17:57:40.299408Z","shell.execute_reply":"2023-08-31T17:57:40.299431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cancer_model.predict(flattened_test)\npreds=np.argmax(y_pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.301110Z","iopub.status.idle":"2023-08-31T17:57:40.301958Z","shell.execute_reply.started":"2023-08-31T17:57:40.301675Z","shell.execute_reply":"2023-08-31T17:57:40.301700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"label_mapping = {\"normal\": 0, \"malignant\": 1, \"beneign\": 2}\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\nsubmn = pd.DataFrame({'filename' : filenames,'class': preds},)\nsubmn","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.303337Z","iopub.status.idle":"2023-08-31T17:57:40.304119Z","shell.execute_reply.started":"2023-08-31T17:57:40.303875Z","shell.execute_reply":"2023-08-31T17:57:40.303899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlabel_mapping = {0: \"normal\", 1: \"malignant\", 2: \"beneign\"}\n\n# Replace the numerical labels with string labels\nsubmn['class'] = submn['class'].replace(label_mapping)\n\nsubmn","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.305459Z","iopub.status.idle":"2023-08-31T17:57:40.306241Z","shell.execute_reply.started":"2023-08-31T17:57:40.305999Z","shell.execute_reply":"2023-08-31T17:57:40.306023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submn.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.307588Z","iopub.status.idle":"2023-08-31T17:57:40.308389Z","shell.execute_reply.started":"2023-08-31T17:57:40.308130Z","shell.execute_reply":"2023-08-31T17:57:40.308153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Inference ","metadata":{}},{"cell_type":"code","source":"num_images = 1\nnum_pixels = test[0,:,:,:].shape[0] * test[0,:,:,:].shape[1] * test[0,:,:,:].shape[2]\nflattened_test = test[0,:,:,:].reshape(num_images, num_pixels)\nflattened_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.309744Z","iopub.status.idle":"2023-08-31T17:57:40.310536Z","shell.execute_reply.started":"2023-08-31T17:57:40.310297Z","shell.execute_reply":"2023-08-31T17:57:40.310320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nstart = time.time() ## record start time\ny_pred = cancer_model.predict(flattened_test)\npreds=np.argmax(y_pred,axis=1)\nend = time.time() ## records end time\nprint(end - start)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:57:40.311916Z","iopub.status.idle":"2023-08-31T17:57:40.312671Z","shell.execute_reply.started":"2023-08-31T17:57:40.312428Z","shell.execute_reply":"2023-08-31T17:57:40.312452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}