{"cells":[{"metadata":{"_cell_guid":"caffd5e2-6c6a-4e91-a70c-ec25f33a4e0a","_uuid":"b8918d53ae00c330b14ef9bacfe317594fd814c9","collapsed":true,"trusted":true},"cell_type":"code","source":"import os, cv2, itertools\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5922169e-fb93-4f2f-a770-5e8b39fd2944","scrolled":true,"_uuid":"55e555a46f76d9f4b1ef6c982e6ac9726fb778e5","trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\nROWS = 64\nCOLS = 64\nCHANNELS = 3\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntest_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n\n\n# Sample from train and test data.\n\"\"\"\nsample_size = 5000\ntrain_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\ntrain_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\ntrain_images = np.hstack([np.random.choice(train_dogs, sample_size, replace=False),\n                          np.random.choice(train_cats, sample_size, replace=False)])\n\ntest_images =  test_images[:sample_size]\n\"\"\"\n\ndef read_image(file_path):\n    img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE\n    return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n\n\ndef prep_data(images):\n    \"\"\"\n    Returns:\n        X(n_x, m)\n        y(1, m) -- 1: dog, 0: cat\n    \"\"\"\n    m = len(images)\n    n_x = ROWS * COLS * CHANNELS\n    \n    X = np.ndarray((n_x, m), dtype=np.uint8)\n    y = np.zeros((1, m))\n    print (\"X shape is {}\".format(X.shape))\n    \n    for i, image_file in enumerate(images):\n        image = read_image(image_file)\n        X[:, i] = np.squeeze(image.reshape((n_x, 1)))\n        if 'dog' in image_file.lower():\n            y[0, i] = 1\n        elif 'cat' in image_file.lower():\n            y[0, i] = 0\n        else:# if neither dog nor cat exist, return the image index (this is the case for test data)\n            y[0, i] = image_file.split('/')[-1].split('.')[0]\n        if i%1000 == 0: print('Processed {} of {}'.format(i, m))\n    \n    return X, y\n\nX_train, y_train = prep_data(train_images)\nX_test, test_idx = prep_data(test_images)\n\nprint(\"Train shape: {}\".format(X_train.shape))\nprint(\"Test shape: {}\".format(X_test.shape))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5b3e24f6-6745-4f6f-a007-88c09e748638","_uuid":"ea48b9db43e8ffbbbf2eba17f5c898c6efc30016","collapsed":true,"trusted":true},"cell_type":"code","source":"classes = {0:'cat',\n           1:'dog'}\ndef show_image(X, y, idx):\n    image = X[idx]\n    image = image.reshape((ROWS, COLS, CHANNELS))\n    plt.figure(figsize=(4,2))\n    plt.imshow(image)\n    plt.title(\"This is a {}\".format(classes[y[idx,0]]))\n    plt.show()\n    \ndef show_image_prediction(X, idx, model):\n    image = X[idx].reshape(1, -1)\n    image_class = classes[model.predict(image).item()]\n    image = image.reshape((ROWS, COLS, CHANNELS))\n    plt.figure(figsize=(4,2))\n    plt.imshow(image)\n    plt.title(\"Test {}: I think this is a {}\".format(idx, image_class))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0a4f834b-9850-4905-9e48-2938dc63dfd6","_uuid":"7f600f710e003ca4de6c1ecd946917e0f9be789e","trusted":true},"cell_type":"code","source":"show_image(X_train.T, y_train.T, 1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2af41974-4d33-4988-a647-6c10112b8435","_uuid":"075f00b47b10fb940f657ccf153f4842836a2342"},"cell_type":"markdown","source":"# Logistic Regression","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"1b3a1679-f8eb-47fd-a300-bc7043beeaa5","_uuid":"a68f46fb982bce2ba11beb4018821ff094670d6d","trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegressionCV\nfrom sklearn.metrics import confusion_matrix\nclf = LogisticRegressionCV()\nX_train_lr, y_train_lr = X_train.T, y_train.T.ravel()\nclf.fit(X_train_lr, y_train_lr)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"85a35fb10336e19d67c3a4d775d2f788e5fde7f5","_cell_guid":"b529a67e-65ac-4bac-b301-43a987e0a905","trusted":true},"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], fmt),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3768c1e6-46ff-423e-a5d4-76293feece26","_uuid":"3c4827ce367c47d76231b5c4685c5d9698b6d1aa","trusted":true},"cell_type":"code","source":"print(\"Model accuracy: {:.2f}%\".format(clf.score(X_train_lr, y_train_lr)*100))\nplot_confusion_matrix(confusion_matrix(y_train_lr, clf.predict(X_train_lr)), ['cat', 'dog'])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d2532dc5-cbe3-43be-b3e7-914886d0cd9e","scrolled":false,"_uuid":"ce04a46ca71887fb8416ce07ee3eaa2885ae99c0","trusted":true},"cell_type":"code","source":"X_test_lr, test_idx = X_test.T, test_idx.T\nfor i in np.random.randint(0, len(X_test_lr), 10):\n    show_image_prediction(X_test_lr, i, clf)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"78ff488e-b8f0-40a9-9bdc-03d2413d1eb4","_uuid":"9f08d860b686f31a73e6e2bbe4b35bb18f1959cf","trusted":true,"collapsed":true},"cell_type":"code","source":"submission = pd.DataFrame(np.hstack([test_idx, clf.predict_proba(X_test_lr)]), columns=['id', 'cat', 'dog'])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5bb9b63c-e463-4d24-87d2-864f17c99f1e","_uuid":"ff9907b8c9d6c41184b98497f3b8bc3b53c22cf3","trusted":true,"collapsed":true},"cell_type":"code","source":"submission = submission.drop(['cat'], axis=1)\nsubmission = submission.rename(index=str, columns={\"dog\": \"label\"})\nsubmission['id'] = submission['id'].astype(int)\nsubmission.sort_values('id', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"64c6668a-96e3-4e07-8164-38dd5b3de20a","_uuid":"06e9c20b429ec33acc2b37f6226404bf429b5972","trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca7b8345ca27e580c3b8c8a6f23bc11344eccdd5"},"cell_type":"code","source":"submission.to_csv('STahamtan_Dog_vs_Cat_Submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}