{"cells":[{"metadata":{"_uuid":"239a91f6-8a8b-462f-9753-9316ec383c5d","_cell_guid":"064af5ff-6503-4674-9fc0-fff2bb5637b9","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8a19b436-aa85-494f-96a0-6f70dc60e18d","_cell_guid":"8e0b8846-7839-4002-a090-77b74913f60a","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d64739b-2688-446f-9d36-efe1f9ebc9f4","_cell_guid":"b22929b4-e616-401f-9de0-17d8ecf713a5","trusted":true},"cell_type":"code","source":"# reading the dataset using pandas\n\ntrainset = pd.read_csv('../input/digit-recognizer/train.csv')\ntestset = pd.read_csv('../input/digit-recognizer/test.csv')\ntrainset.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bab649ed-5ccd-4d53-a4cb-0f4fd93a6135","_cell_guid":"041656bf-fe4e-4c67-92bb-e5ba414889a2","trusted":true},"cell_type":"code","source":"#splitting the training dataset into label and input\n\nlabel = trainset.iloc[:,0]\ntrain_input = trainset.iloc[:,1:]\ntrain_input.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"297ff68d-a745-4d9e-85ac-15a5e971e8fd","_cell_guid":"8ad4bded-ac74-48d9-ba32-f3e0e67d75fc","trusted":true},"cell_type":"code","source":"#splitting the tranin dataset in trainset and validationset(ratio 80% train and 20% test)\n\nfrom sklearn.model_selection import train_test_split\n\nxtrain, xval, ytrain, yval = train_test_split(train_input, label, test_size = 0.2, random_state = 42)\n\nprint('shape of xtrain {}\\nshape of xval {}\\nshape of ytrain {}\\nshape of yval {}'.format(xtrain.shape, \n                                                                                          xval.shape,\n                                                                                          ytrain.shape,\n                                                                                          yval.shape))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6ccfec20-2be5-4bd3-a819-bfcbb1cf7716","_cell_guid":"45c831eb-82c6-4a7b-ad4b-9988dd30c5c3","trusted":true},"cell_type":"code","source":"# onehotencoding of label (label are from 0 to 9)\nytrain = tf.keras.utils.to_categorical(ytrain, 10)\nyval = tf.keras.utils.to_categorical(yval, 10)\n\nytrain.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a64e2d42-c211-453b-bcff-c944e5a32b92","_cell_guid":"a662ce4d-475b-4021-a266-a00f79ad3f75","trusted":true},"cell_type":"code","source":"# resizeing the image pixel from (0 to255) to (0 to 1) by dividing 255\n#because ml model work best with small numbers\n\nxtrain = xtrain/255.\nxval = xval/255.\ntestset = testset/255.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"450d2511-83b6-4add-b5eb-8d79a5c600c2","_cell_guid":"1a90ce2f-e1dc-4c50-a8a6-9b612f70012f","trusted":true},"cell_type":"code","source":"# building the keras model\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Dense(512, activation = 'relu', input_shape = (None,784)),\n    tf.keras.layers.Dense(256, activation = 'relu'),\n    tf.keras.layers.Dense(128, activation = 'relu'),\n    tf.keras.layers.Dense(64, activation = 'relu'),\n    tf.keras.layers.Dense(32, activation = 'relu'),\n    tf.keras.layers.Dropout(0.1),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n    \n])\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = tf.keras.optimizers.Adam(), metrics = ['acc'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c3b00656-9808-45c4-a621-0ffbfdb13980","_cell_guid":"0091201f-ebc4-4742-87e7-02abbb9e2f6a","trusted":true},"cell_type":"code","source":"#training the model \n\nepoch = 20\nhistory = model.fit(xtrain, ytrain, batch_size = 512, epochs = epoch, \n                    validation_data = (xval, yval))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a205c952-9af5-4e7a-9d19-2149e9457eb2","_cell_guid":"fffc9188-f1db-4040-acc1-3623881f2adb","trusted":true},"cell_type":"code","source":"# visualizing the result using matplotlib \n\n# ploting acc vs epoch graph\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nepoch_list = [i for i in range(epoch)]\nplt.plot(epoch_list, acc,'r-', label = 'acc')\nplt.plot(epoch_list,val_acc ,'b-', label = 'val_acc')\nplt.xlabel('epoch count')\nplt.ylabel('accuracy')\nplt.title('acc vs epoch')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"601add68-cb19-4cdf-80b0-768a433e38c5","_cell_guid":"5322075f-1ea9-46f7-9adf-833486e2ee52","trusted":true},"cell_type":"code","source":"# visualizing the result using matplotlib \n\n# ploting loss vs epoch graph\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepoch_list = [i for i in range(epoch)]\nplt.plot(epoch_list, loss,'r-', label = 'loss')\nplt.plot(epoch_list,val_loss ,'b-', label = 'val_loss')\nplt.xlabel('epoch count')\nplt.ylabel('loss')\nplt.title('loss vs epoch')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"480bfbf6-dc02-453a-8a7f-7ed992be8029","_cell_guid":"edac0efb-50c3-4d1b-8ee9-86118d6f113c","trusted":true},"cell_type":"code","source":"#making prediction\n\nprediction = model.predict(testset)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3cf941db-6b4a-49cd-9443-b17412e4cf4e","_cell_guid":"e1d55bc9-c732-44db-8cf3-528fb9050222","trusted":true},"cell_type":"code","source":"labels = []\nlabel_name = [0,1,2,3,4,5,6,7,8,9]\nfor i in range(28000):\n    label = label_name[np.argmax(prediction[i])]\n    labels.append(label)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bca02c57-fbff-4977-a233-daa963a44c76","_cell_guid":"8f00fb15-eb63-463c-97a4-fe6284d6e712","trusted":true},"cell_type":"code","source":"index = [i for i in range(1,28001)]\ndf = pd.DataFrame({'ImageId': index, 'Label': labels})\n\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2996a0de-3b68-4a55-b3fc-8aba5bc3bf75","_cell_guid":"d6a04f3e-cdcc-480a-8c7c-55fc7b6aa213","trusted":true},"cell_type":"code","source":"df.to_csv('/kaggle/working/answer1.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}