{"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)\nimport cv2\nimport matplotlib.pyplot as plt\n# import tensorflow as tf\n# from tensorflow.keras.models import Sequential\n# from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D\nfrom sklearn.ensemble import RandomForestClassifier\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\n\n# unzip datasets\nimport zipfile\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n\n# read in dataset images in greyscale\n\nmain_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array, dsize=(80, 80))\n#     print(np.asarray(new_img_array).reshape(1, 6400).shape)\n    plt.imshow(new_img_array,cmap=\"gray\")\n    break","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-15T23:57:19.252101Z","iopub.execute_input":"2023-03-15T23:57:19.252515Z","iopub.status.idle":"2023-03-15T23:57:19.506063Z","shell.execute_reply.started":"2023-03-15T23:57:19.252480Z","shell.execute_reply":"2023-03-15T23:57:19.504805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example image in greyscale scaled down to 80x80","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"# process training data\nmain_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array, dsize=(80, 80))\n#     plt.imshow(new_img_array,cmap=\"gray\")\n\n# create arrays for model training\nX = []\ny = []\nconvert = lambda category : int(category == 'dog') # maps dog to 1, cat to 0 for labeling\ndef create_test_data(path):\n    for p in os.listdir(path):\n        category = p.split(\".\")[0] # extract category name from image name (category.num.jpg)\n        category = convert(category) # map category to int value\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE) # contains the values of the pixels in each img, (0-255)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80)) # resize image\n        new_img_array_flat = np.asarray(new_img_array).reshape(1, 6400)\n        X.append(new_img_array_flat)\n        y.append(category) # add category (cat/dog)\n        \ncreate_test_data(path)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-15T23:58:31.231365Z","iopub.execute_input":"2023-03-15T23:58:31.231765Z","iopub.status.idle":"2023-03-15T23:59:22.669761Z","shell.execute_reply.started":"2023-03-15T23:58:31.231732Z","shell.execute_reply":"2023-03-15T23:59:22.668249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X).reshape(-1, 6400)\ny = np.array(y)\n\n# normalize data to values between 0 and 1\nX_train = X_train/255","metadata":{"execution":{"iopub.status.busy":"2023-03-16T00:00:49.102423Z","iopub.execute_input":"2023-03-16T00:00:49.103666Z","iopub.status.idle":"2023-03-16T00:00:49.708401Z","shell.execute_reply.started":"2023-03-16T00:00:49.103621Z","shell.execute_reply":"2023-03-16T00:00:49.707438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create models\nrfc = RandomForestClassifier(random_state = 42)\nrfc.fit(X_train, y)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-16T00:00:58.678233Z","iopub.execute_input":"2023-03-16T00:00:58.679730Z","iopub.status.idle":"2023-03-16T00:04:24.563556Z","shell.execute_reply.started":"2023-03-16T00:00:58.679665Z","shell.execute_reply":"2023-03-16T00:04:24.561930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# process test set with same process as before\ntrain_dir = \"test1\"\npath = os.path.join(main_dir,train_dir)\n#os.listdir(path)\n\nX_test = []\nid_line = []\ndef create_test1_data(path):\n    for p in os.listdir(path):\n        id_line.append(p.split(\".\")[0])\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        new_img_array_flat = np.asarray(new_img_array).reshape(1, 6400)\n        X_test.append(new_img_array_flat)\ncreate_test1_data(path)\nX_test = np.array(X_test).reshape(-1,6400)\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2023-03-16T00:04:31.725067Z","iopub.execute_input":"2023-03-16T00:04:31.726366Z","iopub.status.idle":"2023-03-16T00:04:45.311442Z","shell.execute_reply.started":"2023-03-16T00:04:31.726314Z","shell.execute_reply":"2023-03-16T00:04:45.310111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make predictions on test data\npreds = rfc.predict(X_test)\n\nsubmission_df = pd.DataFrame({'id':id_line, 'label':preds})\n\nprint(submission_df)\n\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T00:04:51.259346Z","iopub.execute_input":"2023-03-16T00:04:51.259855Z","iopub.status.idle":"2023-03-16T00:04:52.670651Z","shell.execute_reply.started":"2023-03-16T00:04:51.259810Z","shell.execute_reply":"2023-03-16T00:04:52.668949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\n# accuracy_score(y)\nprint(rfc.predict(X_train))\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T00:13:13.828727Z","iopub.execute_input":"2023-03-16T00:13:13.829369Z","iopub.status.idle":"2023-03-16T00:13:16.164484Z","shell.execute_reply.started":"2023-03-16T00:13:13.829276Z","shell.execute_reply":"2023-03-16T00:13:16.162869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}