{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pip install imutils","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\nfrom imutils import paths\nimport numpy as np\nimport imutils \nimport cv2 \nimport os\nimport argparse\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def image_to_feature_vector(image, size=(32, 32)):\n    return cv2.resize(image, size).flatten()\n\ndataset_train = \"/content/input2/train/\"\n\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset_train))\n\nprint(len(imagePaths))\n\nrawImages = []\nlabels = []\nfor (i, imagePath) in enumerate(imagePaths):\n\n    image = cv2.imread(imagePath)\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0]\n \n\n    pixels = image_to_feature_vector(image)\n    \n \n\n    rawImages.append(pixels)\n    labels.append(label)\n\n    if i > 0 and i % 1000 == 0:\n        print(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nrawImages = np.array(rawImages)\nlabels = np.array(labels)\n\nprint(\"[INFO] pixels matrix: {:.2f}MB\".format(\nrawImages.nbytes / (1024 * 1000.0)))\nbUseCompleteDataset = False\nif bUseCompleteDataset:\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.25, random_state=42)\nelse:\n    rawImages_subset = rawImages[:2000]\n    labels_subset = labels[:2000]\n    (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages_subset, labels_subset, test_size=0.25, random_state=42)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"[INFO] evaluating raw pixel accuracy...\")\nneighbors = [1, 3, 5, 7, 13]\n\nfor k in neighbors:\n    model = KNeighborsClassifier(n_neighbors= 5)\n    a=model.fit(trainRI, trainRL)\n    acc = model.score(testRI, testRL)\n    print(\"[INFO] raw pixel accuracy: {:.2f}%\".format(acc * 100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_test = \"/content/input2/test1/\"\n\nimagePaths_test=list(paths.list_images(dataset_test))\n\nprint(len(imagePaths_test))\nrawImages_test=[]\nlabels_test=[]\n\nfor(i,imagePath_test) in enumerate(imagePaths_test):\n    image=cv2.imread(imagePath_test)\n    label=imagePath_test.split(os.path.sep)[-1].split(\".\")[0]\n\n    pixels=image_to_feature_vector(image)\n\n    rawImages_test.append(pixels)\n    labels_test.append(label)\n\n\n    if i>0 and i%1000==0:\n      print(\"[INFO] processed {}/{}\".format(i,len(imagePaths_test)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = KNeighborsClassifier(n_neighbors= 5)\na=model.fit(trainRI, trainRL)\n\nresult=a.predict(rawImages_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\n\n\nresult=np.reshape(result,(-1,1))\n\nprint(result.shape)\ndf = pd.DataFrame(result, columns=[\"label\"])\ndf.index.name = 'id'\ndf.index += 1\ndf = df.replace('dog',1)\ndf = df.replace('cat',0)\ndf = df.rename({'1':'id','0':'label'})\ndf.to_csv('results-nk-v2.csv',index=True, header=True)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}