{"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 matplotlib.pyplot as plt  # To get insights about images (to view them)\nplt.gray() # As images are in grayscale\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-07-15T17:54:59.915471Z","iopub.execute_input":"2021-07-15T17:54:59.915867Z","iopub.status.idle":"2021-07-15T17:54:59.929691Z","shell.execute_reply.started":"2021-07-15T17:54:59.915835Z","shell.execute_reply":"2021-07-15T17:54:59.929017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Read training data \ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:55:00.206395Z","iopub.execute_input":"2021-07-15T17:55:00.206749Z","iopub.status.idle":"2021-07-15T17:55:03.490421Z","shell.execute_reply.started":"2021-07-15T17:55:00.206719Z","shell.execute_reply":"2021-07-15T17:55:03.489295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Look at its description\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:55:03.491926Z","iopub.execute_input":"2021-07-15T17:55:03.492346Z","iopub.status.idle":"2021-07-15T17:55:03.513514Z","shell.execute_reply.started":"2021-07-15T17:55:03.492315Z","shell.execute_reply":"2021-07-15T17:55:03.512305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View Images along with labels by whose index are in lookAt array \nlookAt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 26, 68]\nImage = np.array(train.iloc[lookAt, 1:])\nimages = Image.reshape(len(lookAt), 28, 28)\nlabel = np.array(train.iloc[lookAt, 0])\n\ncol = 5\nfor index, image in enumerate(images):\n    if index%col == 0:\n        fig = plt.figure(figsize=(15,16))\n    fig.add_subplot(1,col,index%col+1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.title(label[index])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:55:03.515708Z","iopub.execute_input":"2021-07-15T17:55:03.516155Z","iopub.status.idle":"2021-07-15T17:55:04.168927Z","shell.execute_reply.started":"2021-07-15T17:55:03.51611Z","shell.execute_reply":"2021-07-15T17:55:04.167822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create training data \ntrain_Y = train[\"label\"].values\ntrain_X = train.drop('label', axis=1)\ntrain_X = np.array(train_X, dtype=int)\ntrain_X = train_X.reshape(train_X.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:55:04.170545Z","iopub.execute_input":"2021-07-15T17:55:04.170964Z","iopub.status.idle":"2021-07-15T17:55:04.392436Z","shell.execute_reply.started":"2021-07-15T17:55:04.170922Z","shell.execute_reply":"2021-07-15T17:55:04.39142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, image in enumerate(train_X[lookAt]):\n    if index%col == 0:\n        fig = plt.figure(figsize=(15,16))\n    fig.add_subplot(1,col,index%col+1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.title(label[index])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:30:10.978831Z","iopub.execute_input":"2021-07-15T17:30:10.979134Z","iopub.status.idle":"2021-07-15T17:30:11.843803Z","shell.execute_reply.started":"2021-07-15T17:30:10.979107Z","shell.execute_reply":"2021-07-15T17:30:11.842737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the libraries for implementing CNN model \nimport tensorflow as tf \nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D, Flatten, Dense\nimport keras","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:30:11.846886Z","iopub.execute_input":"2021-07-15T17:30:11.847316Z","iopub.status.idle":"2021-07-15T17:30:11.851901Z","shell.execute_reply.started":"2021-07-15T17:30:11.847274Z","shell.execute_reply":"2021-07-15T17:30:11.850867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create model \nmodel = keras.Sequential([\n    Conv2D(input_shape=(28,28,1), filters=8, strides=1, kernel_size=(5,5), activation = 'relu'),\n    MaxPooling2D(pool_size=(2,2), strides=2),\n    Conv2D(filters=16, strides=1, kernel_size=(5,5), activation = 'relu'),\n    MaxPooling2D(pool_size=(2,2), strides=2),\n    Flatten(),\n    Dense(120, activation='relu'),\n    Dense(84, activation='relu'),\n    Dense(10, activation='relu')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:30:11.854429Z","iopub.execute_input":"2021-07-15T17:30:11.854818Z","iopub.status.idle":"2021-07-15T17:30:11.92821Z","shell.execute_reply.started":"2021-07-15T17:30:11.854787Z","shell.execute_reply":"2021-07-15T17:30:11.92718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', \n              loss=keras.losses.SparseCategoricalCrossentropy(from_logits='true'),\n              metrics=keras.metrics.SparseCategoricalAccuracy())","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:30:11.932284Z","iopub.execute_input":"2021-07-15T17:30:11.932618Z","iopub.status.idle":"2021-07-15T17:30:11.954999Z","shell.execute_reply.started":"2021-07-15T17:30:11.932588Z","shell.execute_reply":"2021-07-15T17:30:11.953835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_X, train_Y, epochs=23)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:30:11.95664Z","iopub.execute_input":"2021-07-15T17:30:11.957066Z","iopub.status.idle":"2021-07-15T17:34:32.771092Z","shell.execute_reply.started":"2021-07-15T17:30:11.957021Z","shell.execute_reply":"2021-07-15T17:34:32.770165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:01.836343Z","iopub.execute_input":"2021-07-15T18:05:01.836728Z","iopub.status.idle":"2021-07-15T18:05:03.622372Z","shell.execute_reply.started":"2021-07-15T18:05:01.836695Z","shell.execute_reply":"2021-07-15T18:05:03.62136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get test data  \ntest_X = np.array(test, dtype=int)\ntest_X = test_X.reshape(test_X.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:04.760578Z","iopub.execute_input":"2021-07-15T18:05:04.760914Z","iopub.status.idle":"2021-07-15T18:05:04.840361Z","shell.execute_reply.started":"2021-07-15T18:05:04.760885Z","shell.execute_reply":"2021-07-15T18:05:04.839377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verify data\nfor index, image in enumerate(test_X[lookAt]):\n    if index%col == 0:\n        fig = plt.figure(figsize=(15,16))\n    fig.add_subplot(1,col,index%col+1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:07.891511Z","iopub.execute_input":"2021-07-15T18:05:07.89188Z","iopub.status.idle":"2021-07-15T18:05:08.512596Z","shell.execute_reply.started":"2021-07-15T18:05:07.891847Z","shell.execute_reply":"2021-07-15T18:05:08.511773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use model to get prediction \npredictions = model.predict(test_X)\nprint(np.argmax(predictions[2]))","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:11.107777Z","iopub.execute_input":"2021-07-15T18:05:11.108199Z","iopub.status.idle":"2021-07-15T18:05:14.45082Z","shell.execute_reply.started":"2021-07-15T18:05:11.108162Z","shell.execute_reply":"2021-07-15T18:05:14.449788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create dataframe to store submission \ndf = []\nfor i in range(len(predictions)):\n    df.append([i+1, np.argmax(predictions[i])])\nsubmission = pd.DataFrame(df, columns=['ImageId', 'Label'])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:20.253173Z","iopub.execute_input":"2021-07-15T18:05:20.25365Z","iopub.status.idle":"2021-07-15T18:05:20.401768Z","shell.execute_reply.started":"2021-07-15T18:05:20.25362Z","shell.execute_reply":"2021-07-15T18:05:20.401078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:25.918854Z","iopub.execute_input":"2021-07-15T18:05:25.919364Z","iopub.status.idle":"2021-07-15T18:05:25.928894Z","shell.execute_reply.started":"2021-07-15T18:05:25.919331Z","shell.execute_reply":"2021-07-15T18:05:25.927874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save submission \nsubmission.to_csv('./submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:05:31.323661Z","iopub.execute_input":"2021-07-15T18:05:31.324042Z","iopub.status.idle":"2021-07-15T18:05:31.379531Z","shell.execute_reply.started":"2021-07-15T18:05:31.32401Z","shell.execute_reply":"2021-07-15T18:05:31.378565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save model \nkeras.models.save_model(model, './model')","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:06:14.644812Z","iopub.execute_input":"2021-07-15T18:06:14.645512Z","iopub.status.idle":"2021-07-15T18:06:16.080296Z","shell.execute_reply.started":"2021-07-15T18:06:14.645462Z","shell.execute_reply":"2021-07-15T18:06:16.079499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('./submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:49:59.279248Z","iopub.execute_input":"2021-07-15T17:49:59.279619Z","iopub.status.idle":"2021-07-15T17:49:59.297955Z","shell.execute_reply.started":"2021-07-15T17:49:59.279589Z","shell.execute_reply":"2021-07-15T17:49:59.296963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = keras.models.load_model('./model')\n# model.fit(train_X, train_Y, epochs=23)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T18:00:12.27197Z","iopub.execute_input":"2021-07-15T18:00:12.272798Z","iopub.status.idle":"2021-07-15T18:04:39.517995Z","shell.execute_reply.started":"2021-07-15T18:00:12.272752Z","shell.execute_reply":"2021-07-15T18:04:39.517023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}