{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-04T16:57:12.693830Z","iopub.execute_input":"2024-08-04T16:57:12.695198Z","iopub.status.idle":"2024-08-04T16:57:12.707632Z","shell.execute_reply.started":"2024-08-04T16:57:12.695149Z","shell.execute_reply":"2024-08-04T16:57:12.705895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-08-04T16:57:12.710857Z","iopub.execute_input":"2024-08-04T16:57:12.711451Z","iopub.status.idle":"2024-08-04T16:57:12.727127Z","shell.execute_reply.started":"2024-08-04T16:57:12.711399Z","shell.execute_reply":"2024-08-04T16:57:12.725709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:12.728832Z","iopub.execute_input":"2024-08-04T16:57:12.729215Z","iopub.status.idle":"2024-08-04T16:57:29.343653Z","shell.execute_reply.started":"2024-08-04T16:57:12.729169Z","shell.execute_reply":"2024-08-04T16:57:29.342303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train/train'\ntest_dir = '/kaggle/working/test/test1'","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:57:29.347053Z","iopub.execute_input":"2024-08-04T16:57:29.347637Z","iopub.status.idle":"2024-08-04T16:57:29.354268Z","shell.execute_reply.started":"2024-08-04T16:57:29.347563Z","shell.execute_reply":"2024-08-04T16:57:29.352780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:29.356010Z","iopub.execute_input":"2024-08-04T16:57:29.356489Z","iopub.status.idle":"2024-08-04T16:57:29.371913Z","shell.execute_reply.started":"2024-08-04T16:57:29.356445Z","shell.execute_reply":"2024-08-04T16:57:29.370493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:29.373800Z","iopub.execute_input":"2024-08-04T16:57:29.374207Z","iopub.status.idle":"2024-08-04T16:57:29.392948Z","shell.execute_reply.started":"2024-08-04T16:57:29.374174Z","shell.execute_reply":"2024-08-04T16:57:29.391488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:29.394275Z","iopub.execute_input":"2024-08-04T16:57:29.394655Z","iopub.status.idle":"2024-08-04T16:57:31.799000Z","shell.execute_reply.started":"2024-08-04T16:57:29.394586Z","shell.execute_reply":"2024-08-04T16:57:31.797466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:31.801110Z","iopub.execute_input":"2024-08-04T16:57:31.801675Z","iopub.status.idle":"2024-08-04T16:57:33.996009Z","shell.execute_reply.started":"2024-08-04T16:57:31.801632Z","shell.execute_reply":"2024-08-04T16:57:33.994737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:57:33.997659Z","iopub.execute_input":"2024-08-04T16:57:33.998058Z","iopub.status.idle":"2024-08-04T16:57:36.370883Z","shell.execute_reply.started":"2024-08-04T16:57:33.998024Z","shell.execute_reply":"2024-08-04T16:57:36.369692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = load_images(train_dir, sample_count=10000) \nimages = images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:57:36.375302Z","iopub.execute_input":"2024-08-04T16:57:36.375759Z","iopub.status.idle":"2024-08-04T16:58:08.905905Z","shell.execute_reply.started":"2024-08-04T16:57:36.375726Z","shell.execute_reply":"2024-08-04T16:58:08.904393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of images array:\", images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:58:08.907494Z","iopub.execute_input":"2024-08-04T16:58:08.907926Z","iopub.status.idle":"2024-08-04T16:58:08.915256Z","shell.execute_reply.started":"2024-08-04T16:58:08.907892Z","shell.execute_reply":"2024-08-04T16:58:08.913683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of the first image:\", images[0].shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:58:08.917178Z","iopub.execute_input":"2024-08-04T16:58:08.917945Z","iopub.status.idle":"2024-08-04T16:58:08.931525Z","shell.execute_reply.started":"2024-08-04T16:58:08.917901Z","shell.execute_reply":"2024-08-04T16:58:08.930246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images[:2]) \nprint(labels[:2])","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:58:08.933052Z","iopub.execute_input":"2024-08-04T16:58:08.933449Z","iopub.status.idle":"2024-08-04T16:58:08.950856Z","shell.execute_reply.started":"2024-08-04T16:58:08.933396Z","shell.execute_reply":"2024-08-04T16:58:08.949040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:58:08.952422Z","iopub.execute_input":"2024-08-04T16:58:08.952825Z","iopub.status.idle":"2024-08-04T16:58:08.971300Z","shell.execute_reply.started":"2024-08-04T16:58:08.952793Z","shell.execute_reply":"2024-08-04T16:58:08.969745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nimages_flat_scaled = scaler.fit_transform(images_flat)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T16:58:08.974564Z","iopub.execute_input":"2024-08-04T16:58:08.975010Z","iopub.status.idle":"2024-08-04T16:58:11.278965Z","shell.execute_reply.started":"2024-08-04T16:58:08.974978Z","shell.execute_reply":"2024-08-04T16:58:11.277585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T16:58:11.280630Z","iopub.execute_input":"2024-08-04T16:58:11.281235Z","iopub.status.idle":"2024-08-04T17:00:13.739884Z","shell.execute_reply.started":"2024-08-04T16:58:11.281191Z","shell.execute_reply":"2024-08-04T17:00:13.738805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:00:13.741358Z","iopub.execute_input":"2024-08-04T17:00:13.745741Z","iopub.status.idle":"2024-08-04T17:00:14.059486Z","shell.execute_reply.started":"2024-08-04T17:00:13.745697Z","shell.execute_reply":"2024-08-04T17:00:14.058505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:00:14.060847Z","iopub.execute_input":"2024-08-04T17:00:14.061184Z","iopub.status.idle":"2024-08-04T17:00:14.069151Z","shell.execute_reply.started":"2024-08-04T17:00:14.061157Z","shell.execute_reply":"2024-08-04T17:00:14.067885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:00:14.070725Z","iopub.execute_input":"2024-08-04T17:00:14.071126Z","iopub.status.idle":"2024-08-04T17:00:14.085677Z","shell.execute_reply.started":"2024-08-04T17:00:14.071094Z","shell.execute_reply":"2024-08-04T17:00:14.084387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:00:14.087325Z","iopub.execute_input":"2024-08-04T17:00:14.087826Z","iopub.status.idle":"2024-08-04T17:00:14.101215Z","shell.execute_reply.started":"2024-08-04T17:00:14.087784Z","shell.execute_reply":"2024-08-04T17:00:14.100033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unique, counts = np.unique(labels, return_counts=True)\n#print(f\"Class distribution: {dict(zip(unique, counts))}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:00:14.103100Z","iopub.execute_input":"2024-08-04T17:00:14.103624Z","iopub.status.idle":"2024-08-04T17:00:14.112465Z","shell.execute_reply.started":"2024-08-04T17:00:14.103555Z","shell.execute_reply":"2024-08-04T17:00:14.111214Z"},"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-08-04T17:00:14.114190Z","iopub.execute_input":"2024-08-04T17:00:14.114746Z","iopub.status.idle":"2024-08-04T17:00:16.390788Z","shell.execute_reply.started":"2024-08-04T17:00:14.114701Z","shell.execute_reply":"2024-08-04T17:00:16.389694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm = SVC(kernel='linear')\nsvm.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:00:16.392108Z","iopub.execute_input":"2024-08-04T17:00:16.392444Z","iopub.status.idle":"2024-08-04T17:18:08.363928Z","shell.execute_reply.started":"2024-08-04T17:00:16.392416Z","shell.execute_reply":"2024-08-04T17:18:08.362540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = svm.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:18:08.365571Z","iopub.execute_input":"2024-08-04T17:18:08.365960Z","iopub.status.idle":"2024-08-04T17:19:30.442094Z","shell.execute_reply.started":"2024-08-04T17:18:08.365929Z","shell.execute_reply":"2024-08-04T17:19:30.440178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:30.444557Z","iopub.execute_input":"2024-08-04T17:19:30.445558Z","iopub.status.idle":"2024-08-04T17:19:30.456274Z","shell.execute_reply.started":"2024-08-04T17:19:30.445494Z","shell.execute_reply":"2024-08-04T17:19:30.454864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:30.458533Z","iopub.execute_input":"2024-08-04T17:19:30.459477Z","iopub.status.idle":"2024-08-04T17:19:30.499380Z","shell.execute_reply.started":"2024-08-04T17:19:30.459418Z","shell.execute_reply":"2024-08-04T17:19:30.497866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Validation Accuracy: {val_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:30.501760Z","iopub.execute_input":"2024-08-04T17:19:30.502721Z","iopub.status.idle":"2024-08-04T17:19:30.511168Z","shell.execute_reply.started":"2024-08-04T17:19:30.502659Z","shell.execute_reply":"2024-08-04T17:19:30.509745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Validation Classification Report:')\nprint(val_class_report)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:30.527307Z","iopub.execute_input":"2024-08-04T17:19:30.528365Z","iopub.status.idle":"2024-08-04T17:19:30.535860Z","shell.execute_reply.started":"2024-08-04T17:19:30.528313Z","shell.execute_reply":"2024-08-04T17:19:30.534472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:30.538370Z","iopub.execute_input":"2024-08-04T17:19:30.539486Z","iopub.status.idle":"2024-08-04T17:19:30.883750Z","shell.execute_reply.started":"2024-08-04T17:19:30.539444Z","shell.execute_reply":"2024-08-04T17:19:30.882504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:30.885533Z","iopub.execute_input":"2024-08-04T17:19:30.886022Z","iopub.status.idle":"2024-08-04T17:19:32.787335Z","shell.execute_reply.started":"2024-08-04T17:19:30.885980Z","shell.execute_reply":"2024-08-04T17:19:32.786136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images, test_labels = load_images(test_dir)  \ntest_images = test_images / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:32.788802Z","iopub.execute_input":"2024-08-04T17:19:32.789158Z","iopub.status.idle":"2024-08-04T17:19:32.874885Z","shell.execute_reply.started":"2024-08-04T17:19:32.789127Z","shell.execute_reply":"2024-08-04T17:19:32.873677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:32.876514Z","iopub.execute_input":"2024-08-04T17:19:32.876963Z","iopub.status.idle":"2024-08-04T17:19:32.884341Z","shell.execute_reply.started":"2024-08-04T17:19:32.876931Z","shell.execute_reply":"2024-08-04T17:19:32.883076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_flat_scaled = scaler.transform(test_images_flat)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:32.885897Z","iopub.execute_input":"2024-08-04T17:19:32.886273Z","iopub.status.idle":"2024-08-04T17:19:32.900833Z","shell.execute_reply.started":"2024-08-04T17:19:32.886242Z","shell.execute_reply":"2024-08-04T17:19:32.899531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:32.902339Z","iopub.execute_input":"2024-08-04T17:19:32.902795Z","iopub.status.idle":"2024-08-04T17:19:33.250831Z","shell.execute_reply.started":"2024-08-04T17:19:32.902761Z","shell.execute_reply":"2024-08-04T17:19:33.249830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred = svm.predict(test_images_flat_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:33.252224Z","iopub.execute_input":"2024-08-04T17:19:33.252833Z","iopub.status.idle":"2024-08-04T17:19:34.752805Z","shell.execute_reply.started":"2024-08-04T17:19:33.252797Z","shell.execute_reply":"2024-08-04T17:19:34.751378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred","metadata":{"execution":{"iopub.status.busy":"2024-08-04T17:19:34.755470Z","iopub.execute_input":"2024-08-04T17:19:34.757473Z","iopub.status.idle":"2024-08-04T17:19:34.769750Z","shell.execute_reply.started":"2024-08-04T17:19:34.757423Z","shell.execute_reply":"2024-08-04T17:19:34.768354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:34.771777Z","iopub.execute_input":"2024-08-04T17:19:34.772771Z","iopub.status.idle":"2024-08-04T17:19:34.800337Z","shell.execute_reply.started":"2024-08-04T17:19:34.772725Z","shell.execute_reply":"2024-08-04T17:19:34.797853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:34.802904Z","iopub.execute_input":"2024-08-04T17:19:34.804726Z","iopub.status.idle":"2024-08-04T17:19:35.173028Z","shell.execute_reply.started":"2024-08-04T17:19:34.804667Z","shell.execute_reply":"2024-08-04T17:19:35.171736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:35.175083Z","iopub.execute_input":"2024-08-04T17:19:35.175531Z","iopub.status.idle":"2024-08-04T17:19:35.266584Z","shell.execute_reply.started":"2024-08-04T17:19:35.175493Z","shell.execute_reply":"2024-08-04T17:19:35.265344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T17:19:35.268411Z","iopub.execute_input":"2024-08-04T17:19:35.269087Z","iopub.status.idle":"2024-08-04T17:19:35.299336Z","shell.execute_reply.started":"2024-08-04T17:19:35.269043Z","shell.execute_reply":"2024-08-04T17:19:35.298048Z"},"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-08-04T17:19:35.300960Z","iopub.execute_input":"2024-08-04T17:19:35.301400Z","iopub.status.idle":"2024-08-04T17:19:51.908603Z","shell.execute_reply.started":"2024-08-04T17:19:35.301363Z","shell.execute_reply":"2024-08-04T17:19:51.907373Z"},"trusted":true},"execution_count":null,"outputs":[]}]}