{"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":"import matplotlib.pyplot as plt\n\n# 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":"2021-12-17T20:55:55.036447Z","iopub.execute_input":"2021-12-17T20:55:55.036963Z","iopub.status.idle":"2021-12-17T20:55:55.047943Z","shell.execute_reply.started":"2021-12-17T20:55:55.036924Z","shell.execute_reply":"2021-12-17T20:55:55.047101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/kaggle/input/dogs-vs-cats/test1.zip\n\ndata_set = \"dogs-vs-cats\"\n\nimport zipfile \nwith zipfile.ZipFile(\"/kaggle/input/\"+ data_set +\"/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    # save all files to kaggle/files/images\n    destination = '/kaggle/files/images'\n    z.extractall(destination)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:55:55.050201Z","iopub.execute_input":"2021-12-17T20:55:55.050473Z","iopub.status.idle":"2021-12-17T20:56:13.529980Z","shell.execute_reply.started":"2021-12-17T20:55:55.050440Z","shell.execute_reply":"2021-12-17T20:56:13.529217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_ = pd.DataFrame({'file': os.listdir('/kaggle/files/images/train')})\n#print(data_.head())","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:56:13.531264Z","iopub.execute_input":"2021-12-17T20:56:13.531589Z","iopub.status.idle":"2021-12-17T20:56:13.556446Z","shell.execute_reply.started":"2021-12-17T20:56:13.531546Z","shell.execute_reply":"2021-12-17T20:56:13.555693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nY=[]\nfor i in os.listdir('/kaggle/files/images/train'):\n    if 'dog' in i:\n        Y.append(1)\n    else:\n        Y.append(0)\n    \n        \ndata_['class'] = Y\n#print(data_.head())","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:56:13.558542Z","iopub.execute_input":"2021-12-17T20:56:13.558817Z","iopub.status.idle":"2021-12-17T20:56:14.497311Z","shell.execute_reply.started":"2021-12-17T20:56:13.558781Z","shell.execute_reply":"2021-12-17T20:56:14.496618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = data_['file']  \n\nY = data_['class']","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:56:14.499436Z","iopub.execute_input":"2021-12-17T20:56:14.500199Z","iopub.status.idle":"2021-12-17T20:56:14.505475Z","shell.execute_reply.started":"2021-12-17T20:56:14.500161Z","shell.execute_reply":"2021-12-17T20:56:14.504835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing matplotlib modules\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\n\nfrom skimage.feature import hog\nfrom skimage import data, exposure\nfrom skimage.transform import rescale, resize\n\n\ndata_size = 1200\nptr=0\nX = []\nfor i in file:\n    img = mpimg.imread('/kaggle/files/images/train/' + i)\n    resized_img = resize(img , (128 , 64))\n    fd, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),cells_per_block=(2, 2), visualize=True, multichannel = True)\n    X.append(fd)\n    ptr = ptr+1\n    if(ptr >= data_size):\n        break\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:03:08.981675Z","iopub.execute_input":"2021-12-17T21:03:08.981948Z","iopub.status.idle":"2021-12-17T21:04:49.814320Z","shell.execute_reply.started":"2021-12-17T21:03:08.981917Z","shell.execute_reply":"2021-12-17T21:04:49.813321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Y = Y.tolist()\nY = Y[:data_size]\n\n# dividing X, y into train and test data\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=0)\n#X_train.head()\n#print(type(X_train))#<class 'pandas.core.series.Series'>","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:05:40.570484Z","iopub.execute_input":"2021-12-17T21:05:40.571061Z","iopub.status.idle":"2021-12-17T21:05:40.578056Z","shell.execute_reply.started":"2021-12-17T21:05:40.571019Z","shell.execute_reply":"2021-12-17T21:05:40.576927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(type(X_train[0])) #<class 'numpy.ndarray'>\n#print(X_train[0].shape) #(3780,)\n\n# n is large relative to m (n = (3780,)  m=1200) --> model: SVM without a kernel","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:05:44.245365Z","iopub.execute_input":"2021-12-17T21:05:44.245871Z","iopub.status.idle":"2021-12-17T21:05:44.251185Z","shell.execute_reply.started":"2021-12-17T21:05:44.245834Z","shell.execute_reply":"2021-12-17T21:05:44.248503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# svm_LinearSVC  min(h^2)\nc = 1\n\nfrom sklearn.svm import LinearSVC\nsvm_LinearSVC = LinearSVC(C=c).fit(X_train, y_train) #minimize hinge loss Squared","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:10:21.707577Z","iopub.execute_input":"2021-12-17T21:10:21.707828Z","iopub.status.idle":"2021-12-17T21:10:23.494658Z","shell.execute_reply.started":"2021-12-17T21:10:21.707799Z","shell.execute_reply":"2021-12-17T21:10:23.493921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#svm_svc  min(h)\n\nfrom sklearn.svm import SVC\nsvm_svc = SVC(kernel='linear', C=c).fit(X_train, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:10:26.470671Z","iopub.execute_input":"2021-12-17T21:10:26.471364Z","iopub.status.idle":"2021-12-17T21:10:30.075543Z","shell.execute_reply.started":"2021-12-17T21:10:26.471328Z","shell.execute_reply":"2021-12-17T21:10:30.074843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# svm_LinearSVC accuracy\naccuracy = svm_LinearSVC.score(X_test, y_test)\nprint('svm_LinearSVC accuracy: ' + str(accuracy))\n\n#svm _svc accuracy\naccuracy = svm_svc.score(X_test, y_test)\nprint('svm_svc accuracy: ' + str(accuracy))\nprint(\"c = \",c)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T21:10:33.478182Z","iopub.execute_input":"2021-12-17T21:10:33.478455Z","iopub.status.idle":"2021-12-17T21:10:34.390134Z","shell.execute_reply.started":"2021-12-17T21:10:33.478424Z","shell.execute_reply":"2021-12-17T21:10:34.389296Z"},"trusted":true},"execution_count":null,"outputs":[]}]}