{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import cross_val_score, train_test_split\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) 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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-03T22:24:20.015181Z","iopub.execute_input":"2023-04-03T22:24:20.015609Z","iopub.status.idle":"2023-04-03T22:24:20.026962Z","shell.execute_reply.started":"2023-04-03T22:24:20.015572Z","shell.execute_reply":"2023-04-03T22:24:20.025489Z"},"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\")","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:20.029616Z","iopub.execute_input":"2023-04-03T22:24:20.030573Z","iopub.status.idle":"2023-04-03T22:24:24.347045Z","shell.execute_reply.started":"2023-04-03T22:24:20.030517Z","shell.execute_reply":"2023-04-03T22:24:24.345469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:24.349090Z","iopub.execute_input":"2023-04-03T22:24:24.349871Z","iopub.status.idle":"2023-04-03T22:24:24.370568Z","shell.execute_reply.started":"2023-04-03T22:24:24.349810Z","shell.execute_reply":"2023-04-03T22:24:24.369090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_data.to_numpy()\nytrain = train[:, 0]\nxtrain = train[:, 1:]\nxtrain = xtrain / 255","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:24.374295Z","iopub.execute_input":"2023-04-03T22:24:24.374832Z","iopub.status.idle":"2023-04-03T22:24:24.550401Z","shell.execute_reply.started":"2023-04-03T22:24:24.374782Z","shell.execute_reply":"2023-04-03T22:24:24.548871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain, xval, ytrain, yval = train_test_split(\n    xtrain, ytrain, test_size=0.2, random_state=42,   \n)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:24.552160Z","iopub.execute_input":"2023-04-03T22:24:24.552576Z","iopub.status.idle":"2023-04-03T22:24:25.611061Z","shell.execute_reply.started":"2023-04-03T22:24:24.552525Z","shell.execute_reply":"2023-04-03T22:24:25.609781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel=RandomForestClassifier(random_state=0)\nmymodel.fit(xtrain,ytrain)\npred=mymodel.predict(xval)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:25.612569Z","iopub.execute_input":"2023-04-03T22:24:25.612902Z","iopub.status.idle":"2023-04-03T22:24:54.216416Z","shell.execute_reply.started":"2023-04-03T22:24:25.612870Z","shell.execute_reply":"2023-04-03T22:24:54.214777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" mymodel.score(xval,yval)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:54.218180Z","iopub.execute_input":"2023-04-03T22:24:54.218709Z","iopub.status.idle":"2023-04-03T22:24:54.656440Z","shell.execute_reply.started":"2023-04-03T22:24:54.218656Z","shell.execute_reply":"2023-04-03T22:24:54.654672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:54.658817Z","iopub.execute_input":"2023-04-03T22:24:54.659383Z","iopub.status.idle":"2023-04-03T22:24:54.687605Z","shell.execute_reply.started":"2023-04-03T22:24:54.659322Z","shell.execute_reply":"2023-04-03T22:24:54.686202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred=mymodel.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:54.689744Z","iopub.execute_input":"2023-04-03T22:24:54.691128Z","iopub.status.idle":"2023-04-03T22:24:56.002858Z","shell.execute_reply.started":"2023-04-03T22:24:54.691071Z","shell.execute_reply":"2023-04-03T22:24:56.001174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred\nypred = pd.Series(ypred,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:56.006232Z","iopub.execute_input":"2023-04-03T22:24:56.006682Z","iopub.status.idle":"2023-04-03T22:24:56.012955Z","shell.execute_reply.started":"2023-04-03T22:24:56.006635Z","shell.execute_reply":"2023-04-03T22:24:56.011568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),ypred],axis = 1)\n\nsubmission.to_csv(\"mysubmission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:56.014660Z","iopub.execute_input":"2023-04-03T22:24:56.015100Z","iopub.status.idle":"2023-04-03T22:24:56.061024Z","shell.execute_reply.started":"2023-04-03T22:24:56.015060Z","shell.execute_reply":"2023-04-03T22:24:56.059664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ytest = test_data[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:56.062758Z","iopub.execute_input":"2023-04-03T22:24:56.063164Z","iopub.status.idle":"2023-04-03T22:24:56.068875Z","shell.execute_reply.started":"2023-04-03T22:24:56.063112Z","shell.execute_reply":"2023-04-03T22:24:56.067297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# xtest = test_data[:, 1:]\n# xtest = xtest / 255","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:56.070930Z","iopub.execute_input":"2023-04-03T22:24:56.071737Z","iopub.status.idle":"2023-04-03T22:24:56.081790Z","shell.execute_reply.started":"2023-04-03T22:24:56.071575Z","shell.execute_reply":"2023-04-03T22:24:56.080483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"finalpred=mymodel.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T22:24:56.083563Z","iopub.execute_input":"2023-04-03T22:24:56.084202Z","iopub.status.idle":"2023-04-03T22:24:57.378260Z","shell.execute_reply.started":"2023-04-03T22:24:56.084158Z","shell.execute_reply":"2023-04-03T22:24:57.376777Z"},"trusted":true},"execution_count":null,"outputs":[]}]}