{"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from __future__ import print_function\nimport numpy as np\nimport warnings\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport os\nimport keras\n!pip install keras_applications\nfrom keras.models import Model\nfrom keras.layers import Flatten\nfrom keras.layers import Dense\nfrom keras.layers import Input\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import GlobalMaxPooling2D\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.preprocessing import image\nfrom tensorflow.keras.utils import get_source_inputs \nfrom tensorflow.python.keras.utils import layer_utils \nfrom tensorflow.python.keras.utils.data_utils import get_file\nfrom keras import backend as K\nfrom tensorflow.keras.optimizers import RMSprop\n\nfrom keras.applications.imagenet_utils import decode_predictions\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom keras_applications.imagenet_utils import _obtain_input_shape\nfrom tensorflow.keras.utils import get_source_inputs","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-24T15:42:38.896988Z","iopub.execute_input":"2024-10-24T15:42:38.897758Z","iopub.status.idle":"2024-10-24T15:42:51.100889Z","shell.execute_reply.started":"2024-10-24T15:42:38.897700Z","shell.execute_reply":"2024-10-24T15:42:51.099677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def VGGupdated(input_tensor=None,classes=6):    \n   \n    img_rows, img_cols = 150, 150   # by default size is 224,224\n    img_channels = 3\n\n    img_dim = (img_rows, img_cols, img_channels)\n   \n    img_input = Input(shape=img_dim)\n    \n    # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n\n # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n\n    \n    # Classification block\n    x = Flatten(name='flatten')(x)\n    x = Dense(4096, activation='relu', name='fc1')(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    x = Dense(classes, activation='softmax', name='predictions')(x)\n    \n     # Create model.\n   \n     \n    model = Model(inputs = img_input, outputs = x, name='VGGdemo')\n\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.103342Z","iopub.execute_input":"2024-10-24T15:42:51.103663Z","iopub.status.idle":"2024-10-24T15:42:51.119818Z","shell.execute_reply.started":"2024-10-24T15:42:51.103632Z","shell.execute_reply":"2024-10-24T15:42:51.118887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = VGGupdated(classes = 2) \nmodel.compile(optimizer=\"adam\", loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.120825Z","iopub.execute_input":"2024-10-24T15:42:51.121057Z","iopub.status.idle":"2024-10-24T15:42:51.230484Z","shell.execute_reply.started":"2024-10-24T15:42:51.121036Z","shell.execute_reply":"2024-10-24T15:42:51.229779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport os\n\nPATH = '/kaggle/input/hms-harmful-brain-activity-classification'\ndataset_path = os.listdir(PATH)\n\nfolders = os.listdir(PATH)\nprint (folders)  #what kinds of files are in this dataset\n\nprint(\"No. of Files/Folders found: \", len(dataset_path))","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.231472Z","iopub.execute_input":"2024-10-24T15:42:51.231710Z","iopub.status.idle":"2024-10-24T15:42:51.239481Z","shell.execute_reply.started":"2024-10-24T15:42:51.231689Z","shell.execute_reply":"2024-10-24T15:42:51.238544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(PATH+\"/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.241700Z","iopub.execute_input":"2024-10-24T15:42:51.242055Z","iopub.status.idle":"2024-10-24T15:42:51.405562Z","shell.execute_reply.started":"2024-10-24T15:42:51.242032Z","shell.execute_reply":"2024-10-24T15:42:51.404685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's check how many samples for each category are present\nprint(\"Total number of data in the dataset: \", len(df))\n\ndata_count = df['expert_consensus'].value_counts()\n\nprint(\"expert_consensus data in each category: \")\nprint(data_count)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.406791Z","iopub.execute_input":"2024-10-24T15:42:51.407130Z","iopub.status.idle":"2024-10-24T15:42:51.429035Z","shell.execute_reply.started":"2024-10-24T15:42:51.407098Z","shell.execute_reply":"2024-10-24T15:42:51.428168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(columns=['eeg_sub_id', 'eeg_label_offset_seconds', \"spectrogram_sub_id\", \"spectrogram_label_offset_seconds\",\"label_id\",])\ndf=df.drop_duplicates(subset=(\"spectrogram_id\"))\ndata_count = df['expert_consensus'].value_counts()\n\nprint(\"expert_consensus data in each category: \")\nprint(data_count)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.430213Z","iopub.execute_input":"2024-10-24T15:42:51.430539Z","iopub.status.idle":"2024-10-24T15:42:51.450816Z","shell.execute_reply.started":"2024-10-24T15:42:51.430509Z","shell.execute_reply":"2024-10-24T15:42:51.449701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.replace({'GRDA':'NO'}, inplace=True)\ndf.replace({'Other':'NO'}, inplace=True)\ndf.replace({'GPD':'NO'}, inplace=True)\ndf.replace({'LRDA':'NO'}, inplace=True)\ndf.replace({'LPD':'NO'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.451936Z","iopub.execute_input":"2024-10-24T15:42:51.452254Z","iopub.status.idle":"2024-10-24T15:42:51.469192Z","shell.execute_reply.started":"2024-10-24T15:42:51.452223Z","shell.execute_reply":"2024-10-24T15:42:51.468471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder , OneHotEncoder\ny = df['expert_consensus'].values\n\ny_labelencoder = LabelEncoder ()\ny = y_labelencoder.fit_transform (y)\nprint (y)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.470194Z","iopub.execute_input":"2024-10-24T15:42:51.470454Z","iopub.status.idle":"2024-10-24T15:42:51.479153Z","shell.execute_reply.started":"2024-10-24T15:42:51.470431Z","shell.execute_reply":"2024-10-24T15:42:51.478246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming y is our target variable as a 1D array or DataFrame column\ny = y.reshape(-1, 1)  # Reshape to column vector \n# Create OneHotEncoder object with specified categories\nonehotencoder = OneHotEncoder(categories='auto')  # 'auto' automatically determines categories\nY = onehotencoder.fit_transform(y).toarray()\nprint(Y.shape)  # This should print the shape of the one-hot encoded output\n","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.480353Z","iopub.execute_input":"2024-10-24T15:42:51.480929Z","iopub.status.idle":"2024-10-24T15:42:51.490949Z","shell.execute_reply.started":"2024-10-24T15:42:51.480896Z","shell.execute_reply":"2024-10-24T15:42:51.490070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load spectrogram data from Parquet file\necg_spectrogram = pd.read_parquet(PATH+'/train_spectrograms/353733.parquet')","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.492169Z","iopub.execute_input":"2024-10-24T15:42:51.492694Z","iopub.status.idle":"2024-10-24T15:42:51.539655Z","shell.execute_reply.started":"2024-10-24T15:42:51.492661Z","shell.execute_reply":"2024-10-24T15:42:51.538774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom tensorflow.keras.preprocessing.image import img_to_array, array_to_img\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\n\n# Resizing and Preprocessing Spectrogram Images\ndef preprocess_spectrogram(spectrogram, target_size=(150, 150)):\n    # Normalizing spectrogram data to [0, 255] range \n    if spectrogram.dtype == np.float64:\n        spectrogram = (spectrogram - spectrogram.min()) / (spectrogram.max() - spectrogram.min()) * 255.0\n        spectrogram = spectrogram.astype(np.uint8)\n\n    # Resize spectrogram to target size using OpenCV\n    resized_spectrogram = cv2.resize(spectrogram, target_size)\n    # Convert to RGB image (3 channels)\n    rgb_spectrogram = cv2.cvtColor(resized_spectrogram, cv2.COLOR_GRAY2RGB)\n    # Convert to array and preprocess according to VGG-16 requirements\n    preprocessed_spectrogram = img_to_array(rgb_spectrogram)\n    preprocessed_spectrogram = preprocess_input(preprocessed_spectrogram)\n    return preprocessed_spectrogram\n\n# Preprocess first spectrogram in the DataFrame\nfirst_spectrogram = ecg_spectrogram.iloc[0, :].values  #each row represents a spectrogram\npreprocessed_spectrogram = preprocess_spectrogram(first_spectrogram)\nprint(preprocessed_spectrogram.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.540864Z","iopub.execute_input":"2024-10-24T15:42:51.541185Z","iopub.status.idle":"2024-10-24T15:42:51.617897Z","shell.execute_reply.started":"2024-10-24T15:42:51.541153Z","shell.execute_reply":"2024-10-24T15:42:51.616990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath='/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/'\nimages=[]\n# Ignore all the warnings temporarily\nwith warnings.catch_warnings():\n    warnings.simplefilter(\"ignore\")\n    for f in os.listdir(path):\n        file_path=path+f\n        df = pd.read_parquet(file_path)\n        first_spectrogram = df.iloc[0, :].values  # Assuming each row represents a spectrogram\n        preprocessed_spectrogram = preprocess_spectrogram(first_spectrogram)\n        images.append(preprocessed_spectrogram)","metadata":{"execution":{"iopub.status.busy":"2024-10-24T15:42:51.619007Z","iopub.execute_input":"2024-10-24T15:42:51.619345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(images)\nimages.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\n\n\nimages, Y = shuffle(images, Y, random_state=1)\n\ntrain_x, test_x, train_y, test_y = train_test_split(images, Y, test_size=0.2, random_state=42)\n\n#inspect the shape of the training and testing.\nprint(train_x.shape)\nprint(train_y.shape)\nprint(test_x.shape)\nprint(test_y.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(train_x, train_y, epochs=100,validation_split=0.2, verbose=2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n#Predictions on the test set\npred_y = model.predict(test_x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.evaluate(test_x, test_y)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Test Accuracy = \" + str(preds[1]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import precision_score, recall_score, f1_score\nprecision = precision_score(test_y.argmax(axis=1), pred_y.argmax(axis=1), average='weighted')\nrecall = recall_score(test_y.argmax(axis=1),pred_y.argmax(axis=1), average='weighted')\nf1 = f1_score(test_y.argmax(axis=1),pred_y.argmax(axis=1), average='weighted')\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"F1 Score:\", f1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(test_y.argmax(axis=1), pred_y.argmax(axis=1))\n\nprint(\"Confusion Matrix:\")\nprint(cm)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the training and validation loss at each epoch\nloss = hist.history['loss']\nval_loss = hist.history['val_loss']\nepochs = range(1, len(loss) + 1)\nplt.figure(figsize=(8, 6))  \nplt.plot(epochs, loss, 'y', label='Training Loss')\nplt.plot(epochs, val_loss, 'r', label='Validation Loss')\nplt.title('Loss Curve (Training and validation loss)')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the training and validation accuracy at each epoch\nacc = hist.history['accuracy']\nval_acc = hist.history['val_accuracy']\nplt.figure(figsize=(8, 6))\nplt.plot(epochs, acc, 'y', label='Training Accuracy')\nplt.plot(epochs, val_acc, 'r', label='Validation Accuracy')\nplt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import EfficientNetV2B0\nfrom keras.layers import GlobalAveragePooling2D, Dense\nfrom keras.models import Model\nfrom keras.optimizers import Adam\nfrom sklearn.metrics import precision_score, recall_score, f1_score\nimport matplotlib.pyplot as plt\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define EfficientNetV2 model\nefficientnet_base = EfficientNetV2B0(include_top=False, weights='imagenet', input_shape=(150, 150, 3))\n\n# Adding GlobalAveragePooling and Dense layers\nx = efficientnet_base.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(4096, activation='relu')(x)\nx = Dense(2, activation='softmax')(x)  # Assuming binary classification\n\n# Creating the EfficientNetV2 model\nefficientnet_model = Model(inputs=efficientnet_base.input, outputs=x)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile EfficientNetV2 model\nefficientnet_model.compile(optimizer=\"adam\", loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train EfficientNetV2 model\nefficientnet_history = efficientnet_model.fit(train_x, train_y, epochs=100, validation_split=0.2, verbose=2)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate EfficientNetV2 model\nefficientnet_preds = efficientnet_model.evaluate(test_x, test_y)\nprint(\"EfficientNetV2 Loss =\", efficientnet_preds[0])\nprint(\"EfficientNetV2 Test Accuracy =\", efficientnet_preds[1])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions and calculate metrics\nefficientnet_pred_y = efficientnet_model.predict(test_x)\n\nefficientnet_precision = precision_score(test_y.argmax(axis=1), efficientnet_pred_y.argmax(axis=1), average='weighted')\nefficientnet_recall = recall_score(test_y.argmax(axis=1), efficientnet_pred_y.argmax(axis=1), average='weighted')\nefficientnet_f1 = f1_score(test_y.argmax(axis=1), efficientnet_pred_y.argmax(axis=1), average='weighted')\n\nprint(\"EfficientNetV2 Precision:\", efficientnet_precision)\nprint(\"EfficientNetV2 Recall:\", efficientnet_recall)\nprint(\"EfficientNetV2 F1 Score:\", efficientnet_f1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot loss curve\nplt.figure(figsize=(8, 6))\nplt.plot(efficientnet_history.history['loss'], label='EfficientNetV2 Training Loss')\nplt.plot(efficientnet_history.history['val_loss'], label='EfficientNetV2 Validation Loss')\nplt.title('EfficientNetV2 Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\n# Plot accuracy curve\nplt.figure(figsize=(8, 6))\nplt.plot(efficientnet_history.history['accuracy'], label='EfficientNetV2 Training Accuracy')\nplt.plot(efficientnet_history.history['val_accuracy'], label='EfficientNetV2 Validation Accuracy')\nplt.title('EfficientNetV2 Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# Calculate the confusion matrix\ncm = confusion_matrix(test_y.argmax(axis=1), efficientnet_pred_y.argmax(axis=1))\n\n# Plot the confusion matrix\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=y_labelencoder.classes_, yticklabels=y_labelencoder.classes_)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gather results\nresults = {\n    'Model': ['VGG', 'EfficientNetV2'],\n    'Test Loss': [preds[0], efficientnet_preds[0]],\n    'Test Accuracy': [preds[1], efficientnet_preds[1]],\n    'Precision': [precision, efficientnet_precision],\n    'Recall': [recall, efficientnet_recall],\n    'F1 Score': [f1, efficientnet_f1]\n}\n\nresults_df = pd.DataFrame(results)\nprint(results_df)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot comparison of Test Accuracy and Loss\nfig, ax = plt.subplots(1, 2, figsize=(14, 6))\n\n# Accuracy\nax[0].bar(results_df['Model'], results_df['Test Accuracy'], color=['blue', 'orange'])\nax[0].set_title('Test Accuracy Comparison')\nax[0].set_ylabel('Accuracy')\n\n# Loss\nax[1].bar(results_df['Model'], results_df['Test Loss'], color=['blue', 'orange'])\nax[1].set_title('Test Loss Comparison')\nax[1].set_ylabel('Loss')\n\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrices for both models\nfig, ax = plt.subplots(1, 2, figsize=(12, 5))\n\n# VGG Confusion Matrix\ncm_vgg = confusion_matrix(test_y.argmax(axis=1), pred_y.argmax(axis=1))\nsns.heatmap(cm_vgg, annot=True, fmt='d', cmap='Blues', ax=ax[0], xticklabels=y_labelencoder.classes_, yticklabels=y_labelencoder.classes_)\nax[0].set_title('VGG Confusion Matrix')\nax[0].set_xlabel('Predicted')\nax[0].set_ylabel('True')\n\n# EfficientNetV2 Confusion Matrix\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax[1], xticklabels=y_labelencoder.classes_, yticklabels=y_labelencoder.classes_)\nax[1].set_title('EfficientNetV2 Confusion Matrix')\nax[1].set_xlabel('Predicted')\nax[1].set_ylabel('True')\n\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import VGG19\nfrom keras.models import Model\nfrom keras.layers import Flatten, Dense\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix\n\n# Load the VGG19 model\ndef VGG19_model(input_shape=(150, 150, 3), num_classes=6):\n    # Load the VGG19 model without the top classification layers\n    base_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n\n    # Add custom classification layers on top of VGG19\n    x = Flatten(name='flatten')(base_model.output)\n    x = Dense(4096, activation='relu', name='fc1')(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    predictions = Dense(num_classes, activation='softmax', name='predictions')(x)\n\n    # Create the model\n    model = Model(inputs=base_model.input, outputs=predictions)\n\n    # Freeze the VGG19 base model layers (optional)\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the VGG19 model\nvgg19_model = VGG19_model(input_shape=(150, 150, 3), num_classes=2)  # Change num_classes if needed\nvgg19_model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhist_vgg19 = vgg19_model.fit(train_x, train_y, epochs=100, validation_split=0.2, verbose=2)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the VGG19 model\nvgg19_pred_y = vgg19_model.predict(test_x)\nvgg19_preds = vgg19_model.evaluate(test_x, test_y)\nprint(\"VGG19 Loss =\", vgg19_preds[0])\nprint(\"VGG19 Test Accuracy =\", vgg19_preds[1])\n\n# Calculate precision, recall, and F1 score for VGG19\nvgg19_precision = precision_score(test_y.argmax(axis=1), vgg19_pred_y.argmax(axis=1), average='weighted')\nvgg19_recall = recall_score(test_y.argmax(axis=1), vgg19_pred_y.argmax(axis=1), average='weighted')\nvgg19_f1 = f1_score(test_y.argmax(axis=1), vgg19_pred_y.argmax(axis=1), average='weighted')\n\nprint(\"VGG19 Precision:\", vgg19_precision)\nprint(\"VGG19 Recall:\", vgg19_recall)\nprint(\"VGG19 F1 Score:\", vgg19_f1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrix for VGG19\ncm_vgg19 = confusion_matrix(test_y.argmax(axis=1), vgg19_pred_y.argmax(axis=1))\nprint(\"VGG19 Confusion Matrix:\")\nprint(cm_vgg19)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns  # Add this line\n\n# Plot confusion matrix for VGG19\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm_vgg19, annot=True, fmt='d', cmap='Blues', xticklabels=y_labelencoder.classes_, yticklabels=y_labelencoder.classes_)\nplt.title('VGG19 Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('True Label')\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Evaluation metrics for both models\nmetrics = {\n    'Model': ['VGG', 'VGG19'],\n    'Loss': [0.4713, 1.0849],  # Adjust with your actual loss values\n    'Test Accuracy': [0.8223, 0.8223],  # Same test accuracy\n    'Precision': [0.6761, 0.6761],  # Same precision\n    'Recall': [0.8223, 0.8223],  # Same recall\n    'F1 Score': [0.7421, 0.7421]  # Same F1 score\n}\n\n# Creating a DataFrame for easier plotting\nimport pandas as pd\n\ndf_metrics = pd.DataFrame(metrics)\n\n# Plotting the metrics for comparison\nplt.figure(figsize=(12, 8))\n\n# Loss comparison\nplt.subplot(2, 3, 1)\nplt.bar(df_metrics['Model'], df_metrics['Loss'], color=['blue', 'orange'])\nplt.title('Loss Comparison')\nplt.ylabel('Loss')\n\n# Test Accuracy comparison\nplt.subplot(2, 3, 2)\nplt.bar(df_metrics['Model'], df_metrics['Test Accuracy'], color=['blue', 'orange'])\nplt.title('Test Accuracy Comparison')\nplt.ylabel('Accuracy')\n\n# Precision comparison\nplt.subplot(2, 3, 3)\nplt.bar(df_metrics['Model'], df_metrics['Precision'], color=['blue', 'orange'])\nplt.title('Precision Comparison')\nplt.ylabel('Precision')\n\n# Recall comparison\nplt.subplot(2, 3, 4)\nplt.bar(df_metrics['Model'], df_metrics['Recall'], color=['blue', 'orange'])\nplt.title('Recall Comparison')\nplt.ylabel('Recall')\n\n# F1 Score comparison\nplt.subplot(2, 3, 5)\nplt.bar(df_metrics['Model'], df_metrics['F1 Score'], color=['blue', 'orange'])\nplt.title('F1 Score Comparison')\nplt.ylabel('F1 Score')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Create a DataFrame with the evaluation results\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2'],\n    'Loss': [0.471, 1.084, 1.825],\n    'Test Accuracy': [0.822, 0.822, 0.704],\n    'Precision': [0.676, 0.676, 0.710],\n    'Recall': [0.822, 0.822, 0.704],\n    'F1 Score': [0.742, 0.742, 0.707],\n}\n\ndf = pd.DataFrame(data)\n\n# Plot the comparison\nplt.figure(figsize=(12, 8))\n\n# Plot Accuracy\nplt.subplot(2, 2, 1)\nsns.barplot(x='Model', y='Test Accuracy', data=df, palette='viridis')\nplt.title('Test Accuracy Comparison')\nplt.ylim(0, 1)\n\n# Plot Loss\nplt.subplot(2, 2, 2)\nsns.barplot(x='Model', y='Loss', data=df, palette='viridis')\nplt.title('Loss Comparison')\nplt.ylim(0, max(df['Loss']) + 0.5)\n\n# Plot Precision\nplt.subplot(2, 2, 3)\nsns.barplot(x='Model', y='Precision', data=df, palette='viridis')\nplt.title('Precision Comparison')\nplt.ylim(0, 1)\n\n# Plot F1 Score\nplt.subplot(2, 2, 4)\nsns.barplot(x='Model', y='F1 Score', data=df, palette='viridis')\nplt.title('F1 Score Comparison')\nplt.ylim(0, 1)\n\nplt.tight_layout()\nplt.show()\n\n# Display DataFrame\nprint(df)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 1: Import Libraries\nfrom keras.applications import ResNet152V2\nfrom keras.layers import GlobalAveragePooling2D, Dense\nfrom keras.models import Model\nfrom keras.optimizers import Adam\nfrom sklearn.metrics import precision_score, recall_score, f1_score\nimport matplotlib.pyplot as plt\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 2: Define the ResNet152V2 model\nresnet152_base = ResNet152V2(include_top=False, weights='imagenet', input_shape=(150, 150, 3))\nresnet152_x = resnet152_base.output\nresnet152_x = GlobalAveragePooling2D()(resnet152_x)\nresnet152_x = Dense(4096, activation='relu')(resnet152_x)\nresnet152_x = Dense(2, activation='softmax')(resnet152_x)  # Assuming binary classification\n\nresnet152_model = Model(inputs=resnet152_base.input, outputs=resnet152_x)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 3: Compile the ResNet152V2 model\nresnet152_model.compile(optimizer=\"adam\", loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 4: Train the ResNet152V2 model\nresnet152_history = resnet152_model.fit(train_x, train_y, epochs=100, validation_split=0.2, verbose=2)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 5: Evaluate the ResNet152V2 model\nresnet152_preds = resnet152_model.evaluate(test_x, test_y)\nprint(\"ResNet152V2 Loss =\", resnet152_preds[0])\nprint(\"ResNet152V2 Test Accuracy =\", resnet152_preds[1])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 6: Precision, Recall, F1 Score\nresnet152_pred_y = resnet152_model.predict(test_x)\nresnet152_precision = precision_score(test_y.argmax(axis=1), resnet152_pred_y.argmax(axis=1), average='weighted')\nresnet152_recall = recall_score(test_y.argmax(axis=1), resnet152_pred_y.argmax(axis=1), average='weighted')\nresnet152_f1 = f1_score(test_y.argmax(axis=1), resnet152_pred_y.argmax(axis=1), average='weighted')\nprint(\"ResNet152V2 Precision:\", resnet152_precision)\nprint(\"ResNet152V2 Recall:\", resnet152_recall)\nprint(\"ResNet152V2 F1 Score:\", resnet152_f1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 7: Loss and Accuracy Curves\nplt.figure(figsize=(8, 6))\nplt.plot(resnet152_history.history['loss'], label='ResNet152V2 Training Loss')\nplt.plot(resnet152_history.history['val_loss'], label='ResNet152V2 Validation Loss')\nplt.title('ResNet152V2 Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\nplt.figure(figsize=(8, 6))\nplt.plot(resnet152_history.history['accuracy'], label='ResNet152V2 Training Accuracy')\nplt.plot(resnet152_history.history['val_accuracy'], label='ResNet152V2 Validation Accuracy')\nplt.title('ResNet152V2 Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Model performance metrics\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],  # ResNet152V2 loss\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],  # ResNet152V2 accuracy\n    'Precision': [0.676, 0.676, 0.710, 0.676],  # ResNet152V2 precision\n    'Recall': [0.822, 0.822, 0.704, 0.822],  # ResNet152V2 recall\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]  # ResNet152V2 F1 score\n}\n\nmodel_comparison_df = pd.DataFrame(data)\n\n# Display the comparison\nprint(model_comparison_df)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Model names\nmodels = ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2']\n\n# Metrics\nloss = [0.471, 1.084, 1.825, 0.468]\naccuracy = [0.822, 0.822, 0.704, 0.822]\nprecision = [0.676, 0.676, 0.710, 0.676]\nrecall = [0.822, 0.822, 0.704, 0.822]\nf1_score = [0.742, 0.742, 0.707, 0.742]\n\n# Set up the figure and axes\nfig, axs = plt.subplots(3, 2, figsize=(12, 12))\n\n# Loss bar chart\naxs[0, 0].bar(models, loss, color='red')\naxs[0, 0].set_title('Model Loss')\naxs[0, 0].set_ylabel('Loss')\n\n# Test Accuracy bar chart\naxs[0, 1].bar(models, accuracy, color='blue')\naxs[0, 1].set_title('Test Accuracy')\naxs[0, 1].set_ylabel('Accuracy')\n\n# Precision bar chart\naxs[1, 0].bar(models, precision, color='green')\naxs[1, 0].set_title('Precision')\naxs[1, 0].set_ylabel('Precision')\n\n# Recall bar chart\naxs[1, 1].bar(models, recall, color='orange')\naxs[1, 1].set_title('Recall')\naxs[1, 1].set_ylabel('Recall')\n\n# F1 Score bar chart\naxs[2, 0].bar(models, f1_score, color='purple')\naxs[2, 0].set_title('F1 Score')\naxs[2, 0].set_ylabel('F1 Score')\n\n# Hide the last subplot (bottom right) since we have only five metrics\naxs[2, 1].axis('off')\n\n# Adjust layout\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Model names\nmodels = ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2']\n\n# Metrics\nloss = [0.471, 1.084, 1.825, 0.468]\naccuracy = [0.822, 0.822, 0.704, 0.822]\nprecision = [0.676, 0.676, 0.710, 0.676]\nrecall = [0.822, 0.822, 0.704, 0.822]\nf1_score = [0.742, 0.742, 0.707, 0.742]\n\n# Set up the bar positions and width\nbar_width = 0.15\nx = np.arange(len(models))\n\n# Create subplots for each metric\nfig, ax = plt.subplots(figsize=(12, 8))\n\n# Create bars for each metric\nbars1 = ax.bar(x - 2*bar_width, loss, bar_width, label='Loss', color='red')\nbars2 = ax.bar(x - bar_width, accuracy, bar_width, label='Accuracy', color='blue')\nbars3 = ax.bar(x, precision, bar_width, label='Precision', color='green')\nbars4 = ax.bar(x + bar_width, recall, bar_width, label='Recall', color='orange')\nbars5 = ax.bar(x + 2*bar_width, f1_score, bar_width, label='F1 Score', color='purple')\n\n# Add labels and title\nax.set_xlabel('Models', fontsize=14)\nax.set_ylabel('Scores', fontsize=14)\nax.set_title('Model Performance Comparison', fontsize=16)\nax.set_xticks(x)\nax.set_xticklabels(models)\nax.legend()\n\n# Add value labels on top of each bar\nfor bars in [bars1, bars2, bars3, bars4, bars5]:\n    for bar in bars:\n        height = bar.get_height()\n        ax.annotate(f'{height:.3f}', \n                    xy=(bar.get_x() + bar.get_width() / 2, height), \n                    xytext=(0, 3),  # 3 points vertical offset\n                    textcoords=\"offset points\", \n                    ha='center', va='bottom')\n\n# Show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nfrom sklearn.ensemble import VotingRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.metrics import mean_squared_error\n\n# Step 1: Create a DataFrame with the provided model performance data\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],\n    'Precision': [0.676, 0.676, 0.710, 0.676],\n    'Recall': [0.822, 0.822, 0.704, 0.822],\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]\n}\n\ndf = pd.DataFrame(data)\n\n# Step 2: Define the features (X) and target (y)\nX = df[['Test Accuracy', 'Precision', 'Recall', 'F1 Score']]\ny = df['Loss']\n\n# Step 3: Create individual regression models\nmodel1 = LinearRegression()\nmodel2 = DecisionTreeRegressor()\n\n# Step 4: Create a Voting Regressor\nvoting_regressor = VotingRegressor(estimators=[\n    ('lr', model1),\n    ('dt', model2)\n])\n\n# Step 5: Fit the voting regressor on the data\nvoting_regressor.fit(X, y)\n\n# Step 6: Make predictions on the same dataset\ny_pred = voting_regressor.predict(X)\n\n# Step 7: Evaluate the performance\nmse = mean_squared_error(y, y_pred)\nprint(f'Mean Squared Error of Voting Regressor: {mse:.2f}')\n\n# Optional: Compare predictions with actual values\nresults = pd.DataFrame({'Actual Loss': y, 'Predicted Loss': y_pred})\nprint(results)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-04T05:28:23.481719Z","iopub.execute_input":"2024-11-04T05:28:23.482073Z","iopub.status.idle":"2024-11-04T05:28:23.505696Z","shell.execute_reply.started":"2024-11-04T05:28:23.482044Z","shell.execute_reply":"2024-11-04T05:28:23.504788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Step 1: Create a DataFrame with the provided model performance data\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],\n    'Precision': [0.676, 0.676, 0.710, 0.676],\n    'Recall': [0.822, 0.822, 0.704, 0.822],\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]\n}\n\ndf = pd.DataFrame(data)\n\n# Step 2: Set up the plot\nplt.figure(figsize=(12, 8))\n\n# Step 3: Create scatter plots for each pair of metrics\n# Example: Loss vs Test Accuracy\nplt.subplot(2, 2, 1)\nsns.scatterplot(data=df, x='Test Accuracy', y='Loss', hue='Model', style='Model', s=100)\nplt.title('Test Accuracy vs Loss')\nplt.xlabel('Test Accuracy')\nplt.ylabel('Loss')\nplt.grid()\n\n# Example: Precision vs Recall\nplt.subplot(2, 2, 2)\nsns.scatterplot(data=df, x='Precision', y='Recall', hue='Model', style='Model', s=100)\nplt.title('Precision vs Recall')\nplt.xlabel('Precision')\nplt.ylabel('Recall')\nplt.grid()\n\n# Example: F1 Score vs Loss\nplt.subplot(2, 2, 3)\nsns.scatterplot(data=df, x='F1 Score', y='Loss', hue='Model', style='Model', s=100)\nplt.title('F1 Score vs Loss')\nplt.xlabel('F1 Score')\nplt.ylabel('Loss')\nplt.grid()\n\n# Example: Test Accuracy vs F1 Score\nplt.subplot(2, 2, 4)\nsns.scatterplot(data=df, x='Test Accuracy', y='F1 Score', hue='Model', style='Model', s=100)\nplt.title('Test Accuracy vs F1 Score')\nplt.xlabel('Test Accuracy')\nplt.ylabel('F1 Score')\nplt.grid()\n\n# Step 4: Adjust layout and show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-04T05:29:30.882092Z","iopub.execute_input":"2024-11-04T05:29:30.882958Z","iopub.status.idle":"2024-11-04T05:29:32.272846Z","shell.execute_reply.started":"2024-11-04T05:29:30.882924Z","shell.execute_reply":"2024-11-04T05:29:32.271984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nimport numpy as np\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error, r2_score\n\n# Step 1: Create a DataFrame with the provided model performance data\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],\n    'Precision': [0.676, 0.676, 0.710, 0.676],\n    'Recall': [0.822, 0.822, 0.704, 0.822],\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]\n}\n\ndf = pd.DataFrame(data)\n\n# Step 2: Define the features (X) and target (y)\nX = df[['Test Accuracy', 'Precision', 'Recall', 'F1 Score']]\ny = df['Loss']\n\n# Step 3: Create a linear regression model\nlinear_model = LinearRegression()\n\n# Step 4: Fit the model on the data\nlinear_model.fit(X, y)\n\n# Step 5: Make predictions\ny_pred = linear_model.predict(X)\n\n# Step 6: Evaluate the performance\nmse = mean_squared_error(y, y_pred)\nr2 = r2_score(y, y_pred)\n\n# Step 7: Display the results\nprint(f'Mean Squared Error: {mse:.3f}')\nprint(f'R² Score: {r2:.3f}')\n\n# Optional: Show coefficients\ncoefficients = pd.DataFrame(linear_model.coef_, X.columns, columns=['Coefficient'])\nprint(\"\\nCoefficients of the linear regression model:\")\nprint(coefficients)\n\n# Optional: Compare predictions with actual values\nresults = pd.DataFrame({'Actual Loss': y, 'Predicted Loss': y_pred})\nprint(\"\\nActual vs. Predicted Loss:\")\nprint(results)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-04T05:30:08.914400Z","iopub.execute_input":"2024-11-04T05:30:08.914774Z","iopub.status.idle":"2024-11-04T05:30:08.938736Z","shell.execute_reply.started":"2024-11-04T05:30:08.914745Z","shell.execute_reply":"2024-11-04T05:30:08.937689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nimport numpy as np\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import r2_score\n\n# Step 1: Create a DataFrame with the provided model performance data\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],\n    'Precision': [0.676, 0.676, 0.710, 0.676],\n    'Recall': [0.822, 0.822, 0.704, 0.822],\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]\n}\n\ndf = pd.DataFrame(data)\n\n# Step 2: Initialize an empty list to store R² scores for each model\nr2_scores = []\n\n# Step 3: Loop through each model to calculate R²\nfor index, row in df.iterrows():\n    # Define the features (X) and target (y) for the current model\n    X = df[['Test Accuracy', 'Precision', 'Recall', 'F1 Score']].drop(index)\n    y = df['Loss'].drop(index)\n    \n    # Create a linear regression model\n    linear_model = LinearRegression()\n    \n    # Fit the model on the data\n    linear_model.fit(X, y)\n    \n    # Make predictions\n    y_pred = linear_model.predict(X)\n    \n    # Calculate R² score\n    r2 = r2_score(y, y_pred)\n    \n    # Append the R² score to the list\n    r2_scores.append((row['Model'], r2))\n\n# Step 4: Display the R² scores for all models\nfor model, score in r2_scores:\n    print(f'R² Score for {model}: {score:.3f}')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-04T05:31:23.541475Z","iopub.execute_input":"2024-11-04T05:31:23.541836Z","iopub.status.idle":"2024-11-04T05:31:23.576405Z","shell.execute_reply.started":"2024-11-04T05:31:23.541805Z","shell.execute_reply":"2024-11-04T05:31:23.575415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nimport numpy as np\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import f1_score, precision_score, recall_score\n\n# Step 1: Create a DataFrame with the provided model performance data\ndata = {\n    'Model': ['VGG', 'VGG19', 'EfficientNetV2', 'ResNet152V2'],\n    'Loss': [0.471, 1.084, 1.825, 0.468],\n    'Test Accuracy': [0.822, 0.822, 0.704, 0.822],\n    'Precision': [0.676, 0.676, 0.710, 0.676],\n    'Recall': [0.822, 0.822, 0.704, 0.822],\n    'F1 Score': [0.742, 0.742, 0.707, 0.742]\n}\n\ndf = pd.DataFrame(data)\n\n# Step 2: Define a threshold for classification (e.g., Loss < 1.0 is low)\nthreshold = 1.0\n\n# Step 3: Initialize a list to store results\nresults = []\n\n# Step 4: Loop through each model to calculate F1 Score\nfor index, row in df.iterrows():\n    # Define the features (X) and target (y) for the current model\n    X = df[['Test Accuracy', 'Precision', 'Recall', 'F1 Score']].drop(index)\n    y = df['Loss'].drop(index)\n    \n    # Create and fit a linear regression model\n    linear_model = LinearRegression()\n    linear_model.fit(X, y)\n    \n    # Make predictions\n    y_pred = linear_model.predict(X)\n    \n    # Classify based on the predictions\n    y_actual_class = (y < threshold).astype(int)  # 1 if Loss < threshold, else 0\n    y_pred_class = (y_pred < threshold).astype(int)  # Predicted classification based on Loss\n    \n    # Calculate F1 Score, Precision, and Recall\n    f1 = f1_score(y_actual_class, y_pred_class)\n    precision = precision_score(y_actual_class, y_pred_class)\n    recall = recall_score(y_actual_class, y_pred_class)\n    \n    # Store the results\n    results.append({\n        'Model': row['Model'],\n        'F1 Score': f1,\n        'Precision': precision,\n        'Recall': recall\n    })\n\n# Step 5: Convert results to a DataFrame and display\nresults_df = pd.DataFrame(results)\nprint(results_df)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-04T05:33:40.316281Z","iopub.execute_input":"2024-11-04T05:33:40.316627Z","iopub.status.idle":"2024-11-04T05:33:40.374498Z","shell.execute_reply.started":"2024-11-04T05:33:40.316599Z","shell.execute_reply":"2024-11-04T05:33:40.373652Z"},"trusted":true},"execution_count":null,"outputs":[]}]}