{"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"}],"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-07-18T07:28:42.781044Z","iopub.execute_input":"2024-07-18T07:28:42.781500Z","iopub.status.idle":"2024-07-18T07:28:45.334499Z","shell.execute_reply.started":"2024-07-18T07:28:42.781448Z","shell.execute_reply":"2024-07-18T07:28:45.333255Z"},"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-07-18T07:28:45.336842Z","iopub.execute_input":"2024-07-18T07:28:45.337453Z","iopub.status.idle":"2024-07-18T07:29:02.222181Z","shell.execute_reply.started":"2024-07-18T07:28:45.337417Z","shell.execute_reply":"2024-07-18T07:29:02.220975Z"},"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-07-18T07:29:02.223427Z","iopub.execute_input":"2024-07-18T07:29:02.223745Z","iopub.status.idle":"2024-07-18T07:29:02.229216Z","shell.execute_reply.started":"2024-07-18T07:29:02.223719Z","shell.execute_reply":"2024-07-18T07:29:02.227932Z"},"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-07-18T07:29:02.231737Z","iopub.execute_input":"2024-07-18T07:29:02.232211Z","iopub.status.idle":"2024-07-18T07:29:02.243551Z","shell.execute_reply.started":"2024-07-18T07:29:02.232152Z","shell.execute_reply":"2024-07-18T07:29:02.242489Z"},"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-07-18T07:29:02.245266Z","iopub.execute_input":"2024-07-18T07:29:02.245703Z","iopub.status.idle":"2024-07-18T07:29:02.257257Z","shell.execute_reply.started":"2024-07-18T07:29:02.245667Z","shell.execute_reply":"2024-07-18T07:29:02.256113Z"},"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-07-18T07:29:02.258962Z","iopub.execute_input":"2024-07-18T07:29:02.259463Z","iopub.status.idle":"2024-07-18T07:29:04.437128Z","shell.execute_reply.started":"2024-07-18T07:29:02.259425Z","shell.execute_reply":"2024-07-18T07:29:04.435884Z"},"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-07-18T07:29:04.438613Z","iopub.execute_input":"2024-07-18T07:29:04.439029Z","iopub.status.idle":"2024-07-18T07:29:06.863250Z","shell.execute_reply.started":"2024-07-18T07:29:04.438994Z","shell.execute_reply":"2024-07-18T07:29:06.861549Z"},"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-07-18T07:29:06.864670Z","iopub.execute_input":"2024-07-18T07:29:06.865207Z","iopub.status.idle":"2024-07-18T07:29:09.203146Z","shell.execute_reply.started":"2024-07-18T07:29:06.865170Z","shell.execute_reply":"2024-07-18T07:29:09.201868Z"},"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-07-18T07:29:09.204714Z","iopub.execute_input":"2024-07-18T07:29:09.205180Z","iopub.status.idle":"2024-07-18T07:29:40.143620Z","shell.execute_reply.started":"2024-07-18T07:29:09.205130Z","shell.execute_reply":"2024-07-18T07:29:40.142191Z"},"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-07-18T07:29:40.149817Z","iopub.execute_input":"2024-07-18T07:29:40.150221Z","iopub.status.idle":"2024-07-18T07:29:40.156933Z","shell.execute_reply.started":"2024-07-18T07:29:40.150190Z","shell.execute_reply":"2024-07-18T07:29:40.155253Z"},"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-07-18T07:29:40.158704Z","iopub.execute_input":"2024-07-18T07:29:40.159102Z","iopub.status.idle":"2024-07-18T07:29:40.174315Z","shell.execute_reply.started":"2024-07-18T07:29:40.159070Z","shell.execute_reply":"2024-07-18T07:29:40.173084Z"},"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-07-18T07:29:40.176347Z","iopub.execute_input":"2024-07-18T07:29:40.176736Z","iopub.status.idle":"2024-07-18T07:29:40.196710Z","shell.execute_reply.started":"2024-07-18T07:29:40.176704Z","shell.execute_reply":"2024-07-18T07:29:40.195226Z"},"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-07-18T07:29:40.198381Z","iopub.execute_input":"2024-07-18T07:29:40.198831Z","iopub.status.idle":"2024-07-18T07:29:40.210158Z","shell.execute_reply.started":"2024-07-18T07:29:40.198772Z","shell.execute_reply":"2024-07-18T07:29:40.208841Z"},"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-07-18T07:29:40.211427Z","iopub.execute_input":"2024-07-18T07:29:40.211865Z","iopub.status.idle":"2024-07-18T07:29:42.086420Z","shell.execute_reply.started":"2024-07-18T07:29:40.211816Z","shell.execute_reply":"2024-07-18T07:29:42.085117Z"},"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-07-18T07:29:42.088519Z","iopub.execute_input":"2024-07-18T07:29:42.089039Z","iopub.status.idle":"2024-07-18T07:31:42.554608Z","shell.execute_reply.started":"2024-07-18T07:29:42.088995Z","shell.execute_reply":"2024-07-18T07:31:42.553567Z"},"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-07-18T07:31:42.560071Z","iopub.execute_input":"2024-07-18T07:31:42.563683Z","iopub.status.idle":"2024-07-18T07:31:42.871411Z","shell.execute_reply.started":"2024-07-18T07:31:42.563641Z","shell.execute_reply":"2024-07-18T07:31:42.870012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:31:42.872727Z","iopub.execute_input":"2024-07-18T07:31:42.873121Z","iopub.status.idle":"2024-07-18T07:31:42.881870Z","shell.execute_reply.started":"2024-07-18T07:31:42.873090Z","shell.execute_reply":"2024-07-18T07:31:42.880470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:31:42.883394Z","iopub.execute_input":"2024-07-18T07:31:42.883861Z","iopub.status.idle":"2024-07-18T07:31:42.897546Z","shell.execute_reply.started":"2024-07-18T07:31:42.883820Z","shell.execute_reply":"2024-07-18T07:31:42.896147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:31:42.899170Z","iopub.execute_input":"2024-07-18T07:31:42.899548Z","iopub.status.idle":"2024-07-18T07:31:42.912998Z","shell.execute_reply.started":"2024-07-18T07:31:42.899510Z","shell.execute_reply":"2024-07-18T07:31:42.911694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:31:42.915214Z","iopub.execute_input":"2024-07-18T07:31:42.915633Z","iopub.status.idle":"2024-07-18T07:31:42.928640Z","shell.execute_reply.started":"2024-07-18T07:31:42.915579Z","shell.execute_reply":"2024-07-18T07:31:42.927365Z"},"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-07-18T07:31:42.930155Z","iopub.execute_input":"2024-07-18T07:31:42.930489Z","iopub.status.idle":"2024-07-18T07:31:42.943702Z","shell.execute_reply.started":"2024-07-18T07:31:42.930462Z","shell.execute_reply":"2024-07-18T07:31:42.942185Z"},"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-07-18T07:31:42.945455Z","iopub.execute_input":"2024-07-18T07:31:42.946132Z","iopub.status.idle":"2024-07-18T07:31:45.298222Z","shell.execute_reply.started":"2024-07-18T07:31:42.946008Z","shell.execute_reply":"2024-07-18T07:31:45.296995Z"},"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-07-18T07:31:45.299681Z","iopub.execute_input":"2024-07-18T07:31:45.300153Z","iopub.status.idle":"2024-07-18T07:49:43.854925Z","shell.execute_reply.started":"2024-07-18T07:31:45.300094Z","shell.execute_reply":"2024-07-18T07:49:43.853704Z"},"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-07-18T07:49:43.856687Z","iopub.execute_input":"2024-07-18T07:49:43.857149Z","iopub.status.idle":"2024-07-18T07:51:06.175606Z","shell.execute_reply.started":"2024-07-18T07:49:43.857116Z","shell.execute_reply":"2024-07-18T07:51:06.173927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:51:06.185526Z","iopub.execute_input":"2024-07-18T07:51:06.192162Z","iopub.status.idle":"2024-07-18T07:51:06.213123Z","shell.execute_reply.started":"2024-07-18T07:51:06.192066Z","shell.execute_reply":"2024-07-18T07:51:06.207297Z"},"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-07-18T07:51:06.215637Z","iopub.execute_input":"2024-07-18T07:51:06.216713Z","iopub.status.idle":"2024-07-18T07:51:06.255453Z","shell.execute_reply.started":"2024-07-18T07:51:06.216650Z","shell.execute_reply":"2024-07-18T07:51:06.253665Z"},"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-07-18T07:51:06.257761Z","iopub.execute_input":"2024-07-18T07:51:06.259111Z","iopub.status.idle":"2024-07-18T07:51:06.266967Z","shell.execute_reply.started":"2024-07-18T07:51:06.259053Z","shell.execute_reply":"2024-07-18T07:51:06.265297Z"},"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-07-18T07:51:06.284066Z","iopub.execute_input":"2024-07-18T07:51:06.285839Z","iopub.status.idle":"2024-07-18T07:51:06.293360Z","shell.execute_reply.started":"2024-07-18T07:51:06.285744Z","shell.execute_reply":"2024-07-18T07:51:06.291842Z"},"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-07-18T07:51:06.295379Z","iopub.execute_input":"2024-07-18T07:51:06.296138Z","iopub.status.idle":"2024-07-18T07:51:06.993004Z","shell.execute_reply.started":"2024-07-18T07:51:06.296058Z","shell.execute_reply":"2024-07-18T07:51:06.991713Z"},"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-07-18T07:51:06.994975Z","iopub.execute_input":"2024-07-18T07:51:06.995467Z","iopub.status.idle":"2024-07-18T07:51:08.027777Z","shell.execute_reply.started":"2024-07-18T07:51:06.995421Z","shell.execute_reply":"2024-07-18T07:51:08.026554Z"},"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-07-18T07:51:08.029360Z","iopub.execute_input":"2024-07-18T07:51:08.029740Z","iopub.status.idle":"2024-07-18T07:51:08.120005Z","shell.execute_reply.started":"2024-07-18T07:51:08.029709Z","shell.execute_reply":"2024-07-18T07:51:08.118647Z"},"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-07-18T07:51:08.121614Z","iopub.execute_input":"2024-07-18T07:51:08.122023Z","iopub.status.idle":"2024-07-18T07:51:08.129321Z","shell.execute_reply.started":"2024-07-18T07:51:08.121988Z","shell.execute_reply":"2024-07-18T07:51:08.128081Z"},"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-07-18T07:51:08.131081Z","iopub.execute_input":"2024-07-18T07:51:08.131480Z","iopub.status.idle":"2024-07-18T07:51:08.144855Z","shell.execute_reply.started":"2024-07-18T07:51:08.131450Z","shell.execute_reply":"2024-07-18T07:51:08.143490Z"},"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-07-18T07:51:08.146462Z","iopub.execute_input":"2024-07-18T07:51:08.146983Z","iopub.status.idle":"2024-07-18T07:51:08.573361Z","shell.execute_reply.started":"2024-07-18T07:51:08.146940Z","shell.execute_reply":"2024-07-18T07:51:08.572395Z"},"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-07-18T07:51:08.574820Z","iopub.execute_input":"2024-07-18T07:51:08.575481Z","iopub.status.idle":"2024-07-18T07:51:09.364719Z","shell.execute_reply.started":"2024-07-18T07:51:08.575450Z","shell.execute_reply":"2024-07-18T07:51:09.363170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-18T07:51:09.367290Z","iopub.execute_input":"2024-07-18T07:51:09.368427Z","iopub.status.idle":"2024-07-18T07:51:09.378715Z","shell.execute_reply.started":"2024-07-18T07:51:09.368372Z","shell.execute_reply":"2024-07-18T07:51:09.377253Z"},"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-07-18T07:51:09.381771Z","iopub.execute_input":"2024-07-18T07:51:09.383320Z","iopub.status.idle":"2024-07-18T07:51:09.405227Z","shell.execute_reply.started":"2024-07-18T07:51:09.383256Z","shell.execute_reply":"2024-07-18T07:51:09.403543Z"},"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-07-18T07:51:09.408475Z","iopub.execute_input":"2024-07-18T07:51:09.409706Z","iopub.status.idle":"2024-07-18T07:51:09.533081Z","shell.execute_reply.started":"2024-07-18T07:51:09.409642Z","shell.execute_reply":"2024-07-18T07:51:09.531879Z"},"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-07-18T07:51:09.534874Z","iopub.execute_input":"2024-07-18T07:51:09.535250Z","iopub.status.idle":"2024-07-18T07:51:09.615770Z","shell.execute_reply.started":"2024-07-18T07:51:09.535218Z","shell.execute_reply":"2024-07-18T07:51:09.614597Z"},"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-07-18T07:51:09.617266Z","iopub.execute_input":"2024-07-18T07:51:09.617633Z","iopub.status.idle":"2024-07-18T07:51:09.632223Z","shell.execute_reply.started":"2024-07-18T07:51:09.617600Z","shell.execute_reply":"2024-07-18T07:51:09.630925Z"},"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-07-18T07:51:09.633621Z","iopub.execute_input":"2024-07-18T07:51:09.634395Z","iopub.status.idle":"2024-07-18T07:51:26.031011Z","shell.execute_reply.started":"2024-07-18T07:51:09.634363Z","shell.execute_reply":"2024-07-18T07:51:26.029867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thank You!","metadata":{}}]}