{"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":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":" # Importing Required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport zipfile\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.manifold import TSNE\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport plotly.express as px\nimport random\nfrom PIL import Image\nimport plotly.graph_objects as go\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-06T00:08:56.085311Z","iopub.execute_input":"2024-09-06T00:08:56.085719Z","iopub.status.idle":"2024-09-06T00:08:59.346725Z","shell.execute_reply.started":"2024-09-06T00:08:56.085685Z","shell.execute_reply":"2024-09-06T00:08:59.345557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting the datasets","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train')\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:15.099184Z","iopub.execute_input":"2024-09-06T00:09:15.100136Z","iopub.status.idle":"2024-09-06T00:09:30.823059Z","shell.execute_reply.started":"2024-09-06T00:09:15.100093Z","shell.execute_reply":"2024-09-06T00:09:30.822089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setting up paths","metadata":{}},{"cell_type":"code","source":"train_dir = '/kaggle/working/train/train'\ntest_dir = '/kaggle/working/test/test1'","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:35.385686Z","iopub.execute_input":"2024-09-06T00:09:35.386607Z","iopub.status.idle":"2024-09-06T00:09:35.390820Z","shell.execute_reply.started":"2024-09-06T00:09:35.386570Z","shell.execute_reply":"2024-09-06T00:09:35.389678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to load and preprocess the images","metadata":{}},{"cell_type":"code","source":"def load_images(folder, img_size=(64, 64), sample_count=20, label_filter=None):\n    images = []\n    labels = []\n    filenames = os.listdir(folder)\n    \n    if label_filter is not None:\n        filenames = [fname for fname in filenames if label_filter in fname]\n    \n    random.shuffle(filenames)\n    for img in filenames[:sample_count]:\n        try:\n            img_path = os.path.join(folder, img)\n            img_pil = Image.open(img_path)\n            img_pil = img_pil.resize(img_size)\n            img_np = np.array(img_pil) / 255.0  # Normalize pixel values\n            images.append(img_np)\n            labels.append(0 if 'cat' in img else 1)\n        except Exception as e:\n            print(f\"Error loading image {img}: {e}\")\n    \n    return np.array(images), np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:39.492176Z","iopub.execute_input":"2024-09-06T00:09:39.492605Z","iopub.status.idle":"2024-09-06T00:09:39.502582Z","shell.execute_reply.started":"2024-09-06T00:09:39.492571Z","shell.execute_reply":"2024-09-06T00:09:39.501358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize images with labels","metadata":{}},{"cell_type":"code","source":"def visualize_images(images, labels, sample_count=20):\n    fig, axes = plt.subplots(4, 5, figsize=(15, 12))\n    axes = axes.flatten()\n    \n    for i in range(sample_count):\n        axes[i].imshow(images[i])\n        axes[i].set_title('Cat' if labels[i] == 0 else 'Dog')\n        axes[i].axis('off')\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:42.004966Z","iopub.execute_input":"2024-09-06T00:09:42.005385Z","iopub.status.idle":"2024-09-06T00:09:42.014139Z","shell.execute_reply.started":"2024-09-06T00:09:42.005352Z","shell.execute_reply":"2024-09-06T00:09:42.012519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and visualize cat images","metadata":{}},{"cell_type":"code","source":"cat_images, cat_labels = load_images(train_dir, sample_count=20, label_filter='cat')\nvisualize_images(cat_images, cat_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:44.262070Z","iopub.execute_input":"2024-09-06T00:09:44.262569Z","iopub.status.idle":"2024-09-06T00:09:46.576098Z","shell.execute_reply.started":"2024-09-06T00:09:44.262531Z","shell.execute_reply":"2024-09-06T00:09:46.574892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and visualize dog images","metadata":{}},{"cell_type":"code","source":"dog_images, dog_labels = load_images(train_dir, sample_count=20, label_filter='dog')\nvisualize_images(dog_images, dog_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:49.040376Z","iopub.execute_input":"2024-09-06T00:09:49.040803Z","iopub.status.idle":"2024-09-06T00:09:51.167059Z","shell.execute_reply.started":"2024-09-06T00:09:49.040764Z","shell.execute_reply":"2024-09-06T00:09:51.165843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and visualize both cat and dog images","metadata":{}},{"cell_type":"code","source":"all_images, all_labels = load_images(train_dir, sample_count=20)\nvisualize_images(all_images, all_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:53.685009Z","iopub.execute_input":"2024-09-06T00:09:53.685761Z","iopub.status.idle":"2024-09-06T00:09:56.004176Z","shell.execute_reply.started":"2024-09-06T00:09:53.685721Z","shell.execute_reply":"2024-09-06T00:09:56.003099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading and preprocessing the training data","metadata":{}},{"cell_type":"code","source":"images, labels = load_images(train_dir, sample_count=10000) \nimages = images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:09:58.582679Z","iopub.execute_input":"2024-09-06T00:09:58.583491Z","iopub.status.idle":"2024-09-06T00:10:28.115096Z","shell.execute_reply.started":"2024-09-06T00:09:58.583454Z","shell.execute_reply":"2024-09-06T00:10:28.114154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display the shape of the images array","metadata":{}},{"cell_type":"code","source":"print(\"Shape of images array:\", images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:34.038589Z","iopub.execute_input":"2024-09-06T00:10:34.039440Z","iopub.status.idle":"2024-09-06T00:10:34.044675Z","shell.execute_reply.started":"2024-09-06T00:10:34.039398Z","shell.execute_reply":"2024-09-06T00:10:34.043683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display the shape of the first image","metadata":{}},{"cell_type":"code","source":"print(\"Shape of the first image:\", images[0].shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:35.804979Z","iopub.execute_input":"2024-09-06T00:10:35.805421Z","iopub.status.idle":"2024-09-06T00:10:35.811546Z","shell.execute_reply.started":"2024-09-06T00:10:35.805386Z","shell.execute_reply":"2024-09-06T00:10:35.810280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # Display the first 2 images","metadata":{}},{"cell_type":"code","source":"print(images[:2]) \nprint(labels[:2])","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:37.418077Z","iopub.execute_input":"2024-09-06T00:10:37.418488Z","iopub.status.idle":"2024-09-06T00:10:37.429110Z","shell.execute_reply.started":"2024-09-06T00:10:37.418453Z","shell.execute_reply":"2024-09-06T00:10:37.427938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Flattening the images for the SVM input","metadata":{}},{"cell_type":"code","source":"n_samples, img_height, img_width, img_channels = images.shape\nimages_flat = images.reshape(n_samples, -1)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:38.880760Z","iopub.execute_input":"2024-09-06T00:10:38.881180Z","iopub.status.idle":"2024-09-06T00:10:38.886832Z","shell.execute_reply.started":"2024-09-06T00:10:38.881150Z","shell.execute_reply":"2024-09-06T00:10:38.885450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standardizing the features","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nimages_flat_scaled = scaler.fit_transform(images_flat)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:42.164544Z","iopub.execute_input":"2024-09-06T00:10:42.164991Z","iopub.status.idle":"2024-09-06T00:10:43.988639Z","shell.execute_reply.started":"2024-09-06T00:10:42.164956Z","shell.execute_reply":"2024-09-06T00:10:43.987681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying t-SNE ","metadata":{}},{"cell_type":"code","source":"perplexity = min(30, n_samples - 1)\ntsne = TSNE(n_components=2, perplexity=perplexity, random_state=42)\nimages_tsne = tsne.fit_transform(images_flat_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:10:48.425125Z","iopub.execute_input":"2024-09-06T00:10:48.425631Z","iopub.status.idle":"2024-09-06T00:12:43.794042Z","shell.execute_reply.started":"2024-09-06T00:10:48.425578Z","shell.execute_reply":"2024-09-06T00:12:43.793008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting the dataset into training and test sets","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(images_flat_scaled, labels, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:18.218717Z","iopub.execute_input":"2024-09-06T00:13:18.219712Z","iopub.status.idle":"2024-09-06T00:13:18.547026Z","shell.execute_reply.started":"2024-09-06T00:13:18.219675Z","shell.execute_reply":"2024-09-06T00:13:18.546100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:20.067815Z","iopub.execute_input":"2024-09-06T00:13:20.068236Z","iopub.status.idle":"2024-09-06T00:13:20.075890Z","shell.execute_reply.started":"2024-09-06T00:13:20.068204Z","shell.execute_reply":"2024-09-06T00:13:20.074821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:20.741707Z","iopub.execute_input":"2024-09-06T00:13:20.742367Z","iopub.status.idle":"2024-09-06T00:13:20.749575Z","shell.execute_reply.started":"2024-09-06T00:13:20.742332Z","shell.execute_reply":"2024-09-06T00:13:20.748430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:25.779044Z","iopub.execute_input":"2024-09-06T00:13:25.779720Z","iopub.status.idle":"2024-09-06T00:13:25.786763Z","shell.execute_reply.started":"2024-09-06T00:13:25.779681Z","shell.execute_reply":"2024-09-06T00:13:25.785668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:27.629960Z","iopub.execute_input":"2024-09-06T00:13:27.630950Z","iopub.status.idle":"2024-09-06T00:13:27.637036Z","shell.execute_reply.started":"2024-09-06T00:13:27.630916Z","shell.execute_reply":"2024-09-06T00:13:27.636129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking class distribution","metadata":{}},{"cell_type":"code","source":"unique, counts = np.unique(labels, return_counts=True)\nprint(f\"Class distribution: {dict(zip(unique, counts))}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:29.720653Z","iopub.execute_input":"2024-09-06T00:13:29.721385Z","iopub.status.idle":"2024-09-06T00:13:29.727821Z","shell.execute_reply.started":"2024-09-06T00:13:29.721349Z","shell.execute_reply":"2024-09-06T00:13:29.726787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_tsne_data(tsne_data, labels, title):\n    df_tsne = pd.DataFrame()\n    df_tsne['X'] = tsne_data[:, 0]\n    df_tsne['Y'] = tsne_data[:, 1]\n    df_tsne['Label'] = labels\n    df_tsne['Label'] = df_tsne['Label'].map({0: 'Cat', 1: 'Dog'})\n    \n    fig = px.scatter(df_tsne, x='X', y='Y', color='Label', title=title)\n    fig.show()\n\nplot_tsne_data(images_tsne, labels, 't-SNE visualization of Cats vs Dogs')","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:31.079744Z","iopub.execute_input":"2024-09-06T00:13:31.080585Z","iopub.status.idle":"2024-09-06T00:13:32.963841Z","shell.execute_reply.started":"2024-09-06T00:13:31.080552Z","shell.execute_reply":"2024-09-06T00:13:32.962883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the SVM model","metadata":{}},{"cell_type":"code","source":"svm = SVC(kernel='linear')\nsvm.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:13:36.800179Z","iopub.execute_input":"2024-09-06T00:13:36.800587Z","iopub.status.idle":"2024-09-06T00:30:28.335997Z","shell.execute_reply.started":"2024-09-06T00:13:36.800554Z","shell.execute_reply":"2024-09-06T00:30:28.334852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making predictions on the validation set","metadata":{}},{"cell_type":"code","source":"y_val_pred = svm.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:34:45.024583Z","iopub.execute_input":"2024-09-06T00:34:45.025000Z","iopub.status.idle":"2024-09-06T00:36:01.194476Z","shell.execute_reply.started":"2024-09-06T00:34:45.024970Z","shell.execute_reply":"2024-09-06T00:36:01.192914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:04.638513Z","iopub.execute_input":"2024-09-06T00:36:04.638935Z","iopub.status.idle":"2024-09-06T00:36:04.646086Z","shell.execute_reply.started":"2024-09-06T00:36:04.638902Z","shell.execute_reply":"2024-09-06T00:36:04.645019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluating the model","metadata":{}},{"cell_type":"code","source":"val_accuracy = accuracy_score(y_val, y_val_pred)\nval_class_report = classification_report(y_val, y_val_pred, target_names=['Cat', 'Dog'])\nval_conf_matrix = confusion_matrix(y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:07.672565Z","iopub.execute_input":"2024-09-06T00:36:07.673008Z","iopub.status.idle":"2024-09-06T00:36:07.695518Z","shell.execute_reply.started":"2024-09-06T00:36:07.672974Z","shell.execute_reply":"2024-09-06T00:36:07.694559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy","metadata":{}},{"cell_type":"code","source":"print(f'Validation Accuracy: {val_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:10.070745Z","iopub.execute_input":"2024-09-06T00:36:10.071149Z","iopub.status.idle":"2024-09-06T00:36:10.076975Z","shell.execute_reply.started":"2024-09-06T00:36:10.071116Z","shell.execute_reply":"2024-09-06T00:36:10.075855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classification Report","metadata":{}},{"cell_type":"code","source":"print('Validation Classification Report:')\nprint(val_class_report)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:14.412650Z","iopub.execute_input":"2024-09-06T00:36:14.413070Z","iopub.status.idle":"2024-09-06T00:36:14.418461Z","shell.execute_reply.started":"2024-09-06T00:36:14.413017Z","shell.execute_reply":"2024-09-06T00:36:14.417422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nprint('Validation Confusion Matrix:')\nprint(val_conf_matrix)\nplt.figure(figsize=(10, 7))\nsns.heatmap(val_conf_matrix, annot=True, fmt='d', cmap='Blues')\nplt.ylabel('Actual')\nplt.xlabel('Predicted')\nplt.title('Validation Confusion Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:16.849310Z","iopub.execute_input":"2024-09-06T00:36:16.849724Z","iopub.status.idle":"2024-09-06T00:36:17.445405Z","shell.execute_reply.started":"2024-09-06T00:36:16.849692Z","shell.execute_reply":"2024-09-06T00:36:17.444354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the SVM model and scaler","metadata":{}},{"cell_type":"code","source":"import pickle\nwith open('/kaggle/working/svm_model.pkl', 'wb') as f:\n    pickle.dump(svm, f)\nwith open('/kaggle/working/scaler.pkl', 'wb') as f:\n    pickle.dump(scaler, f)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:19.673210Z","iopub.execute_input":"2024-09-06T00:36:19.673648Z","iopub.status.idle":"2024-09-06T00:36:20.652332Z","shell.execute_reply.started":"2024-09-06T00:36:19.673602Z","shell.execute_reply":"2024-09-06T00:36:20.651154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading and preprocessing the test data","metadata":{}},{"cell_type":"code","source":"test_images, test_labels = load_images(test_dir)  \ntest_images = test_images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:26.062429Z","iopub.execute_input":"2024-09-06T00:36:26.063156Z","iopub.status.idle":"2024-09-06T00:36:26.143557Z","shell.execute_reply.started":"2024-09-06T00:36:26.063117Z","shell.execute_reply":"2024-09-06T00:36:26.142162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Flattening the test images for the SVM input","metadata":{}},{"cell_type":"code","source":"n_test_samples, test_img_height, test_img_width, test_img_channels = test_images.shape\ntest_images_flat = test_images.reshape(n_test_samples, -1)\ntest_images_flat_scaled = scaler.transform(test_images_flat)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:29.035695Z","iopub.execute_input":"2024-09-06T00:36:29.036422Z","iopub.status.idle":"2024-09-06T00:36:29.043357Z","shell.execute_reply.started":"2024-09-06T00:36:29.036373Z","shell.execute_reply":"2024-09-06T00:36:29.041963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standardizing the test features","metadata":{}},{"cell_type":"code","source":"test_images_flat_scaled = scaler.transform(test_images_flat)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:31.066537Z","iopub.execute_input":"2024-09-06T00:36:31.067367Z","iopub.status.idle":"2024-09-06T00:36:31.074863Z","shell.execute_reply.started":"2024-09-06T00:36:31.067328Z","shell.execute_reply":"2024-09-06T00:36:31.073646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying t-SNE for visualization on test data","metadata":{}},{"cell_type":"code","source":"# Determine a new perplexity value for the test data\ntest_perplexity = min(30, n_test_samples - 1)\n\n# Create a new t-SNE instance for the test data\ntest_tsne = TSNE(n_components=2, perplexity=test_perplexity, random_state=42)\ntest_images_tsne = test_tsne.fit_transform(test_images_flat_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:33.364864Z","iopub.execute_input":"2024-09-06T00:36:33.365290Z","iopub.status.idle":"2024-09-06T00:36:33.772326Z","shell.execute_reply.started":"2024-09-06T00:36:33.365253Z","shell.execute_reply":"2024-09-06T00:36:33.771367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making predictions on the test set","metadata":{}},{"cell_type":"code","source":"y_test_pred = svm.predict(test_images_flat_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:36.812281Z","iopub.execute_input":"2024-09-06T00:36:36.812695Z","iopub.status.idle":"2024-09-06T00:36:37.611545Z","shell.execute_reply.started":"2024-09-06T00:36:36.812665Z","shell.execute_reply":"2024-09-06T00:36:37.610057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:41.397714Z","iopub.execute_input":"2024-09-06T00:36:41.398360Z","iopub.status.idle":"2024-09-06T00:36:41.404718Z","shell.execute_reply.started":"2024-09-06T00:36:41.398326Z","shell.execute_reply":"2024-09-06T00:36:41.403665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a DataFrame for Plotly","metadata":{}},{"cell_type":"code","source":"df_test_tsne = pd.DataFrame()\ndf_test_tsne['X'] = test_images_tsne[:, 0]\ndf_test_tsne['Y'] = test_images_tsne[:, 1]\ndf_test_tsne['Predicted Label'] = y_test_pred\ndf_test_tsne['Predicted Label'] = df_test_tsne['Predicted Label'].map({0: 'Cat', 1: 'Dog'})\n","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:45.484508Z","iopub.execute_input":"2024-09-06T00:36:45.484941Z","iopub.status.idle":"2024-09-06T00:36:45.495698Z","shell.execute_reply.started":"2024-09-06T00:36:45.484908Z","shell.execute_reply":"2024-09-06T00:36:45.494223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting with Plotly","metadata":{}},{"cell_type":"code","source":"fig = px.scatter(df_test_tsne, x='X', y='Y', color='Predicted Label', title='t-SNE visualization of Test Data Predictions')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:47.924729Z","iopub.execute_input":"2024-09-06T00:36:47.925149Z","iopub.status.idle":"2024-09-06T00:36:48.018361Z","shell.execute_reply.started":"2024-09-06T00:36:47.925117Z","shell.execute_reply":"2024-09-06T00:36:48.017315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing some of the test results with Plotly","metadata":{}},{"cell_type":"code","source":"def plot_test_samples(images, tsne_data, predictions, sample_count=10):\n    sample_indices = np.random.choice(range(len(images)), sample_count, replace=False)\n    sample_images = images[sample_indices]\n    tsne_samples = tsne_data[sample_indices]\n    pred_samples = predictions[sample_indices]\n\n    fig = px.scatter(tsne_samples, x=tsne_samples[:, 0], y=tsne_samples[:, 1], color=pred_samples.astype(str),\n                     title=\"Test Data Predictions (t-SNE Space)\")\n    fig.show()\n\nplot_test_samples(test_images, test_images_tsne, y_test_pred)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:36:55.997488Z","iopub.execute_input":"2024-09-06T00:36:55.998348Z","iopub.status.idle":"2024-09-06T00:36:56.078335Z","shell.execute_reply.started":"2024-09-06T00:36:55.998315Z","shell.execute_reply":"2024-09-06T00:36:56.077285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the test predictions to a CSV file","metadata":{}},{"cell_type":"code","source":"test_predictions = pd.DataFrame({'Id': np.arange(1, n_test_samples + 1), 'Label': y_test_pred})\ntest_predictions['Label'] = test_predictions['Label'].map({0: 'Cat', 1: 'Dog'})\ntest_predictions.to_csv('/kaggle/working/test_predictions.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:37:02.808987Z","iopub.execute_input":"2024-09-06T00:37:02.809809Z","iopub.status.idle":"2024-09-06T00:37:02.822632Z","shell.execute_reply.started":"2024-09-06T00:37:02.809774Z","shell.execute_reply":"2024-09-06T00:37:02.821663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_samples(tsne_data, y_true, y_pred, sample_count=10):\n    sample_indices = np.random.choice(range(len(tsne_data)), sample_count, replace=False)\n    tsne_samples = tsne_data[sample_indices]\n    y_true_samples = y_true[sample_indices]\n    y_pred_samples = y_pred[sample_indices]\n\n    df_tsne_samples = pd.DataFrame()\n    df_tsne_samples['X'] = tsne_samples[:, 0]\n    df_tsne_samples['Y'] = tsne_samples[:, 1]\n    df_tsne_samples['True Label'] = y_true_samples\n    df_tsne_samples['Predicted Label'] = y_pred_samples\n    df_tsne_samples['True Label'] = df_tsne_samples['True Label'].map({0: 'Cat', 1: 'Dog'})\n    df_tsne_samples['Predicted Label'] = df_tsne_samples['Predicted Label'].map({0: 'Cat', 1: 'Dog'})\n\n    fig = px.scatter(df_tsne_samples, x='X', y='Y', color='True Label', symbol='Predicted Label', title=\"True vs Predicted Labels (t-SNE Space)\")\n    fig.show()\n\n# Apply t-SNE to the validation data\nval_tsne = TSNE(n_components=2, perplexity=perplexity, random_state=42)\nval_images_tsne = val_tsne.fit_transform(X_val)\n\n# Plot true vs predicted labels for the validation set\nplot_samples(val_images_tsne, y_val, y_val_pred)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-06T00:37:05.011747Z","iopub.execute_input":"2024-09-06T00:37:05.012402Z","iopub.status.idle":"2024-09-06T00:37:19.895604Z","shell.execute_reply.started":"2024-09-06T00:37:05.012365Z","shell.execute_reply":"2024-09-06T00:37:19.894474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thank You!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}