{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"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","trusted":true,"execution":{"iopub.status.busy":"2025-06-28T10:40:57.20309Z","iopub.execute_input":"2025-06-28T10:40:57.203392Z","iopub.status.idle":"2025-06-28T10:40:59.531433Z","shell.execute_reply.started":"2025-06-28T10:40:57.203361Z","shell.execute_reply":"2025-06-28T10:40:59.529989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\nprint(os.listdir(\"/kaggle/input/dogs-vs-cats\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T11:27:25.916355Z","iopub.execute_input":"2025-06-28T11:27:25.916708Z","iopub.status.idle":"2025-06-28T11:27:25.922692Z","shell.execute_reply.started":"2025-06-28T11:27:25.916676Z","shell.execute_reply":"2025-06-28T11:27:25.921647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\ntrain_zip_path = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntrain_unzip_path = \"/kaggle/working/train\"\nos.makedirs(train_unzip_path, exist_ok=True)\nwith zipfile.ZipFile(train_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(train_unzip_path)\nprint(\"✅ Train images extraction complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T12:08:23.242441Z","iopub.execute_input":"2025-06-28T12:08:23.242994Z","iopub.status.idle":"2025-06-28T12:08:29.798396Z","shell.execute_reply.started":"2025-06-28T12:08:23.242967Z","shell.execute_reply":"2025-06-28T12:08:29.797637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nbase_dir = \"/kaggle/working/train/train\"\nimage_data = []\nlabels = []\nfor filename in os.listdir(base_dir):\n    if filename.endswith(\".jpg\"):\n        img_path = os.path.join(base_dir, filename)\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, (64, 64))\n        flattened_img = img.flatten()\n        image_data.append(flattened_img)\n        if \"cat\" in filename:\n            labels.append(0)\n        elif \"dog\" in filename:\n            labels.append(1)\nimage_data = np.array(image_data)\nlabels = np.array(labels)\nimage_data = image_data / 255.0\nprint(\"✅ Image loading and preprocessing complete.\")\nprint(f\"Image data shape: {image_data.shape}\")\nprint(f\"Labels shape: {labels.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T12:08:43.0762Z","iopub.execute_input":"2025-06-28T12:08:43.076855Z","iopub.status.idle":"2025-06-28T12:09:07.960852Z","shell.execute_reply.started":"2025-06-28T12:08:43.076831Z","shell.execute_reply":"2025-06-28T12:09:07.960075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(image_data, labels, test_size=0.2, random_state=42)\nprint(f\"X_train shape: {X_train.shape}\")\nprint(f\"X_test shape: {X_test.shape}\")\nprint(f\"y_train shape: {y_train.shape}\")\nprint(f\"y_test shape: {y_test.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T12:09:49.079878Z","iopub.execute_input":"2025-06-28T12:09:49.080279Z","iopub.status.idle":"2025-06-28T12:09:50.065602Z","shell.execute_reply.started":"2025-06-28T12:09:49.080254Z","shell.execute_reply":"2025-06-28T12:09:50.064735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.svm import SVC\nsvm_model = SVC()\nsvm_model.fit(X_train, y_train)\nprint(\"✅ SVM model training complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T12:09:52.574939Z","iopub.execute_input":"2025-06-28T12:09:52.575648Z","iopub.status.idle":"2025-06-28T12:57:01.927772Z","shell.execute_reply.started":"2025-06-28T12:09:52.575614Z","shell.execute_reply":"2025-06-28T12:57:01.924669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ny_pred = svm_model.predict(X_test)\naccuracy = accuracy_score(y_test, y_pred)\nprint(f\"Accuracy of the SVM model: {accuracy:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T13:04:22.731276Z","iopub.execute_input":"2025-06-28T13:04:22.732127Z","iopub.status.idle":"2025-06-28T13:36:52.208598Z","shell.execute_reply.started":"2025-06-28T13:04:22.732071Z","shell.execute_reply":"2025-06-28T13:36:52.206785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test1_zip_path = \"/kaggle/input/dogs-vs-cats/test1.zip\"\ntest1_unzip_path = \"/kaggle/working/test1\"\nos.makedirs(test1_unzip_path, exist_ok=True)\nwith zipfile.ZipFile(test1_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(test1_unzip_path)\nprint(\"✅ Train images extraction complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T13:37:44.772397Z","iopub.execute_input":"2025-06-28T13:37:44.773219Z","iopub.status.idle":"2025-06-28T13:37:51.127214Z","shell.execute_reply.started":"2025-06-28T13:37:44.773187Z","shell.execute_reply":"2025-06-28T13:37:51.126404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir = \"/kaggle/working/test1/test1\"\ntest_image_data = []\ntest_image_ids = []\nfor filename in os.listdir(test_dir):\n    if filename.endswith(\".jpg\"):\n        image_id = int(os.path.splitext(filename)[0])\n        test_image_ids.append(image_id)\n        img_path = os.path.join(test_dir, filename)\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, (64, 64))\n        flattened_img = img.flatten()\n        test_image_data.append(flattened_img)\ntest_image_data = np.array(test_image_data)\ntest_image_data = test_image_data / 255.0\nprint(\"✅ Test image loading and preprocessing complete.\")\nprint(f\"Test image data shape: {test_image_data.shape}\")\nprint(f\"Test image IDs shape: {len(test_image_ids)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T13:37:55.686351Z","iopub.execute_input":"2025-06-28T13:37:55.686662Z","iopub.status.idle":"2025-06-28T13:38:07.796087Z","shell.execute_reply.started":"2025-06-28T13:37:55.686639Z","shell.execute_reply":"2025-06-28T13:38:07.795063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = svm_model.predict(test_image_data)\nprint(\"✅ Predictions on test set complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T13:38:15.016518Z","iopub.execute_input":"2025-06-28T13:38:15.016847Z","iopub.status.idle":"2025-06-28T15:00:30.11239Z","shell.execute_reply.started":"2025-06-28T13:38:15.016822Z","shell.execute_reply":"2025-06-28T15:00:30.108875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import joblib\n\n# Define the path to save the model\nmodel_save_path = \"svm_cat_dog_model.pkl\"\n\n# Save the trained model\njoblib.dump(svm_model, model_save_path)\n\nprint(f\"✅ SVM model saved successfully to {model_save_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T15:00:35.678823Z","iopub.execute_input":"2025-06-28T15:00:35.679552Z","iopub.status.idle":"2025-06-28T15:00:46.49166Z","shell.execute_reply.started":"2025-06-28T15:00:35.679481Z","shell.execute_reply":"2025-06-28T15:00:46.490553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Create a DataFrame with image IDs and predictions\nsubmission_df = pd.DataFrame({'id': test_image_ids, 'label': test_predictions})\n\n# Sort by image ID\nsubmission_df = submission_df.sort_values(by='id')\n\n# Save to submission.csv\nsubmission_df.to_csv('submission.csv', index=False)\n\nprint(\"✅ submission.csv created successfully.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T15:02:08.247447Z","iopub.execute_input":"2025-06-28T15:02:08.248365Z","iopub.status.idle":"2025-06-28T15:02:09.56304Z","shell.execute_reply.started":"2025-06-28T15:02:08.248336Z","shell.execute_reply":"2025-06-28T15:02:09.562149Z"}},"outputs":[],"execution_count":null}]}