{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":31260,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2026-01-19T14:32:16.602795Z","iopub.execute_input":"2026-01-19T14:32:16.603166Z","iopub.status.idle":"2026-01-19T14:32:17.864262Z","shell.execute_reply.started":"2026-01-19T14:32:16.603136Z","shell.execute_reply":"2026-01-19T14:32:17.863418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q \"/kaggle/input/dogs-vs-cats/train.zip\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:17.865501Z","iopub.execute_input":"2026-01-19T14:32:17.865826Z","iopub.status.idle":"2026-01-19T14:32:26.339849Z","shell.execute_reply.started":"2026-01-19T14:32:17.865803Z","shell.execute_reply":"2026-01-19T14:32:26.338825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q \"/kaggle/input/dogs-vs-cats/test1.zip\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:26.341295Z","iopub.execute_input":"2026-01-19T14:32:26.341801Z","iopub.status.idle":"2026-01-19T14:32:30.473692Z","shell.execute_reply.started":"2026-01-19T14:32:26.341770Z","shell.execute_reply":"2026-01-19T14:32:30.472889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom sklearn.model_selection import train_test_split\nimport cv2\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:30.475664Z","iopub.execute_input":"2026-01-19T14:32:30.475885Z","iopub.status.idle":"2026-01-19T14:32:31.437398Z","shell.execute_reply.started":"2026-01-19T14:32:30.475860Z","shell.execute_reply":"2026-01-19T14:32:31.436837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef prepare_data(path,val_size = 0.25, random_state=42):\n    filenames = os.listdir(path)\n\n    categories = []\n    for filename in filenames:\n        if filename.startswith(\"dog\"):\n            categories.append(\"dog\")\n        elif filename.startswith(\"cat\"):\n            categories.append(\"cat\")\n        else:\n            continue\n\n    df = pd.DataFrame({\n        \"filename\" : filenames[:len(categories)],\n        \"category\" : categories\n    })\n\n    train_df, val_df= train_test_split(\n        df,\n        test_size = val_size,\n        random_state = random_state,\n        stratify = df[\"category\"]\n    )\n\n    return train_df, val_df\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.438152Z","iopub.execute_input":"2026-01-19T14:32:31.438464Z","iopub.status.idle":"2026-01-19T14:32:31.443633Z","shell.execute_reply.started":"2026-01-19T14:32:31.438418Z","shell.execute_reply":"2026-01-19T14:32:31.442928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = \"/kaggle/working/train\"\ntrain_df, val_df = prepare_data(train_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.444413Z","iopub.execute_input":"2026-01-19T14:32:31.444735Z","iopub.status.idle":"2026-01-19T14:32:31.507707Z","shell.execute_reply.started":"2026-01-19T14:32:31.444714Z","shell.execute_reply":"2026-01-19T14:32:31.507152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Total Training Images: {len(train_df)}\")\nprint(f\"Total Validation Images: {len(val_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.508407Z","iopub.execute_input":"2026-01-19T14:32:31.508641Z","iopub.status.idle":"2026-01-19T14:32:31.513040Z","shell.execute_reply.started":"2026-01-19T14:32:31.508621Z","shell.execute_reply":"2026-01-19T14:32:31.512322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.513945Z","iopub.execute_input":"2026-01-19T14:32:31.514218Z","iopub.status.idle":"2026-01-19T14:32:31.538673Z","shell.execute_reply.started":"2026-01-19T14:32:31.514190Z","shell.execute_reply":"2026-01-19T14:32:31.538110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.539492Z","iopub.execute_input":"2026-01-19T14:32:31.539767Z","iopub.status.idle":"2026-01-19T14:32:31.565675Z","shell.execute_reply.started":"2026-01-19T14:32:31.539737Z","shell.execute_reply":"2026-01-19T14:32:31.564981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.567619Z","iopub.execute_input":"2026-01-19T14:32:31.568015Z","iopub.status.idle":"2026-01-19T14:32:31.591776Z","shell.execute_reply.started":"2026-01-19T14:32:31.567995Z","shell.execute_reply":"2026-01-19T14:32:31.591084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['category'].nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.592603Z","iopub.execute_input":"2026-01-19T14:32:31.592873Z","iopub.status.idle":"2026-01-19T14:32:31.598181Z","shell.execute_reply.started":"2026-01-19T14:32:31.592833Z","shell.execute_reply":"2026-01-19T14:32:31.597483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['category'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.599030Z","iopub.execute_input":"2026-01-19T14:32:31.599309Z","iopub.status.idle":"2026-01-19T14:32:31.613776Z","shell.execute_reply.started":"2026-01-19T14:32:31.599287Z","shell.execute_reply":"2026-01-19T14:32:31.613233Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model: SVM","metadata":{}},{"cell_type":"markdown","source":"> Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or cat vs. dog.\n>\n> Image → grayscale → resize → flatten → SVM → prediction","metadata":{}},{"cell_type":"code","source":"# Load images & convert to numbers\n\nIMG_SIZE = 32  \n\ndef load_images(df, img_dir):\n    X, y = [], []\n\n    for _, row in df.iterrows():\n        path = os.path.join(img_dir, row['filename'])\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n\n        if img is None:\n            continue\n\n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n        X.append(img.flatten())\n        y.append(1 if row['category'] == 'dog' else 0)\n\n    return np.array(X), np.array(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.614458Z","iopub.execute_input":"2026-01-19T14:32:31.614694Z","iopub.status.idle":"2026-01-19T14:32:31.627041Z","shell.execute_reply.started":"2026-01-19T14:32:31.614663Z","shell.execute_reply":"2026-01-19T14:32:31.626324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare train & validation data\n\nX_train, y_train = load_images(train_df, train_path)\nX_val, y_val = load_images(val_df, train_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:31.627888Z","iopub.execute_input":"2026-01-19T14:32:31.628223Z","iopub.status.idle":"2026-01-19T14:32:47.953871Z","shell.execute_reply.started":"2026-01-19T14:32:31.628195Z","shell.execute_reply":"2026-01-19T14:32:47.953287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(X_val.shape)\nprint(y_val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:32:47.954722Z","iopub.execute_input":"2026-01-19T14:32:47.955007Z","iopub.status.idle":"2026-01-19T14:32:47.959097Z","shell.execute_reply.started":"2026-01-19T14:32:47.954975Z","shell.execute_reply":"2026-01-19T14:32:47.958470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import Pipeline\n\nsvm = Pipeline([\n    (\"scaler\", StandardScaler()),\n    (\"svm\", SVC(kernel=\"linear\", C=1, max_iter=3000))\n])\n\n\nX_train_small = X_train[:3000]\ny_train_small = y_train[:3000]\n\nsvm.fit(X_train_small, y_train_small)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:37:20.799450Z","iopub.execute_input":"2026-01-19T14:37:20.799680Z","iopub.status.idle":"2026-01-19T14:37:22.724805Z","shell.execute_reply.started":"2026-01-19T14:37:20.799659Z","shell.execute_reply":"2026-01-19T14:37:22.724124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\ny_pred = svm.predict(X_val)\nprint(\"Accuracy:\", accuracy_score(y_val, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T14:37:22.725669Z","iopub.execute_input":"2026-01-19T14:37:22.725939Z","iopub.status.idle":"2026-01-19T14:37:26.180297Z","shell.execute_reply.started":"2026-01-19T14:37:22.725918Z","shell.execute_reply":"2026-01-19T14:37:26.179695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}