{"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":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import zipfile\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n\nimport joblib","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-12T00:58:27.307793Z","iopub.execute_input":"2024-03-12T00:58:27.308231Z","iopub.status.idle":"2024-03-12T00:58:27.655581Z","shell.execute_reply.started":"2024-03-12T00:58:27.308199Z","shell.execute_reply":"2024-03-12T00:58:27.654645Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:32:15.567667Z","iopub.execute_input":"2024-03-11T20:32:15.568575Z","iopub.status.idle":"2024-03-11T20:32:30.742871Z","shell.execute_reply.started":"2024-03-11T20:32:15.568516Z","shell.execute_reply":"2024-03-11T20:32:30.741505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:32:30.744819Z","iopub.execute_input":"2024-03-11T20:32:30.745399Z","iopub.status.idle":"2024-03-11T20:32:30.753565Z","shell.execute_reply.started":"2024-03-11T20:32:30.745349Z","shell.execute_reply":"2024-03-11T20:32:30.752006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nfor filename in os.listdir(path):\n    imgPath = os.path.join(path, filename)\n    img = cv2.imread(imgPath)\n    img = cv2.resize(img, (100,100))\n    label = filename.split('.')[0]\n    X.append(img)\n    y.append(label)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:32:30.757392Z","iopub.execute_input":"2024-03-11T20:32:30.758012Z","iopub.status.idle":"2024-03-11T20:33:26.917133Z","shell.execute_reply.started":"2024-03-11T20:32:30.757958Z","shell.execute_reply":"2024-03-11T20:33:26.915583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X))\nprint(len(y))","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:26.918743Z","iopub.execute_input":"2024-03-11T20:33:26.919198Z","iopub.status.idle":"2024-03-11T20:33:26.925541Z","shell.execute_reply.started":"2024-03-11T20:33:26.919156Z","shell.execute_reply":"2024-03-11T20:33:26.923952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:26.927444Z","iopub.execute_input":"2024-03-11T20:33:26.928015Z","iopub.status.idle":"2024-03-11T20:33:27.384370Z","shell.execute_reply.started":"2024-03-11T20:33:26.927962Z","shell.execute_reply":"2024-03-11T20:33:27.382627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[1], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.386550Z","iopub.execute_input":"2024-03-11T20:33:27.387159Z","iopub.status.idle":"2024-03-11T20:33:27.719333Z","shell.execute_reply.started":"2024-03-11T20:33:27.387100Z","shell.execute_reply":"2024-03-11T20:33:27.717831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(len(X), -1)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.721179Z","iopub.execute_input":"2024-03-11T20:33:27.721672Z","iopub.status.idle":"2024-03-11T20:33:27.728703Z","shell.execute_reply.started":"2024-03-11T20:33:27.721632Z","shell.execute_reply":"2024-03-11T20:33:27.727096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.730261Z","iopub.execute_input":"2024-03-11T20:33:27.730670Z","iopub.status.idle":"2024-03-11T20:33:27.744490Z","shell.execute_reply.started":"2024-03-11T20:33:27.730635Z","shell.execute_reply":"2024-03-11T20:33:27.742817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.748805Z","iopub.execute_input":"2024-03-11T20:33:27.750449Z","iopub.status.idle":"2024-03-11T20:33:27.762998Z","shell.execute_reply.started":"2024-03-11T20:33:27.750379Z","shell.execute_reply":"2024-03-11T20:33:27.761413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.765507Z","iopub.execute_input":"2024-03-11T20:33:27.766082Z","iopub.status.idle":"2024-03-11T20:33:27.779660Z","shell.execute_reply.started":"2024-03-11T20:33:27.765924Z","shell.execute_reply":"2024-03-11T20:33:27.778338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y).value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.781005Z","iopub.execute_input":"2024-03-11T20:33:27.781576Z","iopub.status.idle":"2024-03-11T20:33:27.812725Z","shell.execute_reply.started":"2024-03-11T20:33:27.781531Z","shell.execute_reply":"2024-03-11T20:33:27.811362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:27.814789Z","iopub.execute_input":"2024-03-11T20:33:27.815368Z","iopub.status.idle":"2024-03-11T20:33:28.113929Z","shell.execute_reply.started":"2024-03-11T20:33:27.815297Z","shell.execute_reply":"2024-03-11T20:33:28.112301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.max(), X_train.min())\nprint(X_test.max(), X_test.min())\nX_train = X_train/255\nX_test = X_test/255\nprint(X_train.max(), X_train.min())\nprint(X_test.max(), X_test.min())","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:28.115733Z","iopub.execute_input":"2024-03-11T20:33:28.116674Z","iopub.status.idle":"2024-03-11T20:33:33.019713Z","shell.execute_reply.started":"2024-03-11T20:33:28.116621Z","shell.execute_reply":"2024-03-11T20:33:33.018629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:33.020818Z","iopub.execute_input":"2024-03-11T20:33:33.021224Z","iopub.status.idle":"2024-03-11T20:33:33.027792Z","shell.execute_reply.started":"2024-03-11T20:33:33.021190Z","shell.execute_reply":"2024-03-11T20:33:33.026487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.ndim)\nprint(y_train.ndim)\nprint(X_test.ndim)\nprint(y_test.ndim)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:33.029475Z","iopub.execute_input":"2024-03-11T20:33:33.030350Z","iopub.status.idle":"2024-03-11T20:33:33.043310Z","shell.execute_reply.started":"2024-03-11T20:33:33.030309Z","shell.execute_reply":"2024-03-11T20:33:33.041795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SVC(kernel='rbf')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:33.045847Z","iopub.execute_input":"2024-03-11T20:33:33.046527Z","iopub.status.idle":"2024-03-11T20:33:33.059664Z","shell.execute_reply.started":"2024-03-11T20:33:33.046466Z","shell.execute_reply":"2024-03-11T20:33:33.058180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T20:33:33.061667Z","iopub.execute_input":"2024-03-11T20:33:33.062957Z","iopub.status.idle":"2024-03-11T23:19:26.803471Z","shell.execute_reply.started":"2024-03-11T20:33:33.062909Z","shell.execute_reply":"2024-03-11T23:19:26.800695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:28:44.940113Z","iopub.execute_input":"2024-03-11T23:28:44.940578Z","iopub.status.idle":"2024-03-12T00:52:02.852752Z","shell.execute_reply.started":"2024-03-11T23:28:44.940542Z","shell.execute_reply":"2024-03-12T00:52:02.841785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = accuracy_score(y_test, y_pred)\nprint(f\"Accuracy: {accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2024-03-12T00:52:02.862926Z","iopub.execute_input":"2024-03-12T00:52:02.868880Z","iopub.status.idle":"2024-03-12T00:52:02.916791Z","shell.execute_reply.started":"2024-03-12T00:52:02.868785Z","shell.execute_reply":"2024-03-12T00:52:02.915266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-03-12T00:53:38.478290Z","iopub.execute_input":"2024-03-12T00:53:38.478708Z","iopub.status.idle":"2024-03-12T00:53:38.568373Z","shell.execute_reply.started":"2024-03-12T00:53:38.478677Z","shell.execute_reply":"2024-03-12T00:53:38.567466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred)\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.xlabel('Predicted labels')\nplt.ylabel('True labels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T00:58:39.538933Z","iopub.execute_input":"2024-03-12T00:58:39.540427Z","iopub.status.idle":"2024-03-12T00:58:39.833105Z","shell.execute_reply.started":"2024-03-12T00:58:39.540373Z","shell.execute_reply":"2024-03-12T00:58:39.831865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joblib.dump(model, 'cat_vs_dog_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-03-12T01:00:14.419728Z","iopub.execute_input":"2024-03-12T01:00:14.420256Z","iopub.status.idle":"2024-03-12T01:00:23.729357Z","shell.execute_reply.started":"2024-03-12T01:00:14.420215Z","shell.execute_reply":"2024-03-12T01:00:23.728123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}