{"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-06-06T06:18:21.556666Z","iopub.execute_input":"2021-06-06T06:18:21.557054Z","iopub.status.idle":"2021-06-06T06:18:21.569414Z","shell.execute_reply.started":"2021-06-06T06:18:21.55697Z","shell.execute_reply":"2021-06-06T06:18:21.568702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Problem Statement**\nIn mnist dataset contains thousands of handwritten digit images. Our goal is predicat the handwritten image to digit.\n","metadata":{}},{"cell_type":"code","source":"## Loading the necessary library\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport keras\nfrom keras import losses\nfrom keras import optimizers\nfrom keras.layers import Dropout, MaxPooling2D, Dense, BatchNormalization, Flatten, Conv2D\nfrom keras.models import Sequential\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:18:24.030986Z","iopub.execute_input":"2021-06-06T06:18:24.031534Z","iopub.status.idle":"2021-06-06T06:18:30.432381Z","shell.execute_reply.started":"2021-06-06T06:18:24.031485Z","shell.execute_reply":"2021-06-06T06:18:30.431455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reading the train dataset\ndf= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:00.872094Z","iopub.execute_input":"2021-06-06T06:19:00.872471Z","iopub.status.idle":"2021-06-06T06:19:06.878061Z","shell.execute_reply.started":"2021-06-06T06:19:00.872437Z","shell.execute_reply":"2021-06-06T06:19:06.877083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the shape of the data\nprint('Shape of the dataset {}'.format(df.shape))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:06.879387Z","iopub.execute_input":"2021-06-06T06:19:06.879693Z","iopub.status.idle":"2021-06-06T06:19:06.884505Z","shell.execute_reply.started":"2021-06-06T06:19:06.879665Z","shell.execute_reply":"2021-06-06T06:19:06.883575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the unique label\nprint('Unique labels of the dataset: {}'.format(df['label'].unique()))\nprint('Number of unique classes: {}'.format(len(df['label'].unique())))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:06.886158Z","iopub.execute_input":"2021-06-06T06:19:06.886469Z","iopub.status.idle":"2021-06-06T06:19:06.901264Z","shell.execute_reply.started":"2021-06-06T06:19:06.886429Z","shell.execute_reply":"2021-06-06T06:19:06.899988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# seperating the dependent and independent features\nx = df.drop('label', axis=1)\ny = df['label']\nx","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:06.903048Z","iopub.execute_input":"2021-06-06T06:19:06.903554Z","iopub.status.idle":"2021-06-06T06:19:07.035716Z","shell.execute_reply.started":"2021-06-06T06:19:06.903511Z","shell.execute_reply":"2021-06-06T06:19:07.034885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the label\ny","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:13.212893Z","iopub.execute_input":"2021-06-06T06:19:13.213256Z","iopub.status.idle":"2021-06-06T06:19:13.220661Z","shell.execute_reply.started":"2021-06-06T06:19:13.213224Z","shell.execute_reply":"2021-06-06T06:19:13.219552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# spliting the data into test and train\nx_train, x_val, y_train, y_val = train_test_split(x,y,test_size=0.2, random_state=0)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:13.442452Z","iopub.execute_input":"2021-06-06T06:19:13.442796Z","iopub.status.idle":"2021-06-06T06:19:13.716539Z","shell.execute_reply.started":"2021-06-06T06:19:13.442768Z","shell.execute_reply":"2021-06-06T06:19:13.715544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of the x_train set: {}'.format(x_train.shape))\nprint('Shape of the y_train set: {}'.format(y_train.shape))\nprint('Shape of the x_test set: {}'.format(x_val.shape))\nprint('Shape of the y_test set: {}'.format(y_val.shape))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:13.871822Z","iopub.execute_input":"2021-06-06T06:19:13.872155Z","iopub.status.idle":"2021-06-06T06:19:13.877618Z","shell.execute_reply.started":"2021-06-06T06:19:13.872127Z","shell.execute_reply":"2021-06-06T06:19:13.876974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshaping the data\nx_train = x_train.values.reshape(-1,28,28,1)\nx_val = x_val.values.reshape(-1,28,28,1)\nprint('Shape of the x_trian image: {}'.format(x_train.shape[1:]))\nprint('Shape of the x_test image: {}'.format(x_val.shape[1:]))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:16.219834Z","iopub.execute_input":"2021-06-06T06:19:16.220311Z","iopub.status.idle":"2021-06-06T06:19:16.226123Z","shell.execute_reply.started":"2021-06-06T06:19:16.22028Z","shell.execute_reply":"2021-06-06T06:19:16.22539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying the 10 handwritten digit\nfor i in range(5):\n    plt.imshow(x_train[i])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:16.580874Z","iopub.execute_input":"2021-06-06T06:19:16.581257Z","iopub.status.idle":"2021-06-06T06:19:17.385437Z","shell.execute_reply.started":"2021-06-06T06:19:16.581226Z","shell.execute_reply":"2021-06-06T06:19:17.384403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize the pixel\nx_train = x_train.astype('float32')/255.0\nx_val = x_val.astype('float32')/255.0","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:20.093716Z","iopub.execute_input":"2021-06-06T06:19:20.094115Z","iopub.status.idle":"2021-06-06T06:19:20.180953Z","shell.execute_reply.started":"2021-06-06T06:19:20.094069Z","shell.execute_reply":"2021-06-06T06:19:20.180211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_creation(shape_input):\n    model = Sequential([\n        \n        Dense(300,activation='relu',input_shape = shape_input),\n        Dropout(0.20),\n        Dense(100,activation='relu'),\n        Dropout(0.20),\n        Dense(64,activation='relu'),\n        Dropout(0.20),\n        Flatten(),\n        Dense(256,activation='relu'),\n        Dropout(0.20),\n        Dense(10,activation='softmax')\n    ])\n    sgd = optimizers.Adam(lr=1e-3)\n    model.compile(optimizer = sgd, loss=losses.sparse_categorical_crossentropy, metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:21.845887Z","iopub.execute_input":"2021-06-06T06:19:21.846459Z","iopub.status.idle":"2021-06-06T06:19:21.854566Z","shell.execute_reply.started":"2021-06-06T06:19:21.846416Z","shell.execute_reply":"2021-06-06T06:19:21.853278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# build a model\nshape_input = x_train.shape[1:]\nmodel = model_creation(shape_input)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:23.07048Z","iopub.execute_input":"2021-06-06T06:19:23.07083Z","iopub.status.idle":"2021-06-06T06:19:23.321969Z","shell.execute_reply.started":"2021-06-06T06:19:23.0708Z","shell.execute_reply":"2021-06-06T06:19:23.321027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print the model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:19:23.450815Z","iopub.execute_input":"2021-06-06T06:19:23.451172Z","iopub.status.idle":"2021-06-06T06:19:23.461969Z","shell.execute_reply.started":"2021-06-06T06:19:23.451143Z","shell.execute_reply":"2021-06-06T06:19:23.461002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fti the model\nhistory = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=10, batch_size=50, verbose=2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model Evaluation**","metadata":{}},{"cell_type":"code","source":"## plot function\ndef plot_model_evaluation(history,materics,title):\n    plt.plot(history.history[materics])\n    plt.plot(history.history['val_'+materics])\n    plt.title(title)\n    plt.legend(['train','val'])\n    plt.xlabel('Epochs')\n    plt.ylabel(materics)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:06:51.567715Z","iopub.execute_input":"2021-06-06T06:06:51.568121Z","iopub.status.idle":"2021-06-06T06:06:51.575742Z","shell.execute_reply.started":"2021-06-06T06:06:51.568088Z","shell.execute_reply":"2021-06-06T06:06:51.574213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ploting the accuracy\nplot_model_evaluation(history,'accuracy', 'Model Accuracy')","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:06:58.044837Z","iopub.execute_input":"2021-06-06T06:06:58.045238Z","iopub.status.idle":"2021-06-06T06:06:58.221388Z","shell.execute_reply.started":"2021-06-06T06:06:58.045206Z","shell.execute_reply":"2021-06-06T06:06:58.220303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ploting the model loss\nplot_model_evaluation(history, 'loss', 'Model Loss')","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:07:02.037231Z","iopub.execute_input":"2021-06-06T06:07:02.037677Z","iopub.status.idle":"2021-06-06T06:07:02.197893Z","shell.execute_reply.started":"2021-06-06T06:07:02.037639Z","shell.execute_reply":"2021-06-06T06:07:02.19652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test.copy()\nx_test = x_test.values.reshape(-1,28,28,1)\nx_test = x_test.astype('float32')/255.0","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:07:04.645739Z","iopub.execute_input":"2021-06-06T06:07:04.646103Z","iopub.status.idle":"2021-06-06T06:07:05.231546Z","shell.execute_reply.started":"2021-06-06T06:07:04.646073Z","shell.execute_reply":"2021-06-06T06:07:05.230166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" y_pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:07:08.457011Z","iopub.execute_input":"2021-06-06T06:07:08.457359Z","iopub.status.idle":"2021-06-06T06:07:54.591148Z","shell.execute_reply.started":"2021-06-06T06:07:08.45733Z","shell.execute_reply":"2021-06-06T06:07:54.589851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submition = pd.DataFrame({'ImageId':test.index.values + 1, 'Label':np.argmax(y_pred, axis=1)})","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:08:01.829924Z","iopub.execute_input":"2021-06-06T06:08:01.830303Z","iopub.status.idle":"2021-06-06T06:08:01.837712Z","shell.execute_reply.started":"2021-06-06T06:08:01.830273Z","shell.execute_reply":"2021-06-06T06:08:01.836557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submition.to_csv('digit.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T06:08:05.70559Z","iopub.execute_input":"2021-06-06T06:08:05.706018Z","iopub.status.idle":"2021-06-06T06:08:05.768922Z","shell.execute_reply.started":"2021-06-06T06:08:05.705983Z","shell.execute_reply":"2021-06-06T06:08:05.768066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}