{"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":"2022-02-22T11:28:50.785464Z","iopub.execute_input":"2022-02-22T11:28:50.785873Z","iopub.status.idle":"2022-02-22T11:28:50.816334Z","shell.execute_reply.started":"2022-02-22T11:28:50.785708Z","shell.execute_reply":"2022-02-22T11:28:50.815288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.models import Sequential\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.utils.np_utils import to_categorical\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:50.819163Z","iopub.execute_input":"2022-02-22T11:28:50.819738Z","iopub.status.idle":"2022-02-22T11:28:56.309304Z","shell.execute_reply.started":"2022-02-22T11:28:50.819692Z","shell.execute_reply":"2022-02-22T11:28:56.308515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:56.310798Z","iopub.execute_input":"2022-02-22T11:28:56.31104Z","iopub.status.idle":"2022-02-22T11:28:59.458776Z","shell.execute_reply.started":"2022-02-22T11:28:56.311006Z","shell.execute_reply":"2022-02-22T11:28:59.458091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.460027Z","iopub.execute_input":"2022-02-22T11:28:59.460374Z","iopub.status.idle":"2022-02-22T11:28:59.46685Z","shell.execute_reply.started":"2022-02-22T11:28:59.460333Z","shell.execute_reply":"2022-02-22T11:28:59.46596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12,6))\nsns.countplot(train_df['label'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.469506Z","iopub.execute_input":"2022-02-22T11:28:59.469804Z","iopub.status.idle":"2022-02-22T11:28:59.692489Z","shell.execute_reply.started":"2022-02-22T11:28:59.469765Z","shell.execute_reply":"2022-02-22T11:28:59.691845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_df.drop(labels = ['label'], axis = 1)\ny_train = train_df['label']","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.693854Z","iopub.execute_input":"2022-02-22T11:28:59.694109Z","iopub.status.idle":"2022-02-22T11:28:59.776573Z","shell.execute_reply.started":"2022-02-22T11:28:59.694073Z","shell.execute_reply":"2022-02-22T11:28:59.775742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28,28,1)\nx_train = x_train/255.0","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.778087Z","iopub.execute_input":"2022-02-22T11:28:59.778439Z","iopub.status.idle":"2022-02-22T11:28:59.887456Z","shell.execute_reply.started":"2022-02-22T11:28:59.778392Z","shell.execute_reply":"2022-02-22T11:28:59.886714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.888821Z","iopub.execute_input":"2022-02-22T11:28:59.889082Z","iopub.status.idle":"2022-02-22T11:28:59.895731Z","shell.execute_reply.started":"2022-02-22T11:28:59.889031Z","shell.execute_reply":"2022-02-22T11:28:59.894987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[2][:, : , 0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:28:59.897Z","iopub.execute_input":"2022-02-22T11:28:59.897427Z","iopub.status.idle":"2022-02-22T11:29:00.071721Z","shell.execute_reply.started":"2022-02-22T11:28:59.897387Z","shell.execute_reply":"2022-02-22T11:29:00.070988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[2]","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:29:00.073142Z","iopub.execute_input":"2022-02-22T11:29:00.073393Z","iopub.status.idle":"2022-02-22T11:29:00.079739Z","shell.execute_reply.started":"2022-02-22T11:29:00.073359Z","shell.execute_reply":"2022-02-22T11:29:00.078735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5), padding = 'Same', activation = 'relu', input_shape = (28,28,1)), \n         )\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size = (2,2)))\nmodel.add(Dropout(0.5))\n\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size =( 2,2)))\nmodel.add(Dropout(0.5))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(256, activation = 'relu'))\n\nmodel.add(Dropout(0.5))\n\n\n\nmodel.add(Dense(10, activation = 'softmax'))\n\n\noptimizer = RMSprop(lr = 0.01)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:29:00.081524Z","iopub.execute_input":"2022-02-22T11:29:00.08187Z","iopub.status.idle":"2022-02-22T11:29:02.586171Z","shell.execute_reply.started":"2022-02-22T11:29:00.081827Z","shell.execute_reply":"2022-02-22T11:29:02.585406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling the Model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:29:02.587445Z","iopub.execute_input":"2022-02-22T11:29:02.587846Z","iopub.status.idle":"2022-02-22T11:29:02.606505Z","shell.execute_reply.started":"2022-02-22T11:29:02.587809Z","shell.execute_reply":"2022-02-22T11:29:02.605729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nbatch_size = 64\nmodel.fit(x_train, y_train, epochs = epochs, batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T11:29:02.60792Z","iopub.execute_input":"2022-02-22T11:29:02.6082Z","iopub.status.idle":"2022-02-22T11:30:25.441267Z","shell.execute_reply.started":"2022-02-22T11:29:02.608164Z","shell.execute_reply":"2022-02-22T11:30:25.440567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}