{"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":"2021-06-22T11:51:07.960651Z","iopub.execute_input":"2021-06-22T11:51:07.96101Z","iopub.status.idle":"2021-06-22T11:51:07.971357Z","shell.execute_reply.started":"2021-06-22T11:51:07.96098Z","shell.execute_reply":"2021-06-22T11:51:07.970193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:51:08.530076Z","iopub.execute_input":"2021-06-22T11:51:08.530437Z","iopub.status.idle":"2021-06-22T11:51:08.535042Z","shell.execute_reply.started":"2021-06-22T11:51:08.530407Z","shell.execute_reply":"2021-06-22T11:51:08.533658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = pd.read_csv('../input/digit-recognizer/train.csv')\nX_test = pd.read_csv('../input/digit-recognizer/test.csv')\nprint(X_train.head())","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:52:07.167006Z","iopub.execute_input":"2021-06-22T11:52:07.167467Z","iopub.status.idle":"2021-06-22T11:52:11.954262Z","shell.execute_reply.started":"2021-06-22T11:52:07.167435Z","shell.execute_reply":"2021-06-22T11:52:11.953143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = X_train.pop('label')\nprint(type(y_train))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:52:15.838984Z","iopub.execute_input":"2021-06-22T11:52:15.839574Z","iopub.status.idle":"2021-06-22T11:52:15.926048Z","shell.execute_reply.started":"2021-06-22T11:52:15.839516Z","shell.execute_reply":"2021-06-22T11:52:15.925001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = (X_train/255).to_numpy()\nX_test = (X_test/255).to_numpy()\ny_train = y_train.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:52:17.565962Z","iopub.execute_input":"2021-06-22T11:52:17.566547Z","iopub.status.idle":"2021-06-22T11:52:17.679632Z","shell.execute_reply.started":"2021-06-22T11:52:17.566497Z","shell.execute_reply":"2021-06-22T11:52:17.678523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(X_train))\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:56:51.406068Z","iopub.execute_input":"2021-06-22T11:56:51.406479Z","iopub.status.idle":"2021-06-22T11:56:51.411166Z","shell.execute_reply.started":"2021-06-22T11:56:51.406446Z","shell.execute_reply":"2021-06-22T11:56:51.410364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(42000,28,28)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:55:46.373533Z","iopub.execute_input":"2021-06-22T11:55:46.373922Z","iopub.status.idle":"2021-06-22T11:55:46.37984Z","shell.execute_reply.started":"2021-06-22T11:55:46.373891Z","shell.execute_reply":"2021-06-22T11:55:46.378379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:56:58.143136Z","iopub.execute_input":"2021-06-22T11:56:58.143741Z","iopub.status.idle":"2021-06-22T11:56:58.147873Z","shell.execute_reply.started":"2021-06-22T11:56:58.143706Z","shell.execute_reply":"2021-06-22T11:56:58.146736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:55:59.70663Z","iopub.execute_input":"2021-06-22T11:55:59.707134Z","iopub.status.idle":"2021-06-22T11:55:59.711459Z","shell.execute_reply.started":"2021-06-22T11:55:59.707102Z","shell.execute_reply":"2021-06-22T11:55:59.710619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train[0].shape)\nX_trainExample = X_train[0].reshape(28,28)\nprint(X_trainExample.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:57:05.480825Z","iopub.execute_input":"2021-06-22T11:57:05.481368Z","iopub.status.idle":"2021-06-22T11:57:05.487072Z","shell.execute_reply.started":"2021-06-22T11:57:05.481332Z","shell.execute_reply":"2021-06-22T11:57:05.486242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow(X_trainExample,cmap=plt.cm.binary)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:57:08.373752Z","iopub.execute_input":"2021-06-22T11:57:08.37427Z","iopub.status.idle":"2021-06-22T11:57:08.543965Z","shell.execute_reply.started":"2021-06-22T11:57:08.374237Z","shell.execute_reply":"2021-06-22T11:57:08.543264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Normalizing**","metadata":{}},{"cell_type":"code","source":"X_train = keras.utils.normalize(X_train, axis=1)\nX_test = keras.utils.normalize(X_test, axis=1)\nprint(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:57:13.305165Z","iopub.execute_input":"2021-06-22T11:57:13.305699Z","iopub.status.idle":"2021-06-22T11:57:13.779402Z","shell.execute_reply.started":"2021-06-22T11:57:13.305665Z","shell.execute_reply":"2021-06-22T11:57:13.778247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:57:17.800487Z","iopub.execute_input":"2021-06-22T11:57:17.800832Z","iopub.status.idle":"2021-06-22T11:57:17.80551Z","shell.execute_reply.started":"2021-06-22T11:57:17.800801Z","shell.execute_reply":"2021-06-22T11:57:17.804656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:57:20.783013Z","iopub.execute_input":"2021-06-22T11:57:20.783376Z","iopub.status.idle":"2021-06-22T11:57:20.787937Z","shell.execute_reply.started":"2021-06-22T11:57:20.783346Z","shell.execute_reply":"2021-06-22T11:57:20.786818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extend dims of numpy arrays, so they can be used in model","metadata":{}},{"cell_type":"code","source":"X_train = np.expand_dims(X_train, -1)\nX_test = np.expand_dims(X_test, -1)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:05:00.62679Z","iopub.execute_input":"2021-06-22T12:05:00.627163Z","iopub.status.idle":"2021-06-22T12:05:00.631534Z","shell.execute_reply.started":"2021-06-22T12:05:00.627132Z","shell.execute_reply":"2021-06-22T12:05:00.630476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:05:01.606704Z","iopub.execute_input":"2021-06-22T12:05:01.607068Z","iopub.status.idle":"2021-06-22T12:05:01.611145Z","shell.execute_reply.started":"2021-06-22T12:05:01.607033Z","shell.execute_reply":"2021-06-22T12:05:01.61015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.layers.Conv2D(32, kernel_size=(3,3), input_shape=input_shape, activation=tf.nn.relu))\nmodel.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\nmodel.add(keras.layers.Conv2D(64, kernel_size=(3,3), activation=tf.nn.relu))\nmodel.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(10, activation=tf.nn.softmax))\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:05:02.467703Z","iopub.execute_input":"2021-06-22T12:05:02.46802Z","iopub.status.idle":"2021-06-22T12:05:02.518188Z","shell.execute_reply.started":"2021-06-22T12:05:02.467993Z","shell.execute_reply":"2021-06-22T12:05:02.517404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = keras.callbacks.EarlyStopping()\nmodel.fit(X_train, y_train, epochs=5, callbacks=[early_stopping])\npred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:05:03.7008Z","iopub.execute_input":"2021-06-22T12:05:03.701154Z","iopub.status.idle":"2021-06-22T12:06:41.885561Z","shell.execute_reply.started":"2021-06-22T12:05:03.701124Z","shell.execute_reply":"2021-06-22T12:06:41.884588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.argmax(pred[4]))\nX_test0 = X_test[4].reshape(28,28)\nplt.imshow(X_test0)\nplt.show()\nprint(pred.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:07:09.060062Z","iopub.execute_input":"2021-06-22T12:07:09.060405Z","iopub.status.idle":"2021-06-22T12:07:09.190611Z","shell.execute_reply.started":"2021-06-22T12:07:09.060377Z","shell.execute_reply":"2021-06-22T12:07:09.189762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.argmax(pred, axis=1))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:07:13.582817Z","iopub.execute_input":"2021-06-22T12:07:13.583189Z","iopub.status.idle":"2021-06-22T12:07:13.58911Z","shell.execute_reply.started":"2021-06-22T12:07:13.583159Z","shell.execute_reply":"2021-06-22T12:07:13.588199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label':np.argmax(pred, axis=1)})\nsubm.to_csv('submission.csv',index=False)\nprint(subm)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T12:07:16.044337Z","iopub.execute_input":"2021-06-22T12:07:16.044714Z","iopub.status.idle":"2021-06-22T12:07:16.106173Z","shell.execute_reply.started":"2021-06-22T12:07:16.044686Z","shell.execute_reply":"2021-06-22T12:07:16.10531Z"},"trusted":true},"execution_count":null,"outputs":[]}]}