{"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":"markdown","source":"# MNIST: SIMPLE Machine Learning algorithms\n\n<hr>","metadata":{}},{"cell_type":"code","source":"# import necessary library\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:36.360494Z","iopub.execute_input":"2022-09-07T03:51:36.360805Z","iopub.status.idle":"2022-09-07T03:51:36.365185Z","shell.execute_reply.started":"2022-09-07T03:51:36.360780Z","shell.execute_reply":"2022-09-07T03:51:36.364352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\n\ntest_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:36.368561Z","iopub.execute_input":"2022-09-07T03:51:36.368884Z","iopub.status.idle":"2022-09-07T03:51:39.920852Z","shell.execute_reply.started":"2022-09-07T03:51:36.368854Z","shell.execute_reply":"2022-09-07T03:51:39.920108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.922102Z","iopub.execute_input":"2022-09-07T03:51:39.922317Z","iopub.status.idle":"2022-09-07T03:51:39.929612Z","shell.execute_reply.started":"2022-09-07T03:51:39.922295Z","shell.execute_reply":"2022-09-07T03:51:39.928758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.930396Z","iopub.execute_input":"2022-09-07T03:51:39.930596Z","iopub.status.idle":"2022-09-07T03:51:39.939177Z","shell.execute_reply.started":"2022-09-07T03:51:39.930576Z","shell.execute_reply":"2022-09-07T03:51:39.938458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sample(7)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.940857Z","iopub.execute_input":"2022-09-07T03:51:39.941339Z","iopub.status.idle":"2022-09-07T03:51:39.966607Z","shell.execute_reply.started":"2022-09-07T03:51:39.941316Z","shell.execute_reply":"2022-09-07T03:51:39.965897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.967475Z","iopub.execute_input":"2022-09-07T03:51:39.967770Z","iopub.status.idle":"2022-09-07T03:51:39.981862Z","shell.execute_reply.started":"2022-09-07T03:51:39.967741Z","shell.execute_reply":"2022-09-07T03:51:39.981004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.983126Z","iopub.execute_input":"2022-09-07T03:51:39.983560Z","iopub.status.idle":"2022-09-07T03:51:39.995918Z","shell.execute_reply.started":"2022-09-07T03:51:39.983530Z","shell.execute_reply":"2022-09-07T03:51:39.995103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's see the class distribution balanced or not\n\nplt.figure(figsize=(8,7))\nsns.countplot(x=\"label\", data=train_df)\nplt.title(\"Label distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:39.996908Z","iopub.execute_input":"2022-09-07T03:51:39.997554Z","iopub.status.idle":"2022-09-07T03:51:40.152515Z","shell.execute_reply.started":"2022-09-07T03:51:39.997530Z","shell.execute_reply":"2022-09-07T03:51:40.151491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits = train_df.drop(labels=['label'], axis=1).values\n\nall_label = train_df['label'].values","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.154137Z","iopub.execute_input":"2022-09-07T03:51:40.154738Z","iopub.status.idle":"2022-09-07T03:51:40.194994Z","shell.execute_reply.started":"2022-09-07T03:51:40.154704Z","shell.execute_reply":"2022-09-07T03:51:40.194036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.196173Z","iopub.execute_input":"2022-09-07T03:51:40.196961Z","iopub.status.idle":"2022-09-07T03:51:40.206291Z","shell.execute_reply.started":"2022-09-07T03:51:40.196935Z","shell.execute_reply":"2022-09-07T03:51:40.205298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.209776Z","iopub.execute_input":"2022-09-07T03:51:40.210088Z","iopub.status.idle":"2022-09-07T03:51:40.217560Z","shell.execute_reply.started":"2022-09-07T03:51:40.210058Z","shell.execute_reply":"2022-09-07T03:51:40.216946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits.max(), all_digits.min()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.218548Z","iopub.execute_input":"2022-09-07T03:51:40.218804Z","iopub.status.idle":"2022-09-07T03:51:40.270040Z","shell.execute_reply.started":"2022-09-07T03:51:40.218780Z","shell.execute_reply":"2022-09-07T03:51:40.269133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_label.max(), all_label.min()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.271270Z","iopub.execute_input":"2022-09-07T03:51:40.271750Z","iopub.status.idle":"2022-09-07T03:51:40.277209Z","shell.execute_reply.started":"2022-09-07T03:51:40.271718Z","shell.execute_reply":"2022-09-07T03:51:40.276322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the pixel value\n\nall_digits = all_digits / 255.\n\nall_digits","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.278025Z","iopub.execute_input":"2022-09-07T03:51:40.278553Z","iopub.status.idle":"2022-09-07T03:51:40.337840Z","shell.execute_reply.started":"2022-09-07T03:51:40.278529Z","shell.execute_reply":"2022-09-07T03:51:40.337021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits.max(), all_digits.min()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.339861Z","iopub.execute_input":"2022-09-07T03:51:40.340312Z","iopub.status.idle":"2022-09-07T03:51:40.370017Z","shell.execute_reply.started":"2022-09-07T03:51:40.340287Z","shell.execute_reply":"2022-09-07T03:51:40.369296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.371008Z","iopub.execute_input":"2022-09-07T03:51:40.371228Z","iopub.status.idle":"2022-09-07T03:51:40.381616Z","shell.execute_reply.started":"2022-09-07T03:51:40.371205Z","shell.execute_reply":"2022-09-07T03:51:40.381104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.382314Z","iopub.execute_input":"2022-09-07T03:51:40.382706Z","iopub.status.idle":"2022-09-07T03:51:40.387252Z","shell.execute_reply.started":"2022-09-07T03:51:40.382683Z","shell.execute_reply":"2022-09-07T03:51:40.386666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_digits[0].reshape(28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.387964Z","iopub.execute_input":"2022-09-07T03:51:40.388777Z","iopub.status.idle":"2022-09-07T03:51:40.401883Z","shell.execute_reply.started":"2022-09-07T03:51:40.388753Z","shell.execute_reply":"2022-09-07T03:51:40.401369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(all_digits[0].reshape(28, 28).shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.402692Z","iopub.execute_input":"2022-09-07T03:51:40.403407Z","iopub.status.idle":"2022-09-07T03:51:40.410871Z","shell.execute_reply.started":"2022-09-07T03:51:40.403382Z","shell.execute_reply":"2022-09-07T03:51:40.410112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,5))\nplt.imshow(all_digits[1].reshape(28, 28))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.412012Z","iopub.execute_input":"2022-09-07T03:51:40.412300Z","iopub.status.idle":"2022-09-07T03:51:40.527703Z","shell.execute_reply.started":"2022-09-07T03:51:40.412275Z","shell.execute_reply":"2022-09-07T03:51:40.527075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,5))\nplt.imshow(all_digits[1].reshape(28, 28), cmap=\"binary\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.528647Z","iopub.execute_input":"2022-09-07T03:51:40.529154Z","iopub.status.idle":"2022-09-07T03:51:40.637758Z","shell.execute_reply.started":"2022-09-07T03:51:40.529121Z","shell.execute_reply":"2022-09-07T03:51:40.636879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show some digits\n\n# nrows = 5, ncols = 4\n\nfig, axis = plt.subplots(5, 4, figsize=(22, 20))\n\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(all_digits[i].reshape(28, 28), cmap=\"binary\")\n    ax.set(title=\"Actual digit is: {}\".format(all_label[i]))","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:40.639603Z","iopub.execute_input":"2022-09-07T03:51:40.640382Z","iopub.status.idle":"2022-09-07T03:51:42.633899Z","shell.execute_reply.started":"2022-09-07T03:51:40.640352Z","shell.execute_reply":"2022-09-07T03:51:42.633298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = all_digits.copy()\n\ny = all_label.copy()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.634906Z","iopub.execute_input":"2022-09-07T03:51:42.635341Z","iopub.status.idle":"2022-09-07T03:51:42.690192Z","shell.execute_reply.started":"2022-09-07T03:51:42.635315Z","shell.execute_reply":"2022-09-07T03:51:42.689286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import confusion_matrix, accuracy_score, classification_report","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.691326Z","iopub.execute_input":"2022-09-07T03:51:42.691625Z","iopub.status.idle":"2022-09-07T03:51:42.917719Z","shell.execute_reply.started":"2022-09-07T03:51:42.691595Z","shell.execute_reply":"2022-09-07T03:51:42.916979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the dataset training and test set\n\nx_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=21)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.918805Z","iopub.execute_input":"2022-09-07T03:51:42.919511Z","iopub.status.idle":"2022-09-07T03:51:42.965666Z","shell.execute_reply.started":"2022-09-07T03:51:42.919477Z","shell.execute_reply":"2022-09-07T03:51:42.964869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X_train shape: \", x_train.shape)\nprint(\"X_test shape: \", x_test.shape)\nprint(\"y_train shape: \", y_train.shape)\nprint(\"y_test shape: \", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.966760Z","iopub.execute_input":"2022-09-07T03:51:42.967300Z","iopub.status.idle":"2022-09-07T03:51:42.971577Z","shell.execute_reply.started":"2022-09-07T03:51:42.967270Z","shell.execute_reply":"2022-09-07T03:51:42.971019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's create Model\n\n<hr>\n\n## 1) RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"model1 = RandomForestClassifier(n_estimators=200, max_depth=10, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.972521Z","iopub.execute_input":"2022-09-07T03:51:42.972870Z","iopub.status.idle":"2022-09-07T03:51:42.985417Z","shell.execute_reply.started":"2022-09-07T03:51:42.972849Z","shell.execute_reply":"2022-09-07T03:51:42.984697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.fit(X=x_train, y=y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:51:42.986231Z","iopub.execute_input":"2022-09-07T03:51:42.986559Z","iopub.status.idle":"2022-09-07T03:52:09.849858Z","shell.execute_reply.started":"2022-09-07T03:51:42.986538Z","shell.execute_reply":"2022-09-07T03:52:09.849030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1 = model1.predict(X=x_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:09.855146Z","iopub.execute_input":"2022-09-07T03:52:09.855383Z","iopub.status.idle":"2022-09-07T03:52:10.158447Z","shell.execute_reply.started":"2022-09-07T03:52:09.855361Z","shell.execute_reply":"2022-09-07T03:52:10.157682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_accuracy = accuracy_score(y_true=y_test, y_pred=y_pred1)\n\nprint(\"Model accuracy is: {:.3f}%\".format(rf_accuracy*100))","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:10.159570Z","iopub.execute_input":"2022-09-07T03:52:10.159896Z","iopub.status.idle":"2022-09-07T03:52:10.165014Z","shell.execute_reply.started":"2022-09-07T03:52:10.159865Z","shell.execute_reply":"2022-09-07T03:52:10.164414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compare our result\n\nfig, axis = plt.subplots(5, 4, figsize=(18,25))\n\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(x_test[i].reshape(28,28), cmap=\"binary\")\n    ax.set(title=\"Predicted digit: {0}\\nActual digit: {1}\".format(y_pred1[i], y_test[i]))","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:10.165806Z","iopub.execute_input":"2022-09-07T03:52:10.166426Z","iopub.status.idle":"2022-09-07T03:52:12.082155Z","shell.execute_reply.started":"2022-09-07T03:52:10.166394Z","shell.execute_reply":"2022-09-07T03:52:12.081545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true=y_test, y_pred=y_pred1)\n\nplt.figure(figsize=(12, 8))\nsns.heatmap(data=cm, annot=True, fmt='.0f')\nplt.xlabel(\"Predicted Digits\")\nplt.ylabel(\"True Digits\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:12.082819Z","iopub.execute_input":"2022-09-07T03:52:12.083082Z","iopub.status.idle":"2022-09-07T03:52:12.645340Z","shell.execute_reply.started":"2022-09-07T03:52:12.083061Z","shell.execute_reply":"2022-09-07T03:52:12.644482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Submission","metadata":{}},{"cell_type":"code","source":"test_df.values","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:12.646527Z","iopub.execute_input":"2022-09-07T03:52:12.647392Z","iopub.status.idle":"2022-09-07T03:52:12.653754Z","shell.execute_reply.started":"2022-09-07T03:52:12.647359Z","shell.execute_reply":"2022-09-07T03:52:12.653005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_df.values / 255.","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:12.654639Z","iopub.execute_input":"2022-09-07T03:52:12.654877Z","iopub.status.idle":"2022-09-07T03:52:12.694122Z","shell.execute_reply.started":"2022-09-07T03:52:12.654847Z","shell.execute_reply":"2022-09-07T03:52:12.693360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model1.predict(X=test_x)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:12.695318Z","iopub.execute_input":"2022-09-07T03:52:12.695546Z","iopub.status.idle":"2022-09-07T03:52:13.721698Z","shell.execute_reply.started":"2022-09-07T03:52:12.695522Z","shell.execute_reply":"2022-09-07T03:52:13.720883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:13.722778Z","iopub.execute_input":"2022-09-07T03:52:13.723036Z","iopub.status.idle":"2022-09-07T03:52:13.727919Z","shell.execute_reply.started":"2022-09-07T03:52:13.723012Z","shell.execute_reply":"2022-09-07T03:52:13.727114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:13.729055Z","iopub.execute_input":"2022-09-07T03:52:13.729273Z","iopub.status.idle":"2022-09-07T03:52:13.751949Z","shell.execute_reply.started":"2022-09-07T03:52:13.729251Z","shell.execute_reply":"2022-09-07T03:52:13.751285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Make submission file","metadata":{}},{"cell_type":"code","source":"submission['Label'] = test_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:13.752924Z","iopub.execute_input":"2022-09-07T03:52:13.753172Z","iopub.status.idle":"2022-09-07T03:52:13.756549Z","shell.execute_reply.started":"2022-09-07T03:52:13.753150Z","shell.execute_reply":"2022-09-07T03:52:13.756006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:13.757501Z","iopub.execute_input":"2022-09-07T03:52:13.757910Z","iopub.status.idle":"2022-09-07T03:52:13.769422Z","shell.execute_reply.started":"2022-09-07T03:52:13.757887Z","shell.execute_reply":"2022-09-07T03:52:13.768628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the submission file\n\n# submission.to_csv(\"dataset/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T03:52:13.770395Z","iopub.execute_input":"2022-09-07T03:52:13.770908Z","iopub.status.idle":"2022-09-07T03:52:13.776901Z","shell.execute_reply.started":"2022-09-07T03:52:13.770878Z","shell.execute_reply":"2022-09-07T03:52:13.776360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}