{"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":"# Importing libraries\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# Running this 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# The print function shows me the file path so I can load the files","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-19T18:31:20.088720Z","iopub.execute_input":"2022-01-19T18:31:20.089323Z","iopub.status.idle":"2022-01-19T18:31:20.101938Z","shell.execute_reply.started":"2022-01-19T18:31:20.089245Z","shell.execute_reply":"2022-01-19T18:31:20.100975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load data","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n# Head function shows me the first 5 rows\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:19:37.771804Z","iopub.execute_input":"2022-01-19T18:19:37.772719Z","iopub.status.idle":"2022-01-19T18:19:41.630213Z","shell.execute_reply.started":"2022-01-19T18:19:37.772666Z","shell.execute_reply":"2022-01-19T18:19:41.629343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:20:14.738491Z","iopub.execute_input":"2022-01-19T18:20:14.738795Z","iopub.status.idle":"2022-01-19T18:20:16.999010Z","shell.execute_reply.started":"2022-01-19T18:20:14.738761Z","shell.execute_reply":"2022-01-19T18:20:16.998041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyze data","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:20:24.449955Z","iopub.execute_input":"2022-01-19T18:20:24.450398Z","iopub.status.idle":"2022-01-19T18:20:24.508424Z","shell.execute_reply.started":"2022-01-19T18:20:24.450365Z","shell.execute_reply":"2022-01-19T18:20:24.507526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:21:52.495275Z","iopub.execute_input":"2022-01-19T18:21:52.495730Z","iopub.status.idle":"2022-01-19T18:21:52.651777Z","shell.execute_reply.started":"2022-01-19T18:21:52.495699Z","shell.execute_reply":"2022-01-19T18:21:52.650815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define x and y","metadata":{}},{"cell_type":"code","source":"ImageId = test_data.index + 1\n# I made ImageId = test_data.index + 1 because the range of the submission file has to be [1:28000] and the range of test_data is [0:27999]\ny=train_data.iloc[:,0]\n# iloc function allows me to select rows and columns\n# I want the label column (the first column) to be \"y\"\nX=train_data.iloc[::,1:]\n# I want the hole dataset except for the label to be \"x\"\nX_test=test_data\n# test_data doesn't have a label column so I don't need iloc","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:24:06.085140Z","iopub.execute_input":"2022-01-19T18:24:06.085446Z","iopub.status.idle":"2022-01-19T18:24:06.092085Z","shell.execute_reply.started":"2022-01-19T18:24:06.085416Z","shell.execute_reply":"2022-01-19T18:24:06.091080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:24:08.355337Z","iopub.execute_input":"2022-01-19T18:24:08.355645Z","iopub.status.idle":"2022-01-19T18:24:08.363383Z","shell.execute_reply.started":"2022-01-19T18:24:08.355604Z","shell.execute_reply":"2022-01-19T18:24:08.362354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:26:25.451193Z","iopub.execute_input":"2022-01-19T18:26:25.451952Z","iopub.status.idle":"2022-01-19T18:26:25.477121Z","shell.execute_reply.started":"2022-01-19T18:26:25.451908Z","shell.execute_reply":"2022-01-19T18:26:25.476212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:32:34.690211Z","iopub.execute_input":"2022-01-19T18:32:34.690517Z","iopub.status.idle":"2022-01-19T18:32:34.717879Z","shell.execute_reply.started":"2022-01-19T18:32:34.690487Z","shell.execute_reply":"2022-01-19T18:32:34.717004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Scaling data: pixels have values from 0 to 255, I have to scale them so they have a range from 0 to 1 for my model to work","metadata":{}},{"cell_type":"code","source":"X=X/255\nX_test=X_test/255","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:34:47.316706Z","iopub.execute_input":"2022-01-19T18:34:47.317035Z","iopub.status.idle":"2022-01-19T18:34:47.447331Z","shell.execute_reply.started":"2022-01-19T18:34:47.316998Z","shell.execute_reply":"2022-01-19T18:34:47.446611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split x for training and validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n# To validate my model before using it in the actual test dataset I need a validation dataset\n# train_test_split allows me to split my original training dataset into 2\nX_train, X_val, y_train, y_val = train_test_split(X,y,test_size=0.1,random_state=1)\n# I will use 10% of the original training dataset as my validation dataset\nX_train.shape,X_val.shape,y_train.shape,y_val.shape,X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:38:16.870118Z","iopub.execute_input":"2022-01-19T18:38:16.870482Z","iopub.status.idle":"2022-01-19T18:38:18.245772Z","shell.execute_reply.started":"2022-01-19T18:38:16.870442Z","shell.execute_reply":"2022-01-19T18:38:18.244876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define model","metadata":{}},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\n# Class MLPClassifier implements a multi-layer perceptron (MLP) algorithm that trains using Backpropagation.\n# The advantage of Multi-layer Perceptron is the capability to learn non-linear models.\nmodel = MLPClassifier(hidden_layer_sizes=(100,), max_iter=50, alpha=1e-5, solver='sgd', verbose=10, random_state=1, learning_rate='constant', learning_rate_init=0.1).fit(X_train, y_train)\n# I found that the model worked best using solver sgd, alpha (penalty parameter) of 1e-5 and an initial learning rate of 0.1 (the defaults are 'adam', 1e-4 and 0.001)\nprint(model.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-01-19T19:25:08.391639Z","iopub.execute_input":"2022-01-19T19:25:08.391974Z","iopub.status.idle":"2022-01-19T19:26:14.585244Z","shell.execute_reply.started":"2022-01-19T19:25:08.391935Z","shell.execute_reply":"2022-01-19T19:26:14.584392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on validation set","metadata":{}},{"cell_type":"code","source":"y_pred=model.predict(X_val)\nprint(model.score(X_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2022-01-19T19:09:31.962969Z","iopub.execute_input":"2022-01-19T19:09:31.963264Z","iopub.status.idle":"2022-01-19T19:09:32.128807Z","shell.execute_reply.started":"2022-01-19T19:09:31.963232Z","shell.execute_reply":"2022-01-19T19:09:32.125553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Comparing predicted values with actual values","metadata":{}},{"cell_type":"code","source":"df_val=pd.DataFrame({'actual_value':y_val, 'predicted_value':y_pred})\ndf_val.reset_index(drop=True, inplace=True)\ndf_val","metadata":{"execution":{"iopub.status.busy":"2022-01-19T19:09:37.098509Z","iopub.execute_input":"2022-01-19T19:09:37.099389Z","iopub.status.idle":"2022-01-19T19:09:37.113299Z","shell.execute_reply.started":"2022-01-19T19:09:37.099343Z","shell.execute_reply":"2022-01-19T19:09:37.112250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on test set","metadata":{}},{"cell_type":"code","source":"pred=model.predict(X_test)\npred","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:42:56.752172Z","iopub.execute_input":"2022-01-19T18:42:56.752485Z","iopub.status.idle":"2022-01-19T18:42:56.915494Z","shell.execute_reply.started":"2022-01-19T18:42:56.752423Z","shell.execute_reply":"2022-01-19T18:42:56.914650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading results into submission file","metadata":{}},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': ImageId, 'Label': pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-01-19T18:43:06.732696Z","iopub.execute_input":"2022-01-19T18:43:06.733430Z","iopub.status.idle":"2022-01-19T18:43:06.795719Z","shell.execute_reply.started":"2022-01-19T18:43:06.733381Z","shell.execute_reply":"2022-01-19T18:43:06.794797Z"},"trusted":true},"execution_count":null,"outputs":[]}]}