{"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\nimport seaborn as sns\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:28.767270Z","iopub.execute_input":"2022-09-14T15:40:28.767747Z","iopub.status.idle":"2022-09-14T15:40:36.838897Z","shell.execute_reply.started":"2022-09-14T15:40:28.767651Z","shell.execute_reply":"2022-09-14T15:40:36.837518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/digit-recognizer/train.csv'\ntest_path = '../input/digit-recognizer/test.csv'\n\ntrain = pd.read_csv(train_path)\ntest = pd.read_csv(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:36.840981Z","iopub.execute_input":"2022-09-14T15:40:36.841647Z","iopub.status.idle":"2022-09-14T15:40:42.465986Z","shell.execute_reply.started":"2022-09-14T15:40:36.841609Z","shell.execute_reply":"2022-09-14T15:40:42.464815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:42.467508Z","iopub.execute_input":"2022-09-14T15:40:42.468798Z","iopub.status.idle":"2022-09-14T15:40:42.505992Z","shell.execute_reply.started":"2022-09-14T15:40:42.468747Z","shell.execute_reply":"2022-09-14T15:40:42.504599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:42.508947Z","iopub.execute_input":"2022-09-14T15:40:42.509468Z","iopub.status.idle":"2022-09-14T15:40:42.576579Z","shell.execute_reply.started":"2022-09-14T15:40:42.509418Z","shell.execute_reply":"2022-09-14T15:40:42.575070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will now preproces de data and build the train and test to evaluate our models","metadata":{}},{"cell_type":"code","source":"x = (train.drop(columns=['label'],axis=1).values / 255)\ny = train['label'].values\n\nx_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.1)\n\nprint(x_train.shape,x_test.shape)\nprint(y_train.shape, y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:42.578154Z","iopub.execute_input":"2022-09-14T15:40:42.578605Z","iopub.status.idle":"2022-09-14T15:40:43.290405Z","shell.execute_reply.started":"2022-09-14T15:40:42.578561Z","shell.execute_reply":"2022-09-14T15:40:43.289148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will take a closer look at part of the data. We will see the image that needs to be processed and it's actual label.","metadata":{}},{"cell_type":"code","source":"imgs = x.reshape(-1,28,28)\nfig, ax = plt.subplots(2, 5, figsize=(15,7));\nax = ax.ravel();\nfor i, index in enumerate(imgs[:10]):\n    ax[i].set_title(f'Label: {y[i]}')\n    ax[i].imshow(imgs[i],cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:43.291958Z","iopub.execute_input":"2022-09-14T15:40:43.292351Z","iopub.status.idle":"2022-09-14T15:40:44.534985Z","shell.execute_reply.started":"2022-09-14T15:40:43.292316Z","shell.execute_reply":"2022-09-14T15:40:44.533392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data= train,x= \"label\")\nplt.title('Distribution of each digit')\nplt.xlabel('Label')","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:40:44.536460Z","iopub.execute_input":"2022-09-14T15:40:44.536846Z","iopub.status.idle":"2022-09-14T15:40:44.779568Z","shell.execute_reply.started":"2022-09-14T15:40:44.536813Z","shell.execute_reply":"2022-09-14T15:40:44.778259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Simple Multi-Layer Perception with sklearn","metadata":{}},{"cell_type":"code","source":"mlp = MLPClassifier()\nmlp.fit(x_train,y_train)\n\nres = mlp.predict(x_test)\nscore = mlp.score(x_test, y_test)\n\nprint(res.shape)\nprint(\"accuracy of MLP is: \", score)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:43:46.701599Z","iopub.execute_input":"2022-09-14T15:43:46.702128Z","iopub.status.idle":"2022-09-14T15:45:00.020758Z","shell.execute_reply.started":"2022-09-14T15:43:46.702064Z","shell.execute_reply":"2022-09-14T15:45:00.016885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now let's use the actual test data for the final sumission.","metadata":{}},{"cell_type":"code","source":"x_sumission_test = test.values / 255\n\nresults = mlp.predict(x_sumission_test)\n\nprint (x_sumission_test.shape,results.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:56:43.326287Z","iopub.execute_input":"2022-09-14T15:56:43.326773Z","iopub.status.idle":"2022-09-14T15:56:43.721242Z","shell.execute_reply.started":"2022-09-14T15:56:43.326735Z","shell.execute_reply":"2022-09-14T15:56:43.719574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': test.index + 1, 'Label': results.flatten()})\noutput.to_csv('submission.csv',index=False)\n\noutput","metadata":{"execution":{"iopub.status.busy":"2022-09-14T15:58:26.173159Z","iopub.execute_input":"2022-09-14T15:58:26.173938Z","iopub.status.idle":"2022-09-14T15:58:26.221220Z","shell.execute_reply.started":"2022-09-14T15:58:26.173898Z","shell.execute_reply":"2022-09-14T15:58:26.219775Z"},"trusted":true},"execution_count":null,"outputs":[]}]}