{"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-11-13T15:41:28.926682Z","iopub.execute_input":"2021-11-13T15:41:28.927080Z","iopub.status.idle":"2021-11-13T15:41:28.954382Z","shell.execute_reply.started":"2021-11-13T15:41:28.926982Z","shell.execute_reply":"2021-11-13T15:41:28.953517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import all libraries\n\nimport pandas as pd \nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:40.989156Z","iopub.execute_input":"2021-11-13T17:00:40.989760Z","iopub.status.idle":"2021-11-13T17:00:45.924093Z","shell.execute_reply.started":"2021-11-13T17:00:40.989667Z","shell.execute_reply":"2021-11-13T17:00:45.923333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the data\n\n The train.csv file contains 42000 rows and 785 columns. Each row represents an image of a handwritten digit and a label with the value of this digit.\n \n The output variable is an integer from 0 to 9. This is a multiclass classification problem.\n\n","metadata":{}},{"cell_type":"code","source":"# read training data from CSV file \ntrain_df = pd.read_csv('../input/digit-recognizer/train.csv')\nprint(train_df.shape)\nprint(train_df.head())","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:47.063726Z","iopub.execute_input":"2021-11-13T17:00:47.064514Z","iopub.status.idle":"2021-11-13T17:00:50.091310Z","shell.execute_reply.started":"2021-11-13T17:00:47.064469Z","shell.execute_reply":"2021-11-13T17:00:50.090360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read the test df\n\ntest_df = pd.read_csv('../input/digit-recognizer/test.csv')\nprint(test_df.shape)\nprint(test_df.head())","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:50.093147Z","iopub.execute_input":"2021-11-13T17:00:50.093632Z","iopub.status.idle":"2021-11-13T17:00:51.781786Z","shell.execute_reply.started":"2021-11-13T17:00:50.093590Z","shell.execute_reply":"2021-11-13T17:00:51.780072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_images = (train_df.iloc[:,1:].values).astype('float32') #pixel values\ntraining_labels = train_df.iloc[:, 0].values.astype('int32') #labels\ntesting_images = test_df.values.astype('float32') #pixel values","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:51.782919Z","iopub.execute_input":"2021-11-13T17:00:51.783118Z","iopub.status.idle":"2021-11-13T17:00:51.910013Z","shell.execute_reply.started":"2021-11-13T17:00:51.783093Z","shell.execute_reply":"2021-11-13T17:00:51.908977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization\ntraining_images = training_images.reshape(42000, 28, 28, 1)\ntraining_images = training_images/255.0\ntesting_images = testing_images.reshape(28000, 28, 28, 1)\ntesting_images = testing_images/255.0","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:52.759085Z","iopub.execute_input":"2021-11-13T17:00:52.759408Z","iopub.status.idle":"2021-11-13T17:00:52.866915Z","shell.execute_reply.started":"2021-11-13T17:00:52.759368Z","shell.execute_reply":"2021-11-13T17:00:52.865998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])\nfrom keras.utils.np_utils import to_categorical\ntraining_labels = to_categorical(training_labels, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:00:56.373757Z","iopub.execute_input":"2021-11-13T17:00:56.374448Z","iopub.status.idle":"2021-11-13T17:00:57.097995Z","shell.execute_reply.started":"2021-11-13T17:00:56.374402Z","shell.execute_reply":"2021-11-13T17:00:57.097300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:01:09.030670Z","iopub.execute_input":"2021-11-13T17:01:09.031683Z","iopub.status.idle":"2021-11-13T17:01:09.044842Z","shell.execute_reply.started":"2021-11-13T17:01:09.031614Z","shell.execute_reply":"2021-11-13T17:01:09.043721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Modelling\n","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu',\n                          input_shape=(28,28,1)),\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    #tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:02:31.866804Z","iopub.execute_input":"2021-11-13T17:02:31.867076Z","iopub.status.idle":"2021-11-13T17:02:31.950706Z","shell.execute_reply.started":"2021-11-13T17:02:31.867048Z","shell.execute_reply":"2021-11-13T17:02:31.949718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_images, training_labels, epochs=50, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:02:39.023012Z","iopub.execute_input":"2021-11-13T17:02:39.023322Z","iopub.status.idle":"2021-11-13T17:22:15.118704Z","shell.execute_reply.started":"2021-11-13T17:02:39.023273Z","shell.execute_reply":"2021-11-13T17:22:15.117693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict results\nresults = model.predict(testing_images)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:22:29.569204Z","iopub.execute_input":"2021-11-13T17:22:29.569532Z","iopub.status.idle":"2021-11-13T17:22:35.609119Z","shell.execute_reply.started":"2021-11-13T17:22:29.569495Z","shell.execute_reply":"2021-11-13T17:22:35.608340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"Digit_recognizer_result.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T17:22:42.788964Z","iopub.execute_input":"2021-11-13T17:22:42.789225Z","iopub.status.idle":"2021-11-13T17:22:42.830798Z","shell.execute_reply.started":"2021-11-13T17:22:42.789198Z","shell.execute_reply":"2021-11-13T17:22:42.830139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}