{"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":"markdown","source":"# **Importing Libraries**","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.neural_network import MLPClassifier","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:46:01.349371Z","iopub.execute_input":"2021-06-29T15:46:01.349856Z","iopub.status.idle":"2021-06-29T15:46:02.351182Z","shell.execute_reply.started":"2021-06-29T15:46:01.349753Z","shell.execute_reply":"2021-06-29T15:46:02.350289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Importing Data Set**","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:47:27.945717Z","iopub.execute_input":"2021-06-29T15:47:27.946101Z","iopub.status.idle":"2021-06-29T15:47:33.825188Z","shell.execute_reply.started":"2021-06-29T15:47:27.946067Z","shell.execute_reply":"2021-06-29T15:47:33.823954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:47:40.157773Z","iopub.execute_input":"2021-06-29T15:47:40.158282Z","iopub.status.idle":"2021-06-29T15:47:40.193954Z","shell.execute_reply.started":"2021-06-29T15:47:40.158250Z","shell.execute_reply":"2021-06-29T15:47:40.192982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:47:53.281304Z","iopub.execute_input":"2021-06-29T15:47:53.281663Z","iopub.status.idle":"2021-06-29T15:47:53.301106Z","shell.execute_reply.started":"2021-06-29T15:47:53.281634Z","shell.execute_reply":"2021-06-29T15:47:53.300065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Preprocessing The Data**","metadata":{}},{"cell_type":"code","source":"# Spliting the train dataset into label and the image\nX_train = df_train.drop('label', axis = 1)\nY_train = df_train['label']\n\nX_test = df_test","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:51:48.970671Z","iopub.execute_input":"2021-06-29T15:51:48.971107Z","iopub.status.idle":"2021-06-29T15:51:49.057083Z","shell.execute_reply.started":"2021-06-29T15:51:48.971069Z","shell.execute_reply":"2021-06-29T15:51:49.055969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting pandas dataframe to numpy array\nX_train = X_train.to_numpy()\nY_train = Y_train.to_numpy()\nX_test = X_test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:53:02.127945Z","iopub.execute_input":"2021-06-29T15:53:02.128371Z","iopub.status.idle":"2021-06-29T15:53:02.133204Z","shell.execute_reply.started":"2021-06-29T15:53:02.128324Z","shell.execute_reply":"2021-06-29T15:53:02.132421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reshaping the dataset to form an image\nX_train = X_train.reshape((-1,28,28))\nX_test = X_test.reshape((-1,28,28))","metadata":{"execution":{"iopub.status.busy":"2021-06-29T15:55:06.247325Z","iopub.execute_input":"2021-06-29T15:55:06.247772Z","iopub.status.idle":"2021-06-29T15:55:06.252407Z","shell.execute_reply.started":"2021-06-29T15:55:06.247738Z","shell.execute_reply":"2021-06-29T15:55:06.251526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualizing The Dataset**","metadata":{}},{"cell_type":"code","source":"for i in range(16):\n    plt.subplot(4,4,i+1)\n    plt.imshow(X_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:33:33.233892Z","iopub.execute_input":"2021-06-29T16:33:33.234393Z","iopub.status.idle":"2021-06-29T16:33:34.333299Z","shell.execute_reply.started":"2021-06-29T16:33:33.234342Z","shell.execute_reply":"2021-06-29T16:33:34.332583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshaping the dataset for the model\nX_train = X_train.reshape((-1,28*28))\nX_test = X_test.reshape((-1,28*28))","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:34:19.048985Z","iopub.execute_input":"2021-06-29T16:34:19.049356Z","iopub.status.idle":"2021-06-29T16:34:19.054267Z","shell.execute_reply.started":"2021-06-29T16:34:19.049321Z","shell.execute_reply":"2021-06-29T16:34:19.053146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Building The MLPClassifier Model**","metadata":{}},{"cell_type":"code","source":"model = MLPClassifier(solver='adam', activation='relu', hidden_layer_sizes = (64,64), early_stopping=True, verbose=True)\nmodel.fit(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:35:12.747447Z","iopub.execute_input":"2021-06-29T16:35:12.747916Z","iopub.status.idle":"2021-06-29T16:36:14.112693Z","shell.execute_reply.started":"2021-06-29T16:35:12.747887Z","shell.execute_reply":"2021-06-29T16:36:14.111780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Using The Model To Predict The Test Dataset**","metadata":{}},{"cell_type":"code","source":"pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:38:44.045893Z","iopub.execute_input":"2021-06-29T16:38:44.046305Z","iopub.status.idle":"2021-06-29T16:38:44.282744Z","shell.execute_reply.started":"2021-06-29T16:38:44.046273Z","shell.execute_reply":"2021-06-29T16:38:44.281727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"sample = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\n\nsubmit = pd.DataFrame()\nsubmit['ImageId'] = sample['ImageId']\nsubmit['Label'] = pred\n\nsubmit.to_csv('Submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:42:54.357540Z","iopub.execute_input":"2021-06-29T16:42:54.357786Z","iopub.status.idle":"2021-06-29T16:42:54.430228Z","shell.execute_reply.started":"2021-06-29T16:42:54.357761Z","shell.execute_reply":"2021-06-29T16:42:54.429463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **End of Code**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}