{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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 matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils import to_categorical","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-11T17:35:41.919830Z","iopub.execute_input":"2024-08-11T17:35:41.920768Z","iopub.status.idle":"2024-08-11T17:35:41.928905Z","shell.execute_reply.started":"2024-08-11T17:35:41.920733Z","shell.execute_reply":"2024-08-11T17:35:41.927847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:47.594475Z","iopub.execute_input":"2024-08-11T17:35:47.594796Z","iopub.status.idle":"2024-08-11T17:35:47.602449Z","shell.execute_reply.started":"2024-08-11T17:35:47.594759Z","shell.execute_reply":"2024-08-11T17:35:47.601573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:47.605500Z","iopub.execute_input":"2024-08-11T17:35:47.605794Z","iopub.status.idle":"2024-08-11T17:35:47.613534Z","shell.execute_reply.started":"2024-08-11T17:35:47.605766Z","shell.execute_reply":"2024-08-11T17:35:47.612681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:47.614836Z","iopub.execute_input":"2024-08-11T17:35:47.615158Z","iopub.status.idle":"2024-08-11T17:35:51.793742Z","shell.execute_reply.started":"2024-08-11T17:35:47.615115Z","shell.execute_reply":"2024-08-11T17:35:51.792832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"label\"]\n\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) \n\n\nX_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:51.794758Z","iopub.execute_input":"2024-08-11T17:35:51.795058Z","iopub.status.idle":"2024-08-11T17:35:51.888435Z","shell.execute_reply.started":"2024-08-11T17:35:51.795035Z","shell.execute_reply":"2024-08-11T17:35:51.887456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest = test / 255.0\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:51.889849Z","iopub.execute_input":"2024-08-11T17:35:51.890294Z","iopub.status.idle":"2024-08-11T17:35:51.984258Z","shell.execute_reply.started":"2024-08-11T17:35:51.890261Z","shell.execute_reply":"2024-08-11T17:35:51.983450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape image in 3 dimensions (height = 28px, width = 28px , channel = 1)\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:51.985493Z","iopub.execute_input":"2024-08-11T17:35:51.985937Z","iopub.status.idle":"2024-08-11T17:35:51.991438Z","shell.execute_reply.started":"2024-08-11T17:35:51.985886Z","shell.execute_reply":"2024-08-11T17:35:51.990421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = to_categorical(Y_train, num_classes = 10)\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:51.992836Z","iopub.execute_input":"2024-08-11T17:35:51.993707Z","iopub.status.idle":"2024-08-11T17:35:52.407543Z","shell.execute_reply.started":"2024-08-11T17:35:51.993675Z","shell.execute_reply":"2024-08-11T17:35:52.406678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:52.408549Z","iopub.execute_input":"2024-08-11T17:35:52.408825Z","iopub.status.idle":"2024-08-11T17:35:52.414335Z","shell.execute_reply.started":"2024-08-11T17:35:52.408801Z","shell.execute_reply":"2024-08-11T17:35:52.413489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:52.415575Z","iopub.execute_input":"2024-08-11T17:35:52.415873Z","iopub.status.idle":"2024-08-11T17:35:52.682351Z","shell.execute_reply.started":"2024-08-11T17:35:52.415851Z","shell.execute_reply":"2024-08-11T17:35:52.681378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\n\nmodel = tf.keras.models.Sequential()\n\n\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\n\nmodel.compile(optimizer = 'adam',\n             loss = 'categorical_crossentropy',\n             metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:52.683541Z","iopub.execute_input":"2024-08-11T17:35:52.683839Z","iopub.status.idle":"2024-08-11T17:35:53.465107Z","shell.execute_reply.started":"2024-08-11T17:35:52.683814Z","shell.execute_reply":"2024-08-11T17:35:53.464308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, batch_size = 16, epochs = 10, \n          validation_data = (X_val, Y_val), verbose = 2)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:35:53.466298Z","iopub.execute_input":"2024-08-11T17:35:53.466725Z","iopub.status.idle":"2024-08-11T17:37:15.582611Z","shell.execute_reply.started":"2024-08-11T17:35:53.466694Z","shell.execute_reply":"2024-08-11T17:37:15.581689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T17:37:23.668367Z","iopub.execute_input":"2024-08-11T17:37:23.668730Z","iopub.status.idle":"2024-08-11T17:37:26.404709Z","shell.execute_reply.started":"2024-08-11T17:37:23.668700Z","shell.execute_reply":"2024-08-11T17:37:26.403698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}