{"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":" # **Import the necessary libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier # precision_score, recall_score, accuracy_score, \nfrom sklearn.ensemble import RandomForestClassifier\nfrom scipy.stats import randint\nfrom sklearn.metrics import confusion_matrix,ConfusionMatrixDisplay,classification_report","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:38.362413Z","iopub.execute_input":"2023-07-13T10:42:38.363067Z","iopub.status.idle":"2023-07-13T10:42:38.369747Z","shell.execute_reply.started":"2023-07-13T10:42:38.363038Z","shell.execute_reply":"2023-07-13T10:42:38.368033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  **Load dataset**","metadata":{}},{"cell_type":"code","source":"data= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:39.126109Z","iopub.execute_input":"2023-07-13T10:42:39.126497Z","iopub.status.idle":"2023-07-13T10:42:41.703784Z","shell.execute_reply.started":"2023-07-13T10:42:39.126470Z","shell.execute_reply":"2023-07-13T10:42:41.702720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split the dataset**","metadata":{}},{"cell_type":"code","source":"x = data.drop(['label'], axis=1)\ny = data['label']\n","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:41.705940Z","iopub.execute_input":"2023-07-13T10:42:41.706571Z","iopub.status.idle":"2023-07-13T10:42:41.782662Z","shell.execute_reply.started":"2023-07-13T10:42:41.706538Z","shell.execute_reply":"2023-07-13T10:42:41.780922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **EDA**","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:41.784409Z","iopub.execute_input":"2023-07-13T10:42:41.784832Z","iopub.status.idle":"2023-07-13T10:42:41.809464Z","shell.execute_reply.started":"2023-07-13T10:42:41.784808Z","shell.execute_reply":"2023-07-13T10:42:41.807846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.info())\nprint('\\nTraining data shape:', data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:41.811380Z","iopub.execute_input":"2023-07-13T10:42:41.811702Z","iopub.status.idle":"2023-07-13T10:42:41.849506Z","shell.execute_reply.started":"2023-07-13T10:42:41.811676Z","shell.execute_reply":"2023-07-13T10:42:41.848022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for NaN values in the DataFrame\ndata.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:42.077822Z","iopub.execute_input":"2023-07-13T10:42:42.078362Z","iopub.status.idle":"2023-07-13T10:42:42.101660Z","shell.execute_reply.started":"2023-07-13T10:42:42.078328Z","shell.execute_reply":"2023-07-13T10:42:42.100535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distribution of the digits in the dataset\n\ncount= [0] * 10\nfor label in data.iloc[:,0]:\n    count[label] += 1\n\ndigits = [str(i) for i in range(10)]\nplt.bar(digits, count)\nplt.title(\"Distribution of digits in MNIST dataset\")\nplt.xlabel(\"Digit\")\nplt.ylabel(\"Count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:42:42.243415Z","iopub.execute_input":"2023-07-13T10:42:42.244266Z","iopub.status.idle":"2023-07-13T10:42:42.439218Z","shell.execute_reply.started":"2023-07-13T10:42:42.244229Z","shell.execute_reply":"2023-07-13T10:42:42.438647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:44:38.126358Z","iopub.execute_input":"2023-07-13T10:44:38.126762Z","iopub.status.idle":"2023-07-13T10:44:38.515050Z","shell.execute_reply.started":"2023-07-13T10:44:38.126736Z","shell.execute_reply":"2023-07-13T10:44:38.513311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\n X Training data shape:', x_train.shape)\nprint('\\n Y Training data shape:', y_train.shape)\nprint('\\n X Test data shape:', x_test.shape)\nprint('\\n Y Test data shape:', y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T09:56:44.182057Z","iopub.execute_input":"2023-07-13T09:56:44.182411Z","iopub.status.idle":"2023-07-13T09:56:44.188390Z","shell.execute_reply.started":"2023-07-13T09:56:44.182385Z","shell.execute_reply":"2023-07-13T09:56:44.187464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **decision tree algorithm**","metadata":{}},{"cell_type":"code","source":"# Define the hyperparameter for GridSearchCV\nparam_grid = {\n    'max_depth': [5, 10, 15],\n    'min_samples_split': [2, 5, 10],\n    'min_samples_leaf': [1, 2, 5],\n}\n\n# Decision tree classifier\ndt = DecisionTreeClassifier()\n\n# Use grid search with cross-validation to find the best hyperparameters\ngrid_search = GridSearchCV(dt, param_grid, cv=5)\ngrid_search.fit(x_train, y_train)\n\n# Print the best hyperparameters and the accuracy score\nprint('The best parameters:', grid_search.best_params_)\nprint('The best accuracy score:', grid_search.best_score_) ","metadata":{"execution":{"iopub.status.busy":"2023-07-13T05:37:18.329093Z","iopub.execute_input":"2023-07-13T05:37:18.329519Z","iopub.status.idle":"2023-07-13T05:45:59.032583Z","shell.execute_reply.started":"2023-07-13T05:37:18.329484Z","shell.execute_reply":"2023-07-13T05:45:59.031562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#decision tree model\ndclf = DecisionTreeClassifier(criterion='entropy',max_depth=15 ,min_samples_split=2, min_samples_leaf=1)\ndclf = dclf.fit(x_train,y_train)\ny_pred = dclf.predict(x_test)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:52:55.439965Z","iopub.execute_input":"2023-07-13T10:52:55.440312Z","iopub.status.idle":"2023-07-13T10:53:02.971701Z","shell.execute_reply.started":"2023-07-13T10:52:55.440285Z","shell.execute_reply":"2023-07-13T10:53:02.970624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:53:02.973050Z","iopub.execute_input":"2023-07-13T10:53:02.973311Z","iopub.status.idle":"2023-07-13T10:53:03.003676Z","shell.execute_reply.started":"2023-07-13T10:53:02.973289Z","shell.execute_reply":"2023-07-13T10:53:03.002446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm = confusion_matrix(y_test, y_pred)\nConfusionMatrixDisplay(confusion_matrix=dcm).plot()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T05:53:56.599397Z","iopub.execute_input":"2023-07-13T05:53:56.599806Z","iopub.status.idle":"2023-07-13T05:53:57.270175Z","shell.execute_reply.started":"2023-07-13T05:53:56.599777Z","shell.execute_reply":"2023-07-13T05:53:57.269182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Random Forest Algorithm**","metadata":{}},{"cell_type":"code","source":"# Random Forest tree\nrclf = RandomForestClassifier()\nrclf = rclf.fit(x_train,y_train)\ny_pred = rclf.predict(x_test)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:49:49.886601Z","iopub.execute_input":"2023-07-13T10:49:49.886915Z","iopub.status.idle":"2023-07-13T10:50:10.404958Z","shell.execute_reply.started":"2023-07-13T10:49:49.886891Z","shell.execute_reply":"2023-07-13T10:50:10.403914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:51:22.958831Z","iopub.execute_input":"2023-07-13T10:51:22.959172Z","iopub.status.idle":"2023-07-13T10:51:22.989920Z","shell.execute_reply.started":"2023-07-13T10:51:22.959146Z","shell.execute_reply":"2023-07-13T10:51:22.989006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm = confusion_matrix(y_test, y_pred)\nConfusionMatrixDisplay(confusion_matrix=dcm).plot()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T10:50:10.438211Z","iopub.execute_input":"2023-07-13T10:50:10.438540Z","iopub.status.idle":"2023-07-13T10:50:10.854316Z","shell.execute_reply.started":"2023-07-13T10:50:10.438514Z","shell.execute_reply":"2023-07-13T10:50:10.852862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The classification reports appear that the Random Forest algorithm performs better than the Decision Tree algorithm for this problem. The accuracy Random Forest classifier is 0.96, while the accuracy of the Decision Tree classifier is 0.86.Furthermore, the values of all other evaluation metrics (precision, recall, and f1-score ) show that, the values of Random Forest classifier are higher than those for the Decision Tree classifier for all 10 classes. This means Random Forest has fewer false positives and false negatives.","metadata":{}}]}