{"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)\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":{"execution":{"iopub.status.busy":"2021-12-10T19:57:58.924055Z","iopub.execute_input":"2021-12-10T19:57:58.924881Z","iopub.status.idle":"2021-12-10T19:57:58.936981Z","shell.execute_reply.started":"2021-12-10T19:57:58.92483Z","shell.execute_reply":"2021-12-10T19:57:58.935884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import accuracy_score\n\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:57:58.938909Z","iopub.execute_input":"2021-12-10T19:57:58.939681Z","iopub.status.idle":"2021-12-10T19:57:58.945541Z","shell.execute_reply.started":"2021-12-10T19:57:58.939638Z","shell.execute_reply":"2021-12-10T19:57:58.944673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_set = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:57:58.947163Z","iopub.execute_input":"2021-12-10T19:57:58.947773Z","iopub.status.idle":"2021-12-10T19:58:03.436078Z","shell.execute_reply.started":"2021-12-10T19:57:58.947727Z","shell.execute_reply":"2021-12-10T19:58:03.435228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:03.438016Z","iopub.execute_input":"2021-12-10T19:58:03.438237Z","iopub.status.idle":"2021-12-10T19:58:03.443322Z","shell.execute_reply.started":"2021-12-10T19:58:03.43821Z","shell.execute_reply":"2021-12-10T19:58:03.442536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:03.444949Z","iopub.execute_input":"2021-12-10T19:58:03.445686Z","iopub.status.idle":"2021-12-10T19:58:03.469865Z","shell.execute_reply.started":"2021-12-10T19:58:03.445639Z","shell.execute_reply":"2021-12-10T19:58:03.468987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.describe()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:03.471281Z","iopub.execute_input":"2021-12-10T19:58:03.472023Z","iopub.status.idle":"2021-12-10T19:58:05.510832Z","shell.execute_reply.started":"2021-12-10T19:58:03.471976Z","shell.execute_reply":"2021-12-10T19:58:05.509875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.info()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.512484Z","iopub.execute_input":"2021-12-10T19:58:05.512797Z","iopub.status.idle":"2021-12-10T19:58:05.562867Z","shell.execute_reply.started":"2021-12-10T19:58:05.512755Z","shell.execute_reply":"2021-12-10T19:58:05.561938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.56422Z","iopub.execute_input":"2021-12-10T19:58:05.564694Z","iopub.status.idle":"2021-12-10T19:58:05.615591Z","shell.execute_reply.started":"2021-12-10T19:58:05.564656Z","shell.execute_reply":"2021-12-10T19:58:05.614662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_category = train_set['label'].unique()\nprint(target_category)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.617193Z","iopub.execute_input":"2021-12-10T19:58:05.618424Z","iopub.status.idle":"2021-12-10T19:58:05.624562Z","shell.execute_reply.started":"2021-12-10T19:58:05.618376Z","shell.execute_reply":"2021-12-10T19:58:05.623656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = train_set['label']\ntrain_set = train_set.drop(columns='label')","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.62665Z","iopub.execute_input":"2021-12-10T19:58:05.627118Z","iopub.status.idle":"2021-12-10T19:58:05.713195Z","shell.execute_reply.started":"2021-12-10T19:58:05.627083Z","shell.execute_reply":"2021-12-10T19:58:05.712301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test, Y_train,Y_test = train_test_split(train_set,label, test_size=0.3, random_state=60 , shuffle=True)\nprint(len(X_train))\nprint(len(X_test))","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.714437Z","iopub.execute_input":"2021-12-10T19:58:05.714674Z","iopub.status.idle":"2021-12-10T19:58:05.983639Z","shell.execute_reply.started":"2021-12-10T19:58:05.714644Z","shell.execute_reply":"2021-12-10T19:58:05.98274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc =  RandomForestClassifier(n_estimators=100)\n\nrfc.fit(X_train, Y_train)\n\ntest_predict = rfc.predict(X_test)\n\ntrain_accuracy = round(rfc.score(X_train,Y_train)*100)\ntest_accuracy =round(accuracy_score(test_predict, Y_test)*100)\n\nprint(\"Random Forest Classifier Train Accuracy Score : {}% \".format(train_accuracy ))\nprint(\"Random Forest Classifier Test Accuracy Score  : {}% \".format(test_accuracy ))","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:05.985011Z","iopub.execute_input":"2021-12-10T19:58:05.985234Z","iopub.status.idle":"2021-12-10T19:58:28.080073Z","shell.execute_reply.started":"2021-12-10T19:58:05.985206Z","shell.execute_reply":"2021-12-10T19:58:28.079261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:28.081234Z","iopub.execute_input":"2021-12-10T19:58:28.081479Z","iopub.status.idle":"2021-12-10T19:58:28.098091Z","shell.execute_reply.started":"2021-12-10T19:58:28.081449Z","shell.execute_reply":"2021-12-10T19:58:28.097531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:28.099096Z","iopub.execute_input":"2021-12-10T19:58:28.099313Z","iopub.status.idle":"2021-12-10T19:58:28.110452Z","shell.execute_reply.started":"2021-12-10T19:58:28.099285Z","shell.execute_reply":"2021-12-10T19:58:28.109466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id = range(1,28001)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:28.112575Z","iopub.execute_input":"2021-12-10T19:58:28.11278Z","iopub.status.idle":"2021-12-10T19:58:28.119692Z","shell.execute_reply.started":"2021-12-10T19:58:28.112755Z","shell.execute_reply":"2021-12-10T19:58:28.118953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prdict = rfc.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:28.121817Z","iopub.execute_input":"2021-12-10T19:58:28.1222Z","iopub.status.idle":"2021-12-10T19:58:29.242976Z","shell.execute_reply.started":"2021-12-10T19:58:28.122145Z","shell.execute_reply":"2021-12-10T19:58:29.242324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(list(zip(id, y_prdict)),\n               columns =['ImageId', 'Label'])\nsubmission.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:29.2439Z","iopub.execute_input":"2021-12-10T19:58:29.244573Z","iopub.status.idle":"2021-12-10T19:58:29.2987Z","shell.execute_reply.started":"2021-12-10T19:58:29.244538Z","shell.execute_reply":"2021-12-10T19:58:29.298166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:58:29.299571Z","iopub.execute_input":"2021-12-10T19:58:29.300268Z","iopub.status.idle":"2021-12-10T19:58:29.358895Z","shell.execute_reply.started":"2021-12-10T19:58:29.300209Z","shell.execute_reply":"2021-12-10T19:58:29.35813Z"},"trusted":true},"execution_count":null,"outputs":[]}]}