{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-03-12T11:41:48.103995Z","iopub.execute_input":"2023-03-12T11:41:48.105124Z","iopub.status.idle":"2023-03-12T11:41:48.114971Z","shell.execute_reply.started":"2023-03-12T11:41:48.105069Z","shell.execute_reply":"2023-03-12T11:41:48.113642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport cv2\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:58:51.662149Z","iopub.execute_input":"2023-03-12T12:58:51.662474Z","iopub.status.idle":"2023-03-12T12:58:51.668416Z","shell.execute_reply.started":"2023-03-12T12:58:51.662441Z","shell.execute_reply":"2023-03-12T12:58:51.666910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## preparing data","metadata":{}},{"cell_type":"code","source":"def data_preprocessing(data_path,width =32, height=32):\n    filenames = os.listdir(data_path)\n    categories = []\n    data=[]\n    for image in filenames :\n        category = image.split('.')[0]\n        img = cv2.imread(data_path+'/'+image,0)\n        gaussian = cv2.GaussianBlur(img, (5, 5), 0)\n        normalized_img =gaussian/255\n        new_img = cv2.resize(normalized_img, (width,height))\n        new_img=new_img.reshape(width*height)\n        data.append(new_img)\n        if category == 'dog' :\n            categories.append(1)\n        else :    \n            categories.append(0)\n    return np.array(data),np.array(categories)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:58:51.671632Z","iopub.execute_input":"2023-03-12T12:58:51.672197Z","iopub.status.idle":"2023-03-12T12:58:51.684805Z","shell.execute_reply.started":"2023-03-12T12:58:51.672158Z","shell.execute_reply":"2023-03-12T12:58:51.683698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:58:51.686117Z","iopub.execute_input":"2023-03-12T12:58:51.686643Z","iopub.status.idle":"2023-03-12T12:59:00.649657Z","shell.execute_reply.started":"2023-03-12T12:58:51.686609Z","shell.execute_reply":"2023-03-12T12:59:00.648480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train'\nX,y=data_preprocessing(train_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:00.651276Z","iopub.execute_input":"2023-03-12T12:59:00.651714Z","iopub.status.idle":"2023-03-12T12:59:51.884184Z","shell.execute_reply.started":"2023-03-12T12:59:00.651680Z","shell.execute_reply":"2023-03-12T12:59:51.883095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.33,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:51.885801Z","iopub.execute_input":"2023-03-12T12:59:51.886272Z","iopub.status.idle":"2023-03-12T12:59:51.971082Z","shell.execute_reply.started":"2023-03-12T12:59:51.886225Z","shell.execute_reply":"2023-03-12T12:59:51.970069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## KNN Model","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:51.972690Z","iopub.execute_input":"2023-03-12T12:59:51.973037Z","iopub.status.idle":"2023-03-12T12:59:51.977924Z","shell.execute_reply.started":"2023-03-12T12:59:51.973004Z","shell.execute_reply":"2023-03-12T12:59:51.976753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kn=KNeighborsClassifier(n_neighbors=int(len(X_train)**0.5))","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:51.979582Z","iopub.execute_input":"2023-03-12T12:59:51.980621Z","iopub.status.idle":"2023-03-12T12:59:52.000140Z","shell.execute_reply.started":"2023-03-12T12:59:51.980573Z","shell.execute_reply":"2023-03-12T12:59:51.998919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kn=kn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:52.003008Z","iopub.execute_input":"2023-03-12T12:59:52.003618Z","iopub.status.idle":"2023-03-12T12:59:52.029468Z","shell.execute_reply.started":"2023-03-12T12:59:52.003577Z","shell.execute_reply":"2023-03-12T12:59:52.028032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_train=kn.predict(X_train)\ntrain_score=round(accuracy_score(y_train,pred_train),2)*100\nprint('train accuracy is: ',train_score)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T12:59:52.031084Z","iopub.execute_input":"2023-03-12T12:59:52.031551Z","iopub.status.idle":"2023-03-12T13:00:06.648309Z","shell.execute_reply.started":"2023-03-12T12:59:52.031514Z","shell.execute_reply":"2023-03-12T13:00:06.646917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test=kn.predict(X_test)\ntest_score=round(accuracy_score(y_test,pred_test),2)*100\nprint('test accuracy is: ',test_score)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T13:00:35.222184Z","iopub.execute_input":"2023-03-12T13:00:35.222629Z","iopub.status.idle":"2023-03-12T13:00:42.535880Z","shell.execute_reply.started":"2023-03-12T13:00:35.222594Z","shell.execute_reply":"2023-03-12T13:00:42.534662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}