{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-28T01:34:21.356144Z","iopub.execute_input":"2021-06-28T01:34:21.356496Z","iopub.status.idle":"2021-06-28T01:34:21.367882Z","shell.execute_reply.started":"2021-06-28T01:34:21.356465Z","shell.execute_reply":"2021-06-28T01:34:21.366975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:34:21.701084Z","iopub.execute_input":"2021-06-28T01:34:21.701407Z","iopub.status.idle":"2021-06-28T01:34:22.472551Z","shell.execute_reply.started":"2021-06-28T01:34:21.701377Z","shell.execute_reply":"2021-06-28T01:34:22.471726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:34:22.473965Z","iopub.execute_input":"2021-06-28T01:34:22.474292Z","iopub.status.idle":"2021-06-28T01:34:27.150961Z","shell.execute_reply.started":"2021-06-28T01:34:22.474257Z","shell.execute_reply":"2021-06-28T01:34:27.150104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_data.drop('label', axis=1)\nX_train = X_train.to_numpy()\nX_train = X_train/255\n\ny_train = train_data['label']\ny_train = y_train.to_numpy()\n\n\nX_test = test_data\nX_test = X_test.to_numpy()\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:34:47.167557Z","iopub.execute_input":"2021-06-28T01:34:47.167878Z","iopub.status.idle":"2021-06-28T01:34:47.416107Z","shell.execute_reply.started":"2021-06-28T01:34:47.167850Z","shell.execute_reply":"2021-06-28T01:34:47.415120Z"},"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-06-28T01:34:48.846389Z","iopub.execute_input":"2021-06-28T01:34:48.846746Z","iopub.status.idle":"2021-06-28T01:34:48.852440Z","shell.execute_reply.started":"2021-06-28T01:34:48.846716Z","shell.execute_reply":"2021-06-28T01:34:48.851269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MLPClassifier(solver='adam', activation='relu', hidden_layer_sizes = (64,128), early_stopping=True, learning_rate = 'constant', verbose=True)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:34:50.976428Z","iopub.execute_input":"2021-06-28T01:34:50.976747Z","iopub.status.idle":"2021-06-28T01:35:18.880694Z","shell.execute_reply.started":"2021-06-28T01:34:50.976720Z","shell.execute_reply":"2021-06-28T01:35:18.879723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)\npred","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:38:44.428889Z","iopub.execute_input":"2021-06-28T01:38:44.429229Z","iopub.status.idle":"2021-06-28T01:38:44.747666Z","shell.execute_reply.started":"2021-06-28T01:38:44.429196Z","shell.execute_reply":"2021-06-28T01:38:44.746696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameter_space = {\n    'hidden_layer_sizes': [(64, 128), (64, 64)],\n    'activation': ['tanh', 'relu'],\n    'alpha': [0.0001, 0.005]\n}","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:40:32.746954Z","iopub.execute_input":"2021-06-28T01:40:32.747286Z","iopub.status.idle":"2021-06-28T01:40:32.751444Z","shell.execute_reply.started":"2021-06-28T01:40:32.747255Z","shell.execute_reply":"2021-06-28T01:40:32.750479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = GridSearchCV(model, parameter_space, cv=3)\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:40:36.666486Z","iopub.execute_input":"2021-06-28T01:40:36.666806Z","iopub.status.idle":"2021-06-28T01:51:59.227028Z","shell.execute_reply.started":"2021-06-28T01:40:36.666771Z","shell.execute_reply":"2021-06-28T01:51:59.226150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Best paramete set\nprint('Best parameters found:\\n', clf.best_params_)\n\n# All results\nmeans = clf.cv_results_['mean_test_score']\nstds = clf.cv_results_['std_test_score']\nfor mean, std, params in zip(means, stds, clf.cv_results_['params']):\n    print(\"%0.3f (+/-%0.03f) for %r\" % (mean, std * 2, params))","metadata":{"execution":{"iopub.status.busy":"2021-06-28T01:51:59.231533Z","iopub.execute_input":"2021-06-28T01:51:59.233824Z","iopub.status.idle":"2021-06-28T01:51:59.247447Z","shell.execute_reply.started":"2021-06-28T01:51:59.233779Z","shell.execute_reply":"2021-06-28T01:51:59.246181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true, pred_train = y_train , clf.predict(X_train)\n\nprint('Results on the test set:')\nprint(classification_report(y_true, pred_train))","metadata":{"execution":{"iopub.status.busy":"2021-06-28T02:18:55.635356Z","iopub.execute_input":"2021-06-28T02:18:55.635722Z","iopub.status.idle":"2021-06-28T02:18:56.116686Z","shell.execute_reply.started":"2021-06-28T02:18:55.635691Z","shell.execute_reply":"2021-06-28T02:18:56.115779Z"},"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-06-28T02:19:08.875870Z","iopub.execute_input":"2021-06-28T02:19:08.876230Z","iopub.status.idle":"2021-06-28T02:19:08.944468Z","shell.execute_reply.started":"2021-06-28T02:19:08.876195Z","shell.execute_reply":"2021-06-28T02:19:08.943619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}