{"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-11-05T23:46:46.451784Z","iopub.execute_input":"2023-11-05T23:46:46.452545Z","iopub.status.idle":"2023-11-05T23:46:46.808825Z","shell.execute_reply.started":"2023-11-05T23:46:46.452515Z","shell.execute_reply":"2023-11-05T23:46:46.807785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:46:49.954460Z","iopub.execute_input":"2023-11-05T23:46:49.954909Z","iopub.status.idle":"2023-11-05T23:46:50.326160Z","shell.execute_reply.started":"2023-11-05T23:46:49.954882Z","shell.execute_reply":"2023-11-05T23:46:50.325111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"working_input_dir = \"/kaggle/input/digit-recognizer\"\ntrain_data = pd.read_csv(os.path.join(working_input_dir,\"train.csv\"))\ntest_data = pd.read_csv(os.path.join(working_input_dir, \"test.csv\"))\nsubmission_data = pd.read_csv(os.path.join(working_input_dir, \"sample_submission.csv\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:46:52.488786Z","iopub.execute_input":"2023-11-05T23:46:52.489520Z","iopub.status.idle":"2023-11-05T23:46:58.826813Z","shell.execute_reply.started":"2023-11-05T23:46:52.489485Z","shell.execute_reply":"2023-11-05T23:46:58.825813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:53:54.577269Z","iopub.execute_input":"2023-11-05T23:53:54.577628Z","iopub.status.idle":"2023-11-05T23:53:54.583624Z","shell.execute_reply.started":"2023-11-05T23:53:54.577599Z","shell.execute_reply":"2023-11-05T23:53:54.582759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:47:06.745555Z","iopub.execute_input":"2023-11-05T23:47:06.746026Z","iopub.status.idle":"2023-11-05T23:47:06.777838Z","shell.execute_reply.started":"2023-11-05T23:47:06.745988Z","shell.execute_reply":"2023-11-05T23:47:06.776959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extracted the label out of train data\n","metadata":{}},{"cell_type":"code","source":"train_minus_labels = train_data.drop('label', axis = 1)\ntrain_arr = train_minus_labels.to_numpy()\n\ntest_arr = test_data.to_numpy()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:47:09.385156Z","iopub.execute_input":"2023-11-05T23:47:09.385545Z","iopub.status.idle":"2023-11-05T23:47:09.487079Z","shell.execute_reply.started":"2023-11-05T23:47:09.385513Z","shell.execute_reply":"2023-11-05T23:47:09.485762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Input Data creation into size of (42000,28,28)","metadata":{}},{"cell_type":"code","source":"def input_data_creation(main_arr, img_rows, img_cols):\n    imgs_count = main_arr.shape[1]\n    imgs_list = []\n    for row in main_arr:\n        img_arr = np.array(row).reshape(img_rows, img_cols)\n        imgs_list.append(img_arr)\n    \n    return np.array(imgs_list)\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:47:11.566315Z","iopub.execute_input":"2023-11-05T23:47:11.566725Z","iopub.status.idle":"2023-11-05T23:47:11.572400Z","shell.execute_reply.started":"2023-11-05T23:47:11.566681Z","shell.execute_reply":"2023-11-05T23:47:11.571459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = input_data_creation(train_arr, 28, 28)\ntest_images = input_data_creation(test_arr, 28, 28)\nlabels = np.array(train_data['label'])","metadata":{"execution":{"iopub.status.busy":"2023-11-05T23:47:13.772737Z","iopub.execute_input":"2023-11-05T23:47:13.773565Z","iopub.status.idle":"2023-11-05T23:47:14.311737Z","shell.execute_reply.started":"2023-11-05T23:47:13.773515Z","shell.execute_reply":"2023-11-05T23:47:14.310577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\ndef random_flip(image):\n    if random.random() > 0.5:\n        image = np.flip(image, axis = 1)\n    if random.random() > 0.5:\n        image = np.flip(image, axis = 0)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:15:07.474577Z","iopub.execute_input":"2023-11-06T00:15:07.475345Z","iopub.status.idle":"2023-11-06T00:15:07.480392Z","shell.execute_reply.started":"2023-11-06T00:15:07.475311Z","shell.execute_reply":"2023-11-06T00:15:07.479461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_train_images = np.empty_like(train_images)\naug_test_images = np.empty_like(test_images)\n\nfor i in range(train_images.shape[0]):\n    aug_train_images[i] = random_flip(train_images[i])\n    \nfor i in range(test_images.shape[0]):\n    aug_test_images[i] = random_flip(test_images[i])\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:15:09.078038Z","iopub.execute_input":"2023-11-06T00:15:09.078538Z","iopub.status.idle":"2023-11-06T00:15:09.770945Z","shell.execute_reply.started":"2023-11-06T00:15:09.078499Z","shell.execute_reply":"2023-11-06T00:15:09.769827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_image_and_label(data, labels, i):\n    print(\"label = \" + str(labels[i]))\n    plt.imshow(data[i], cmap = \"gray\")\n\nview_image_and_label(train_images, labels, 34)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:15:46.590013Z","iopub.execute_input":"2023-11-06T00:15:46.590795Z","iopub.status.idle":"2023-11-06T00:15:46.784669Z","shell.execute_reply.started":"2023-11-06T00:15:46.590762Z","shell.execute_reply":"2023-11-06T00:15:46.783741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CNN model build\n","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:15:58.138473Z","iopub.execute_input":"2023-11-06T00:15:58.138845Z","iopub.status.idle":"2023-11-06T00:15:58.143360Z","shell.execute_reply.started":"2023-11-06T00:15:58.138814Z","shell.execute_reply":"2023-11-06T00:15:58.142445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(aug_train_images, labels, test_size = 0.2, random_state = 42)\nX_train, X_test = X_train/255.0, X_test/255.0","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:16:18.998939Z","iopub.execute_input":"2023-11-06T00:16:18.999625Z","iopub.status.idle":"2023-11-06T00:16:19.195416Z","shell.execute_reply.started":"2023-11-06T00:16:18.999593Z","shell.execute_reply":"2023-11-06T00:16:19.194209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model definition","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (28,28,1)),\n    keras.layers.MaxPooling2D((2,2)),\n    keras.layers.Conv2D(64, (3,3), activation = 'relu'),\n    keras.layers.MaxPooling2D((2,2)),\n    keras.layers.Flatten(),\n    keras.layers.Dense(128,activation = 'relu'),\n    keras.layers.Dense(10, activation = 'softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:16:26.770273Z","iopub.execute_input":"2023-11-06T00:16:26.771232Z","iopub.status.idle":"2023-11-06T00:16:26.850400Z","shell.execute_reply.started":"2023-11-06T00:16:26.771195Z","shell.execute_reply":"2023-11-06T00:16:26.849501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',\n             loss = 'sparse_categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:16:29.849335Z","iopub.execute_input":"2023-11-06T00:16:29.849928Z","iopub.status.idle":"2023-11-06T00:16:29.862050Z","shell.execute_reply.started":"2023-11-06T00:16:29.849898Z","shell.execute_reply":"2023-11-06T00:16:29.861260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs = 30, validation_data = (X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:16:33.313450Z","iopub.execute_input":"2023-11-06T00:16:33.313831Z","iopub.status.idle":"2023-11-06T00:18:33.364796Z","shell.execute_reply.started":"2023-11-06T00:16:33.313801Z","shell.execute_reply":"2023-11-06T00:18:33.363741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test, y_test)\nprint(f\"Test accuracy: {test_accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T23:27:41.547448Z","iopub.execute_input":"2023-11-04T23:27:41.547817Z","iopub.status.idle":"2023-11-04T23:27:44.174344Z","shell.execute_reply.started":"2023-11-04T23:27:41.547789Z","shell.execute_reply":"2023-11-04T23:27:44.173517Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T01:40:22.928846Z","iopub.execute_input":"2023-11-06T01:40:22.929190Z","iopub.status.idle":"2023-11-06T01:40:22.953548Z","shell.execute_reply.started":"2023-11-06T01:40:22.929166Z","shell.execute_reply":"2023-11-06T01:40:22.952264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:18:52.873207Z","iopub.execute_input":"2023-11-06T00:18:52.874014Z","iopub.status.idle":"2023-11-06T00:18:52.879814Z","shell.execute_reply.started":"2023-11-06T00:18:52.873978Z","shell.execute_reply":"2023-11-06T00:18:52.878902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_test_image_and_result(data, predictions, i):\n    print(np.argmax(predictions[i]))\n    plt.imshow(data[i], cmap = \"gray\")\n\nview_test_image_and_result(test_images, predictions, 3)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:18:55.509716Z","iopub.execute_input":"2023-11-06T00:18:55.510359Z","iopub.status.idle":"2023-11-06T00:18:55.719799Z","shell.execute_reply.started":"2023-11-06T00:18:55.510331Z","shell.execute_reply":"2023-11-06T00:18:55.718863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_list = []\n\nfor prediction in predictions:\n    prediction_list.append(np.argmax(prediction))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:07:32.434220Z","iopub.execute_input":"2023-11-06T00:07:32.434891Z","iopub.status.idle":"2023-11-06T00:07:32.553442Z","shell.execute_reply.started":"2023-11-06T00:07:32.434857Z","shell.execute_reply":"2023-11-06T00:07:32.552385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = len(prediction_list)\nsubmissions = pd.DataFrame({'ImageId':np.arange(1,n+1), 'Label':prediction_list})","metadata":{"execution":{"iopub.status.busy":"2023-11-06T00:08:10.285470Z","iopub.execute_input":"2023-11-06T00:08:10.285957Z","iopub.status.idle":"2023-11-06T00:08:10.417807Z","shell.execute_reply.started":"2023-11-06T00:08:10.285916Z","shell.execute_reply":"2023-11-06T00:08:10.416877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.status.busy":"2023-11-06T01:40:03.592421Z","iopub.execute_input":"2023-11-06T01:40:03.593186Z","iopub.status.idle":"2023-11-06T01:40:03.933626Z","shell.execute_reply.started":"2023-11-06T01:40:03.593146Z","shell.execute_reply":"2023-11-06T01:40:03.932174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}