{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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":"2022-12-01T18:41:33.126913Z","iopub.execute_input":"2022-12-01T18:41:33.128282Z","iopub.status.idle":"2022-12-01T18:41:33.146140Z","shell.execute_reply.started":"2022-12-01T18:41:33.128164Z","shell.execute_reply":"2022-12-01T18:41:33.144772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"read the data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsample = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:33.153497Z","iopub.execute_input":"2022-12-01T18:41:33.154294Z","iopub.status.idle":"2022-12-01T18:41:37.978306Z","shell.execute_reply.started":"2022-12-01T18:41:33.154252Z","shell.execute_reply":"2022-12-01T18:41:37.976973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:37.980213Z","iopub.execute_input":"2022-12-01T18:41:37.980787Z","iopub.status.idle":"2022-12-01T18:41:38.018848Z","shell.execute_reply.started":"2022-12-01T18:41:37.980745Z","shell.execute_reply":"2022-12-01T18:41:38.017314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.021937Z","iopub.execute_input":"2022-12-01T18:41:38.022784Z","iopub.status.idle":"2022-12-01T18:41:38.052812Z","shell.execute_reply.started":"2022-12-01T18:41:38.022744Z","shell.execute_reply":"2022-12-01T18:41:38.051461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.055677Z","iopub.execute_input":"2022-12-01T18:41:38.056260Z","iopub.status.idle":"2022-12-01T18:41:38.072073Z","shell.execute_reply.started":"2022-12-01T18:41:38.056209Z","shell.execute_reply":"2022-12-01T18:41:38.070818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.074068Z","iopub.execute_input":"2022-12-01T18:41:38.074492Z","iopub.status.idle":"2022-12-01T18:41:38.126630Z","shell.execute_reply.started":"2022-12-01T18:41:38.074455Z","shell.execute_reply":"2022-12-01T18:41:38.125506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.127878Z","iopub.execute_input":"2022-12-01T18:41:38.128841Z","iopub.status.idle":"2022-12-01T18:41:38.177716Z","shell.execute_reply.started":"2022-12-01T18:41:38.128801Z","shell.execute_reply":"2022-12-01T18:41:38.176352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageID = test.index\nimageID","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.179790Z","iopub.execute_input":"2022-12-01T18:41:38.181497Z","iopub.status.idle":"2022-12-01T18:41:38.191219Z","shell.execute_reply.started":"2022-12-01T18:41:38.181430Z","shell.execute_reply":"2022-12-01T18:41:38.189690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train.iloc[:,0]\ny","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.193220Z","iopub.execute_input":"2022-12-01T18:41:38.193787Z","iopub.status.idle":"2022-12-01T18:41:38.208219Z","shell.execute_reply.started":"2022-12-01T18:41:38.193727Z","shell.execute_reply":"2022-12-01T18:41:38.206654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.iloc[::,1:]\nx","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.209667Z","iopub.execute_input":"2022-12-01T18:41:38.210056Z","iopub.status.idle":"2022-12-01T18:41:38.247246Z","shell.execute_reply.started":"2022-12-01T18:41:38.210024Z","shell.execute_reply":"2022-12-01T18:41:38.245910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test\nx_test","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.248933Z","iopub.execute_input":"2022-12-01T18:41:38.249436Z","iopub.status.idle":"2022-12-01T18:41:38.277699Z","shell.execute_reply.started":"2022-12-01T18:41:38.249399Z","shell.execute_reply":"2022-12-01T18:41:38.276635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train , x_val , y_train , y_val = train_test_split(x,y,test_size=0.1,random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:38.279333Z","iopub.execute_input":"2022-12-01T18:41:38.279765Z","iopub.status.idle":"2022-12-01T18:41:39.190317Z","shell.execute_reply.started":"2022-12-01T18:41:38.279728Z","shell.execute_reply":"2022-12-01T18:41:39.188911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape , x_val.shape , y_train.shape , y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:39.194686Z","iopub.execute_input":"2022-12-01T18:41:39.195170Z","iopub.status.idle":"2022-12-01T18:41:39.202761Z","shell.execute_reply.started":"2022-12-01T18:41:39.195127Z","shell.execute_reply":"2022-12-01T18:41:39.201805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def relu(x):\n    return max(0.0,x)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:39.203946Z","iopub.execute_input":"2022-12-01T18:41:39.205132Z","iopub.status.idle":"2022-12-01T18:41:39.215149Z","shell.execute_reply.started":"2022-12-01T18:41:39.205078Z","shell.execute_reply":"2022-12-01T18:41:39.213915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid(x):\n    return 1.0 / (1.0 + exp(-x))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:39.217183Z","iopub.execute_input":"2022-12-01T18:41:39.218315Z","iopub.status.idle":"2022-12-01T18:41:39.229910Z","shell.execute_reply.started":"2022-12-01T18:41:39.218261Z","shell.execute_reply":"2022-12-01T18:41:39.228416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tanh(x):\n    return (exp(x) - exp(-x)) / (exp(x) + exp(-x))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:39.231742Z","iopub.execute_input":"2022-12-01T18:41:39.232330Z","iopub.status.idle":"2022-12-01T18:41:39.242574Z","shell.execute_reply.started":"2022-12-01T18:41:39.232280Z","shell.execute_reply":"2022-12-01T18:41:39.241348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\n\nmodel = MLPClassifier(\n    hidden_layer_sizes=(50,),\n    max_iter = 50,\n    alpha = 1e-4,\n    solver='sgd',\n    verbose=10,\n    random_state=1,\n    learning_rate_init=0.1,\n)\nmodel.fit(x_train,y_train)\nprint(model.score(x_train,y_train))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:39.246066Z","iopub.execute_input":"2022-12-01T18:41:39.247323Z","iopub.status.idle":"2022-12-01T18:41:58.875514Z","shell.execute_reply.started":"2022-12-01T18:41:39.247262Z","shell.execute_reply":"2022-12-01T18:41:58.874083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = model.predict(x_val)\nprint(model.score(x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:58.877348Z","iopub.execute_input":"2022-12-01T18:41:58.878446Z","iopub.status.idle":"2022-12-01T18:41:59.021932Z","shell.execute_reply.started":"2022-12-01T18:41:58.878386Z","shell.execute_reply":"2022-12-01T18:41:59.019865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x/255\nx_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:59.031289Z","iopub.execute_input":"2022-12-01T18:41:59.036697Z","iopub.status.idle":"2022-12-01T18:41:59.229002Z","shell.execute_reply.started":"2022-12-01T18:41:59.036573Z","shell.execute_reply":"2022-12-01T18:41:59.227830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_val , y_train , y_val = train_test_split(x,y,test_size=0.1,random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:59.230288Z","iopub.execute_input":"2022-12-01T18:41:59.230789Z","iopub.status.idle":"2022-12-01T18:41:59.492905Z","shell.execute_reply.started":"2022-12-01T18:41:59.230753Z","shell.execute_reply":"2022-12-01T18:41:59.490802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape , x_val.shape , y_train.shape , y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:59.495109Z","iopub.execute_input":"2022-12-01T18:41:59.495630Z","iopub.status.idle":"2022-12-01T18:41:59.505643Z","shell.execute_reply.started":"2022-12-01T18:41:59.495560Z","shell.execute_reply":"2022-12-01T18:41:59.504031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MLPClassifier(\n    hidden_layer_sizes=(50,),\n    max_iter = 50,\n    alpha = 1e-4,\n    solver='sgd',\n    verbose=10,\n    random_state=1,\n    learning_rate_init=0.1,\n)\nmodel.fit(x_train,y_train)\nprint(model.score(x_train,y_train))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:41:59.509772Z","iopub.execute_input":"2022-12-01T18:41:59.510219Z","iopub.status.idle":"2022-12-01T18:43:11.363830Z","shell.execute_reply.started":"2022-12-01T18:41:59.510186Z","shell.execute_reply":"2022-12-01T18:43:11.361839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = model.predict(x_val)\nprint(f\"Accuracy : {model.score(x_val,y_val)*100}%\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:43:11.366885Z","iopub.execute_input":"2022-12-01T18:43:11.368433Z","iopub.status.idle":"2022-12-01T18:43:11.485395Z","shell.execute_reply.started":"2022-12-01T18:43:11.368340Z","shell.execute_reply":"2022-12-01T18:43:11.483447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_values = pd.DataFrame({'actual val' : y_val , 'predicted val' : y_prediction})\ndf_values.reset_index(drop = True , inplace = True)\ndf_values","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:43:11.488694Z","iopub.execute_input":"2022-12-01T18:43:11.490225Z","iopub.status.idle":"2022-12-01T18:43:11.526896Z","shell.execute_reply.started":"2022-12-01T18:43:11.490147Z","shell.execute_reply":"2022-12-01T18:43:11.524137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(x_test)\ndf_values_pred = pd.DataFrame({'image id' : imageID , 'predicted val' : prediction})\ndf_values_pred\n","metadata":{"execution":{"iopub.status.busy":"2022-12-01T18:43:11.536191Z","iopub.execute_input":"2022-12-01T18:43:11.537885Z","iopub.status.idle":"2022-12-01T18:43:11.777136Z","shell.execute_reply.started":"2022-12-01T18:43:11.537783Z","shell.execute_reply":"2022-12-01T18:43:11.775390Z"},"trusted":true},"execution_count":null,"outputs":[]}]}