{"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":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n!pip3 install livelossplot\nfrom livelossplot import PlotLossesKeras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-21T10:35:15.170102Z","iopub.execute_input":"2021-11-21T10:35:15.170423Z","iopub.status.idle":"2021-11-21T10:35:31.275641Z","shell.execute_reply.started":"2021-11-21T10:35:15.170342Z","shell.execute_reply":"2021-11-21T10:35:31.274588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:31.277952Z","iopub.execute_input":"2021-11-21T10:35:31.278248Z","iopub.status.idle":"2021-11-21T10:35:36.408931Z","shell.execute_reply.started":"2021-11-21T10:35:31.278209Z","shell.execute_reply":"2021-11-21T10:35:36.408189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of null values in training set:',train_data.isnull().sum().sum())\nprint('')\nprint('Number of null values in test set:',test_data.isnull().sum().sum())","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:36.410248Z","iopub.execute_input":"2021-11-21T10:35:36.410553Z","iopub.status.idle":"2021-11-21T10:35:36.485536Z","shell.execute_reply.started":"2021-11-21T10:35:36.410515Z","shell.execute_reply":"2021-11-21T10:35:36.484721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Figure size\nplt.figure(figsize=(6,6))\n\n# Subplot \nfor i in range(9):\n    img = np.asarray(train_data.iloc[i,1:].values.reshape((28,28))/255);\n    ax=plt.subplot(3, 3, i+1)\n    ax.grid(False)\n    plt.imshow(img, cmap='gray')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:36.486729Z","iopub.execute_input":"2021-11-21T10:35:36.487052Z","iopub.status.idle":"2021-11-21T10:35:37.803872Z","shell.execute_reply.started":"2021-11-21T10:35:36.487014Z","shell.execute_reply":"2021-11-21T10:35:37.80318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label', data=train_data)\nplt.title('Distribution of labels in training set')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:37.806238Z","iopub.execute_input":"2021-11-21T10:35:37.806849Z","iopub.status.idle":"2021-11-21T10:35:38.05782Z","shell.execute_reply.started":"2021-11-21T10:35:37.806805Z","shell.execute_reply":"2021-11-21T10:35:38.057112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_data.iloc[:,1:].values.reshape(-1, 28, 28, 1)\ny_train = train_data.iloc[:,:1].values.reshape(-1, 1)\n\nX_test = test_data.values.reshape(-1, 28, 28, 1)\ny_train = to_categorical(y_train)\nprint(f'Shape of X_train: {X_train.shape}')\nprint(f'Shape of y_train: {y_train.shape}')","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:38.058928Z","iopub.execute_input":"2021-11-21T10:35:38.060438Z","iopub.status.idle":"2021-11-21T10:35:38.068451Z","shell.execute_reply.started":"2021-11-21T10:35:38.060394Z","shell.execute_reply":"2021-11-21T10:35:38.067462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_classes = len(np.unique(y_train))\ninput_shape = (28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:38.07016Z","iopub.execute_input":"2021-11-21T10:35:38.070473Z","iopub.status.idle":"2021-11-21T10:35:38.087742Z","shell.execute_reply.started":"2021-11-21T10:35:38.070416Z","shell.execute_reply":"2021-11-21T10:35:38.087079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype(np.float32)/255\nX_test = X_test.astype(np.float32)/255","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:38.088897Z","iopub.execute_input":"2021-11-21T10:35:38.089224Z","iopub.status.idle":"2021-11-21T10:35:38.211531Z","shell.execute_reply.started":"2021-11-21T10:35:38.089186Z","shell.execute_reply":"2021-11-21T10:35:38.210753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential(\n    [\n        keras.Input(shape=input_shape),\n        layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Flatten(),\n        layers.Dropout(0.5),\n        layers.Dense(10, activation=\"softmax\"),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:38.212843Z","iopub.execute_input":"2021-11-21T10:35:38.213203Z","iopub.status.idle":"2021-11-21T10:35:40.50573Z","shell.execute_reply.started":"2021-11-21T10:35:38.213164Z","shell.execute_reply":"2021-11-21T10:35:40.504879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:40.507187Z","iopub.execute_input":"2021-11-21T10:35:40.507671Z","iopub.status.idle":"2021-11-21T10:35:40.519869Z","shell.execute_reply.started":"2021-11-21T10:35:40.507628Z","shell.execute_reply":"2021-11-21T10:35:40.518937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nepochs = 30\n\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n\nmodel.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1,\n          callbacks = [PlotLossesKeras()])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:35:40.52131Z","iopub.execute_input":"2021-11-21T10:35:40.521761Z","iopub.status.idle":"2021-11-21T10:37:03.262887Z","shell.execute_reply.started":"2021-11-21T10:35:40.52172Z","shell.execute_reply":"2021-11-21T10:37:03.262206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(X_test)\npreds = np.argmax(preds,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:37:03.26408Z","iopub.execute_input":"2021-11-21T10:37:03.264358Z","iopub.status.idle":"2021-11-21T10:37:04.823934Z","shell.execute_reply.started":"2021-11-21T10:37:03.264323Z","shell.execute_reply":"2021-11-21T10:37:04.823164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': test_data.index+1,\n                       'Label': preds})\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:37:04.828512Z","iopub.execute_input":"2021-11-21T10:37:04.830517Z","iopub.status.idle":"2021-11-21T10:37:04.837527Z","shell.execute_reply.started":"2021-11-21T10:37:04.830475Z","shell.execute_reply":"2021-11-21T10:37:04.836903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:37:04.843713Z","iopub.execute_input":"2021-11-21T10:37:04.845939Z","iopub.status.idle":"2021-11-21T10:37:04.929253Z","shell.execute_reply.started":"2021-11-21T10:37:04.8459Z","shell.execute_reply":"2021-11-21T10:37:04.928566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}