{"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":"\n\nimport numpy as np \nimport pandas as pd \nimport seaborn as sns\n\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-23T05:27:25.109745Z","iopub.execute_input":"2021-08-23T05:27:25.110144Z","iopub.status.idle":"2021-08-23T05:27:25.119998Z","shell.execute_reply.started":"2021-08-23T05:27:25.110086Z","shell.execute_reply":"2021-08-23T05:27:25.118854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-23T05:27:25.139035Z","iopub.execute_input":"2021-08-23T05:27:25.139406Z","iopub.status.idle":"2021-08-23T05:27:25.143416Z","shell.execute_reply.started":"2021-08-23T05:27:25.139378Z","shell.execute_reply":"2021-08-23T05:27:25.142457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:25.168905Z","iopub.execute_input":"2021-08-23T05:27:25.169449Z","iopub.status.idle":"2021-08-23T05:27:29.944393Z","shell.execute_reply.started":"2021-08-23T05:27:25.169416Z","shell.execute_reply":"2021-08-23T05:27:29.943532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:29.946117Z","iopub.execute_input":"2021-08-23T05:27:29.946805Z","iopub.status.idle":"2021-08-23T05:27:29.969712Z","shell.execute_reply.started":"2021-08-23T05:27:29.946754Z","shell.execute_reply":"2021-08-23T05:27:29.968689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop('label', axis=1)\ny_train = df_train['label']\n\nX_test = df_test","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:29.971427Z","iopub.execute_input":"2021-08-23T05:27:29.971746Z","iopub.status.idle":"2021-08-23T05:27:30.087588Z","shell.execute_reply.started":"2021-08-23T05:27:29.971715Z","shell.execute_reply":"2021-08-23T05:27:30.086818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convert from Pandas DataFrame to Numpy Array to be able to perform reshape operations in the next step\nX_train = X_train.to_numpy()\ny_train = y_train.to_numpy()\n\nX_test = X_test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:30.088984Z","iopub.execute_input":"2021-08-23T05:27:30.089497Z","iopub.status.idle":"2021-08-23T05:27:30.093380Z","shell.execute_reply.started":"2021-08-23T05:27:30.089465Z","shell.execute_reply":"2021-08-23T05:27:30.092659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape((-1,28,28))\nX_test = X_test.reshape((-1,28,28))","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:30.094778Z","iopub.execute_input":"2021-08-23T05:27:30.095140Z","iopub.status.idle":"2021-08-23T05:27:30.106955Z","shell.execute_reply.started":"2021-08-23T05:27:30.095087Z","shell.execute_reply":"2021-08-23T05:27:30.105908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.countplot(x=y_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:30.108138Z","iopub.execute_input":"2021-08-23T05:27:30.108618Z","iopub.status.idle":"2021-08-23T05:27:30.311696Z","shell.execute_reply.started":"2021-08-23T05:27:30.108588Z","shell.execute_reply":"2021-08-23T05:27:30.310760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-23T05:27:30.312939Z","iopub.execute_input":"2021-08-23T05:27:30.313256Z","iopub.status.idle":"2021-08-23T05:27:31.680981Z","shell.execute_reply.started":"2021-08-23T05:27:30.313229Z","shell.execute_reply":"2021-08-23T05:27:31.679954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape((-1,28*28))\nX_test = X_test.reshape((-1,28*28))","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:27:31.683000Z","iopub.execute_input":"2021-08-23T05:27:31.683315Z","iopub.status.idle":"2021-08-23T05:27:31.688431Z","shell.execute_reply.started":"2021-08-23T05:27:31.683287Z","shell.execute_reply":"2021-08-23T05:27:31.687214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-23T05:27:31.689910Z","iopub.execute_input":"2021-08-23T05:27:31.690232Z","iopub.status.idle":"2021-08-23T05:28:28.047487Z","shell.execute_reply.started":"2021-08-23T05:27:31.690202Z","shell.execute_reply":"2021-08-23T05:28:28.046351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:28:28.049319Z","iopub.execute_input":"2021-08-23T05:28:28.050072Z","iopub.status.idle":"2021-08-23T05:28:28.288618Z","shell.execute_reply.started":"2021-08-23T05:28:28.050020Z","shell.execute_reply":"2021-08-23T05:28:28.287256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nfor i in range(40):  \n    plt.subplot(4, 10, i+1)\n    plt.imshow(X_test[i].reshape((28,28)),cmap=plt.cm.binary)\n    plt.title(\"predict=%d\" % pred[i],y=0.9)\n    plt.xticks([])\n    plt.yticks([])\nplt.subplots_adjust(wspace=0.3, hspace=-0.1)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:28:28.290478Z","iopub.execute_input":"2021-08-23T05:28:28.291257Z","iopub.status.idle":"2021-08-23T05:28:30.467149Z","shell.execute_reply.started":"2021-08-23T05:28:28.291205Z","shell.execute_reply":"2021-08-23T05:28:30.466073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmit = pd.DataFrame()\nsubmit['ImageId'] = sample['ImageId']\nsubmit['label'] = pred\n\nsubmit.to_csv('Submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T05:28:30.468823Z","iopub.execute_input":"2021-08-23T05:28:30.469261Z","iopub.status.idle":"2021-08-23T05:28:30.545366Z","shell.execute_reply.started":"2021-08-23T05:28:30.469217Z","shell.execute_reply":"2021-08-23T05:28:30.544172Z"},"trusted":true},"execution_count":null,"outputs":[]}]}