{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n%matplotlib inline\n\n\nimport 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-11-27T23:11:06.350410Z","iopub.execute_input":"2021-11-27T23:11:06.350812Z","iopub.status.idle":"2021-11-27T23:11:06.368246Z","shell.execute_reply.started":"2021-11-27T23:11:06.350751Z","shell.execute_reply":"2021-11-27T23:11:06.367538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing Data\n\ndf_train = pd.read_csv('../input/digit-recognizer/train.csv', header=0)\ndf_test = pd.read_csv(\"../input/digit-recognizer/test.csv\", header=0)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:06.372999Z","iopub.execute_input":"2021-11-27T23:11:06.373309Z","iopub.status.idle":"2021-11-27T23:11:11.540754Z","shell.execute_reply.started":"2021-11-27T23:11:06.373282Z","shell.execute_reply":"2021-11-27T23:11:11.539748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:11.542114Z","iopub.execute_input":"2021-11-27T23:11:11.542362Z","iopub.status.idle":"2021-11-27T23:11:11.565503Z","shell.execute_reply.started":"2021-11-27T23:11:11.542332Z","shell.execute_reply":"2021-11-27T23:11:11.564751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating Features and Target Subsets\n\nX_train = df_train.drop(\"label\", axis=\"columns\")\n\ny_train = df_train[\"label\"].to_numpy()\ny_train = y_train.reshape(-1, 1)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:11.567707Z","iopub.execute_input":"2021-11-27T23:11:11.568310Z","iopub.status.idle":"2021-11-27T23:11:12.133335Z","shell.execute_reply.started":"2021-11-27T23:11:11.568275Z","shell.execute_reply":"2021-11-27T23:11:12.132633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.134721Z","iopub.execute_input":"2021-11-27T23:11:12.134968Z","iopub.status.idle":"2021-11-27T23:11:12.153267Z","shell.execute_reply.started":"2021-11-27T23:11:12.134938Z","shell.execute_reply":"2021-11-27T23:11:12.152641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to plot the numbers and their labels\n\ndef plot_number(X, y, index):\n    img = X.T[index].values.reshape(28, 28)\n    plt.imshow(img, cmap='Greys',  interpolation='nearest')\n    plt.title('true label: %d' % y[index])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.154447Z","iopub.execute_input":"2021-11-27T23:11:12.154706Z","iopub.status.idle":"2021-11-27T23:11:12.164525Z","shell.execute_reply.started":"2021-11-27T23:11:12.154676Z","shell.execute_reply":"2021-11-27T23:11:12.163707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_number(X_train, y_train, index=6)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.165796Z","iopub.execute_input":"2021-11-27T23:11:12.166415Z","iopub.status.idle":"2021-11-27T23:11:12.386489Z","shell.execute_reply.started":"2021-11-27T23:11:12.166385Z","shell.execute_reply":"2021-11-27T23:11:12.385598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.388287Z","iopub.execute_input":"2021-11-27T23:11:12.388566Z","iopub.status.idle":"2021-11-27T23:11:12.412482Z","shell.execute_reply.started":"2021-11-27T23:11:12.388523Z","shell.execute_reply":"2021-11-27T23:11:12.411618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.414360Z","iopub.execute_input":"2021-11-27T23:11:12.414742Z","iopub.status.idle":"2021-11-27T23:11:12.434831Z","shell.execute_reply.started":"2021-11-27T23:11:12.414697Z","shell.execute_reply":"2021-11-27T23:11:12.433816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Standardizing our Features\n\nfrom sklearn.preprocessing import StandardScaler\n\nsc_x = StandardScaler()\nX_train = sc_x.fit_transform(X_train)\nX_test = sc_x.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:12.436161Z","iopub.execute_input":"2021-11-27T23:11:12.436816Z","iopub.status.idle":"2021-11-27T23:11:13.775508Z","shell.execute_reply.started":"2021-11-27T23:11:12.436778Z","shell.execute_reply":"2021-11-27T23:11:13.774436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Implementing the model\n\nfrom sklearn.neural_network import MLPClassifier\n\nmodel = MLPClassifier().fit(X_train, y_train)\n\ny_pred_test = model.predict(X_test)\ny_pred_train = model.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:11:13.777141Z","iopub.execute_input":"2021-11-27T23:11:13.777646Z","iopub.status.idle":"2021-11-27T23:12:39.890598Z","shell.execute_reply.started":"2021-11-27T23:11:13.777597Z","shell.execute_reply":"2021-11-27T23:12:39.889242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import r2_score\n\nprint(f\"Train r2 score: {r2_score(y_train, y_pred_train)}\")","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:12:39.892429Z","iopub.execute_input":"2021-11-27T23:12:39.901190Z","iopub.status.idle":"2021-11-27T23:12:39.917531Z","shell.execute_reply.started":"2021-11-27T23:12:39.901092Z","shell.execute_reply":"2021-11-27T23:12:39.916400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to plot the numbers from Test subset and predicted labels for them\n\ndef plot_number_test(X, y, index):\n    img = X[index].reshape(28, 28)\n    plt.imshow(img, cmap='Greys',  interpolation='nearest')\n    plt.title('true label: %d' % y[index])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:12:39.926421Z","iopub.execute_input":"2021-11-27T23:12:39.930591Z","iopub.status.idle":"2021-11-27T23:12:39.943322Z","shell.execute_reply.started":"2021-11-27T23:12:39.930516Z","shell.execute_reply":"2021-11-27T23:12:39.942205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_number_test(X_test, y_pred_test, index=9)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:12:39.945550Z","iopub.execute_input":"2021-11-27T23:12:39.949548Z","iopub.status.idle":"2021-11-27T23:12:40.253030Z","shell.execute_reply.started":"2021-11-27T23:12:39.949475Z","shell.execute_reply":"2021-11-27T23:12:40.252163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submitting our results\n\ndf = pd.DataFrame({\n    'ImageId': range(1, len(X_test)+1),\n    'Label': y_pred_test\n})\ndf.to_csv('sample.csv', index=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:12:40.254615Z","iopub.execute_input":"2021-11-27T23:12:40.255341Z","iopub.status.idle":"2021-11-27T23:12:40.322981Z","shell.execute_reply.started":"2021-11-27T23:12:40.255292Z","shell.execute_reply":"2021-11-27T23:12:40.322133Z"},"trusted":true},"execution_count":null,"outputs":[]}]}