{"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-22T09:29:56.108334Z","iopub.execute_input":"2021-06-22T09:29:56.108722Z","iopub.status.idle":"2021-06-22T09:29:56.118363Z","shell.execute_reply.started":"2021-06-22T09:29:56.108686Z","shell.execute_reply":"2021-06-22T09:29:56.11717Z"},"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-22T09:29:56.946004Z","iopub.execute_input":"2021-06-22T09:29:56.946399Z","iopub.status.idle":"2021-06-22T09:29:56.950846Z","shell.execute_reply.started":"2021-06-22T09:29:56.946356Z","shell.execute_reply":"2021-06-22T09:29:56.949578Z"},"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-22T09:29:57.517833Z","iopub.execute_input":"2021-06-22T09:29:57.518217Z","iopub.status.idle":"2021-06-22T09:30:02.512595Z","shell.execute_reply.started":"2021-06-22T09:29:57.518185Z","shell.execute_reply":"2021-06-22T09:30:02.511435Z"},"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-22T09:30:05.702502Z","iopub.execute_input":"2021-06-22T09:30:05.702867Z","iopub.status.idle":"2021-06-22T09:30:05.808924Z","shell.execute_reply.started":"2021-06-22T09:30:05.702831Z","shell.execute_reply":"2021-06-22T09:30:05.807653Z"},"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-22T09:30:07.789901Z","iopub.execute_input":"2021-06-22T09:30:07.790255Z","iopub.status.idle":"2021-06-22T09:30:07.902966Z","shell.execute_reply.started":"2021-06-22T09:30:07.790224Z","shell.execute_reply":"2021-06-22T09:30:07.901986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(X_train))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T09:30:09.164207Z","iopub.execute_input":"2021-06-22T09:30:09.164716Z","iopub.status.idle":"2021-06-22T09:30:09.170128Z","shell.execute_reply.started":"2021-06-22T09:30:09.164683Z","shell.execute_reply":"2021-06-22T09:30:09.168973Z"},"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-22T09:30:11.52945Z","iopub.execute_input":"2021-06-22T09:30:11.529812Z","iopub.status.idle":"2021-06-22T09:30:11.537241Z","shell.execute_reply.started":"2021-06-22T09:30:11.529782Z","shell.execute_reply":"2021-06-22T09:30:11.5362Z"},"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-22T09:30:14.684026Z","iopub.execute_input":"2021-06-22T09:30:14.684454Z","iopub.status.idle":"2021-06-22T09:30:14.830342Z","shell.execute_reply.started":"2021-06-22T09:30:14.684418Z","shell.execute_reply":"2021-06-22T09:30:14.829585Z"},"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-22T09:30:17.350767Z","iopub.execute_input":"2021-06-22T09:30:17.351337Z","iopub.status.idle":"2021-06-22T09:30:17.698844Z","shell.execute_reply.started":"2021-06-22T09:30:17.3513Z","shell.execute_reply":"2021-06-22T09:30:17.697971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T09:30:18.285948Z","iopub.execute_input":"2021-06-22T09:30:18.286467Z","iopub.status.idle":"2021-06-22T09:30:18.292213Z","shell.execute_reply.started":"2021-06-22T09:30:18.286432Z","shell.execute_reply":"2021-06-22T09:30:18.291138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-06-22T09:39:01.785088Z","iopub.execute_input":"2021-06-22T09:39:01.785742Z","iopub.status.idle":"2021-06-22T09:39:01.789696Z","shell.execute_reply.started":"2021-06-22T09:39:01.785703Z","shell.execute_reply":"2021-06-22T09:39:01.788777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.layers.Dense(512, activation=tf.nn.relu))\nmodel.add(keras.layers.Dropout(0.3))\nmodel.add(keras.layers.Dense(256, activation=tf.nn.relu))\nmodel.add(keras.layers.Dropout(0.3))\nmodel.add(keras.layers.Dense(128, activation=tf.nn.relu))\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-22T10:08:01.761738Z","iopub.execute_input":"2021-06-22T10:08:01.762158Z","iopub.status.idle":"2021-06-22T10:08:01.788024Z","shell.execute_reply.started":"2021-06-22T10:08:01.762117Z","shell.execute_reply":"2021-06-22T10:08:01.786814Z"},"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-22T10:08:01.949989Z","iopub.execute_input":"2021-06-22T10:08:01.950397Z","iopub.status.idle":"2021-06-22T10:08:37.732128Z","shell.execute_reply.started":"2021-06-22T10:08:01.950356Z","shell.execute_reply":"2021-06-22T10:08:37.731057Z"},"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-22T10:08:44.17267Z","iopub.execute_input":"2021-06-22T10:08:44.17305Z","iopub.status.idle":"2021-06-22T10:08:44.331106Z","shell.execute_reply.started":"2021-06-22T10:08:44.173017Z","shell.execute_reply":"2021-06-22T10:08:44.330139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.argmax(pred, axis=1))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:08:47.630407Z","iopub.execute_input":"2021-06-22T10:08:47.630792Z","iopub.status.idle":"2021-06-22T10:08:47.638977Z","shell.execute_reply.started":"2021-06-22T10:08:47.630748Z","shell.execute_reply":"2021-06-22T10:08:47.63774Z"},"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-22T10:08:48.067923Z","iopub.execute_input":"2021-06-22T10:08:48.068324Z","iopub.status.idle":"2021-06-22T10:08:48.134419Z","shell.execute_reply.started":"2021-06-22T10:08:48.068287Z","shell.execute_reply":"2021-06-22T10:08:48.133199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}