{"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 os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-26T08:34:45.651016Z","iopub.execute_input":"2023-05-26T08:34:45.651583Z","iopub.status.idle":"2023-05-26T08:34:45.691779Z","shell.execute_reply.started":"2023-05-26T08:34:45.651473Z","shell.execute_reply":"2023-05-26T08:34:45.689851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:48.518351Z","iopub.execute_input":"2023-05-26T08:34:48.519285Z","iopub.status.idle":"2023-05-26T08:34:55.627591Z","shell.execute_reply.started":"2023-05-26T08:34:48.519174Z","shell.execute_reply":"2023-05-26T08:34:55.626269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:55.629697Z","iopub.execute_input":"2023-05-26T08:34:55.630206Z","iopub.status.idle":"2023-05-26T08:34:55.664568Z","shell.execute_reply.started":"2023-05-26T08:34:55.630157Z","shell.execute_reply":"2023-05-26T08:34:55.662530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:55.666159Z","iopub.execute_input":"2023-05-26T08:34:55.666826Z","iopub.status.idle":"2023-05-26T08:34:55.692701Z","shell.execute_reply.started":"2023-05-26T08:34:55.666779Z","shell.execute_reply":"2023-05-26T08:34:55.691276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ref. https://www.kaggle.com/code/prashant111/random-forest-classifier-tutorial","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:55.699404Z","iopub.execute_input":"2023-05-26T08:34:55.700688Z","iopub.status.idle":"2023-05-26T08:34:55.707437Z","shell.execute_reply.started":"2023-05-26T08:34:55.700628Z","shell.execute_reply":"2023-05-26T08:34:55.706175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.iloc[:, 1:].values\ny = train_df[\"label\"].values","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:55.709235Z","iopub.execute_input":"2023-05-26T08:34:55.709810Z","iopub.status.idle":"2023-05-26T08:34:55.723692Z","shell.execute_reply.started":"2023-05-26T08:34:55.709756Z","shell.execute_reply":"2023-05-26T08:34:55.722586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split data into training and testing sets\nfrom sklearn.model_selection import train_test_split\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.33, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:49.195884Z","iopub.execute_input":"2023-05-26T08:37:49.196437Z","iopub.status.idle":"2023-05-26T08:37:49.668322Z","shell.execute_reply.started":"2023-05-26T08:37:49.196395Z","shell.execute_reply":"2023-05-26T08:37:49.666937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[0].reshape((28, 28)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:49.670958Z","iopub.execute_input":"2023-05-26T08:37:49.671687Z","iopub.status.idle":"2023-05-26T08:37:49.888436Z","shell.execute_reply.started":"2023-05-26T08:37:49.671639Z","shell.execute_reply":"2023-05-26T08:37:49.887053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_valid[0].reshape((28, 28)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:49.891059Z","iopub.execute_input":"2023-05-26T08:37:49.891574Z","iopub.status.idle":"2023-05-26T08:37:50.047930Z","shell.execute_reply.started":"2023-05-26T08:37:49.891527Z","shell.execute_reply":"2023-05-26T08:37:50.046549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import Random Forest classifier\nfrom sklearn.ensemble import RandomForestClassifier\n\n# instantiate the classifier \nrfc = RandomForestClassifier(random_state=0)\n\n# fit the model\nrfc.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:34:56.873402Z","iopub.execute_input":"2023-05-26T08:34:56.873842Z","iopub.status.idle":"2023-05-26T08:35:17.662521Z","shell.execute_reply.started":"2023-05-26T08:34:56.873803Z","shell.execute_reply":"2023-05-26T08:35:17.661276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the Test set results\npred_valid = rfc.predict(X_valid)\n\n\n# Check accuracy score \nfrom sklearn.metrics import accuracy_score\nprint('Model accuracy score with 100 decision-trees : {0:0.4f}'. format(accuracy_score(y_valid, pred_valid)))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:42:32.427318Z","iopub.execute_input":"2023-05-26T08:42:32.427822Z","iopub.status.idle":"2023-05-26T08:42:33.044371Z","shell.execute_reply.started":"2023-05-26T08:42:32.427779Z","shell.execute_reply":"2023-05-26T08:42:33.043190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_df.values\npred_test = rfc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:39:18.343059Z","iopub.execute_input":"2023-05-26T08:39:18.343560Z","iopub.status.idle":"2023-05-26T08:39:19.522707Z","shell.execute_reply.started":"2023-05-26T08:39:18.343526Z","shell.execute_reply":"2023-05-26T08:39:19.521608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsample_sub[\"Label\"] = pred_test\nsample_sub.to_csv(\"rf-sample.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:40:37.038400Z","iopub.execute_input":"2023-05-26T08:40:37.038849Z","iopub.status.idle":"2023-05-26T08:40:37.108813Z","shell.execute_reply.started":"2023-05-26T08:40:37.038814Z","shell.execute_reply":"2023-05-26T08:40:37.107354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}