{"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 pandas as pd\nimport numpy as np\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-18T13:05:18.065362Z","iopub.execute_input":"2022-08-18T13:05:18.065883Z","iopub.status.idle":"2022-08-18T13:05:18.072934Z","shell.execute_reply.started":"2022-08-18T13:05:18.065839Z","shell.execute_reply":"2022-08-18T13:05:18.071670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:34:41.380690Z","iopub.execute_input":"2022-08-18T12:34:41.381140Z","iopub.status.idle":"2022-08-18T12:34:41.387994Z","shell.execute_reply.started":"2022-08-18T12:34:41.381104Z","shell.execute_reply":"2022-08-18T12:34:41.386505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:36:12.201428Z","iopub.execute_input":"2022-08-18T12:36:12.201892Z","iopub.status.idle":"2022-08-18T12:36:14.660122Z","shell.execute_reply.started":"2022-08-18T12:36:12.201855Z","shell.execute_reply":"2022-08-18T12:36:14.658630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:36:16.565900Z","iopub.execute_input":"2022-08-18T12:36:16.566339Z","iopub.status.idle":"2022-08-18T12:36:16.595595Z","shell.execute_reply.started":"2022-08-18T12:36:16.566303Z","shell.execute_reply":"2022-08-18T12:36:16.594348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Viewing the number at 3rd row of the dataset i.e. '4'","metadata":{}},{"cell_type":"code","source":"sample_1 = train_data.iloc[3,1:].values\nsample_1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:39:37.821733Z","iopub.execute_input":"2022-08-18T12:39:37.822175Z","iopub.status.idle":"2022-08-18T12:39:37.835068Z","shell.execute_reply.started":"2022-08-18T12:39:37.822139Z","shell.execute_reply":"2022-08-18T12:39:37.833795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reshaping the above data into the size of (28x2) and into type of 'uint8'","metadata":{}},{"cell_type":"code","source":"sample_1 = sample_1.reshape(28,28).astype('uint8')\nsample_1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:41:39.842321Z","iopub.execute_input":"2022-08-18T12:41:39.843488Z","iopub.status.idle":"2022-08-18T12:41:39.855581Z","shell.execute_reply.started":"2022-08-18T12:41:39.843438Z","shell.execute_reply":"2022-08-18T12:41:39.854593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Viewing the number at 3rd row of the dataset using matplotlib","metadata":{}},{"cell_type":"code","source":"plt.imshow(sample_1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:42:39.274043Z","iopub.execute_input":"2022-08-18T12:42:39.274991Z","iopub.status.idle":"2022-08-18T12:42:39.500201Z","shell.execute_reply.started":"2022-08-18T12:42:39.274943Z","shell.execute_reply":"2022-08-18T12:42:39.499289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Viewing the number at the 18th row of the given dataset using the same process as for sample_1","metadata":{}},{"cell_type":"code","source":"sample_2 = train_data.iloc[18,1:].values  ##Value at 18th row\nsample_2 = sample_2.reshape(28,28).astype('uint8') ## Reshaping the sample_2 into size of 28x28\nplt.imshow(sample_2)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:45:49.617584Z","iopub.execute_input":"2022-08-18T12:45:49.618042Z","iopub.status.idle":"2022-08-18T12:45:49.818380Z","shell.execute_reply.started":"2022-08-18T12:45:49.618003Z","shell.execute_reply":"2022-08-18T12:45:49.817101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Verifying whether the number at 18th row is '7' or not","metadata":{}},{"cell_type":"code","source":"train_data[train_data.index == 18]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:48:38.605504Z","iopub.execute_input":"2022-08-18T12:48:38.605996Z","iopub.status.idle":"2022-08-18T12:48:38.635622Z","shell.execute_reply.started":"2022-08-18T12:48:38.605953Z","shell.execute_reply":"2022-08-18T12:48:38.634209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Separating labels and data values(pixel values)","metadata":{}},{"cell_type":"code","source":"train_data_labels = train_data.iloc[:,1:]\ntrain_data_values = train_data.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:50:51.720740Z","iopub.execute_input":"2022-08-18T12:50:51.721699Z","iopub.status.idle":"2022-08-18T12:50:51.730060Z","shell.execute_reply.started":"2022-08-18T12:50:51.721642Z","shell.execute_reply":"2022-08-18T12:50:51.728682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating test and train size of the model","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train_data_labels, train_data_values, test_size = 0.2, random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:54:09.342352Z","iopub.execute_input":"2022-08-18T12:54:09.343655Z","iopub.status.idle":"2022-08-18T12:54:09.680236Z","shell.execute_reply.started":"2022-08-18T12:54:09.343603Z","shell.execute_reply":"2022-08-18T12:54:09.678684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:54:15.762269Z","iopub.execute_input":"2022-08-18T12:54:15.762728Z","iopub.status.idle":"2022-08-18T12:54:15.784679Z","shell.execute_reply.started":"2022-08-18T12:54:15.762688Z","shell.execute_reply":"2022-08-18T12:54:15.783202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:54:45.692734Z","iopub.execute_input":"2022-08-18T12:54:45.693211Z","iopub.status.idle":"2022-08-18T12:54:45.702740Z","shell.execute_reply.started":"2022-08-18T12:54:45.693171Z","shell.execute_reply":"2022-08-18T12:54:45.701471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the model using the first ML model RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"model_1 = RandomForestClassifier(n_estimators = 100)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:56:19.313032Z","iopub.execute_input":"2022-08-18T12:56:19.313549Z","iopub.status.idle":"2022-08-18T12:56:19.318738Z","shell.execute_reply.started":"2022-08-18T12:56:19.313506Z","shell.execute_reply":"2022-08-18T12:56:19.317742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fitting our data into RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"model_1.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:57:14.912980Z","iopub.execute_input":"2022-08-18T12:57:14.913951Z","iopub.status.idle":"2022-08-18T12:57:43.084010Z","shell.execute_reply.started":"2022-08-18T12:57:14.913895Z","shell.execute_reply":"2022-08-18T12:57:43.082844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predicting values using RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"predict_1 = model_1.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:58:51.257708Z","iopub.execute_input":"2022-08-18T12:58:51.258182Z","iopub.status.idle":"2022-08-18T12:58:51.706336Z","shell.execute_reply.started":"2022-08-18T12:58:51.258140Z","shell.execute_reply":"2022-08-18T12:58:51.705018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T12:59:02.686597Z","iopub.execute_input":"2022-08-18T12:59:02.687026Z","iopub.status.idle":"2022-08-18T12:59:02.694172Z","shell.execute_reply.started":"2022-08-18T12:59:02.686988Z","shell.execute_reply":"2022-08-18T12:59:02.693321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Verifying the prediction accuracy","metadata":{}},{"cell_type":"code","source":"y_test_values = y_test.values","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:01:26.733065Z","iopub.execute_input":"2022-08-18T13:01:26.734130Z","iopub.status.idle":"2022-08-18T13:01:26.739702Z","shell.execute_reply.started":"2022-08-18T13:01:26.734074Z","shell.execute_reply":"2022-08-18T13:01:26.738477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculating the total number of correctly predicted values by RandomForestClassifier\ncount_model_1 = 0\nfor i in range(len(predict_1)):\n    if predict_1[i] == y_test_values[i]:\n        count_model_1 = count_model_1 + 1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:02:10.052590Z","iopub.execute_input":"2022-08-18T13:02:10.053034Z","iopub.status.idle":"2022-08-18T13:02:10.062184Z","shell.execute_reply.started":"2022-08-18T13:02:10.052994Z","shell.execute_reply":"2022-08-18T13:02:10.060876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Number of predictions done correctly using RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"count_model_1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:02:17.792082Z","iopub.execute_input":"2022-08-18T13:02:17.792506Z","iopub.status.idle":"2022-08-18T13:02:17.800353Z","shell.execute_reply.started":"2022-08-18T13:02:17.792470Z","shell.execute_reply":"2022-08-18T13:02:17.799068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Total values on which the predcition was done one","metadata":{}},{"cell_type":"code","source":"len(predict_1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:03:41.332122Z","iopub.execute_input":"2022-08-18T13:03:41.332585Z","iopub.status.idle":"2022-08-18T13:03:41.340156Z","shell.execute_reply.started":"2022-08-18T13:03:41.332548Z","shell.execute_reply":"2022-08-18T13:03:41.338665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Calculating Accuracy","metadata":{}},{"cell_type":"code","source":"count_model_1/len(predict_1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:04:26.212342Z","iopub.execute_input":"2022-08-18T13:04:26.212850Z","iopub.status.idle":"2022-08-18T13:04:26.221172Z","shell.execute_reply.started":"2022-08-18T13:04:26.212804Z","shell.execute_reply":"2022-08-18T13:04:26.219946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,predict_1))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:05:51.564009Z","iopub.execute_input":"2022-08-18T13:05:51.564809Z","iopub.status.idle":"2022-08-18T13:05:51.596716Z","shell.execute_reply.started":"2022-08-18T13:05:51.564764Z","shell.execute_reply":"2022-08-18T13:05:51.595441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## So, we got 96.38% accuracy using RandomForestClassifier","metadata":{}},{"cell_type":"markdown","source":"## Training the model using the second ML model DecisionTreeClassifier","metadata":{}},{"cell_type":"code","source":"model_2 = DecisionTreeClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:11:42.534403Z","iopub.execute_input":"2022-08-18T13:11:42.535684Z","iopub.status.idle":"2022-08-18T13:11:42.541352Z","shell.execute_reply.started":"2022-08-18T13:11:42.535634Z","shell.execute_reply":"2022-08-18T13:11:42.539754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fitting our data into the DecisionTreeClassifier model","metadata":{}},{"cell_type":"code","source":"model_2.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:12:41.825511Z","iopub.execute_input":"2022-08-18T13:12:41.826035Z","iopub.status.idle":"2022-08-18T13:12:53.641530Z","shell.execute_reply.started":"2022-08-18T13:12:41.825992Z","shell.execute_reply":"2022-08-18T13:12:53.640087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Viewing how our Decision Tree Model classified the training data","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import plot_tree","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:13:45.754926Z","iopub.execute_input":"2022-08-18T13:13:45.755376Z","iopub.status.idle":"2022-08-18T13:13:45.761357Z","shell.execute_reply.started":"2022-08-18T13:13:45.755339Z","shell.execute_reply":"2022-08-18T13:13:45.760134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(280,40))\nplot_tree(model_2, max_depth = 1,feature_names=train_data_labels.columns,\n          filled = True,\n          impurity = False,\n          rounded = True,\n          precision = 1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:18:14.944514Z","iopub.execute_input":"2022-08-18T13:18:14.945034Z","iopub.status.idle":"2022-08-18T13:18:18.695416Z","shell.execute_reply.started":"2022-08-18T13:18:14.944991Z","shell.execute_reply":"2022-08-18T13:18:18.694062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predicting the values using our trained model","metadata":{}},{"cell_type":"code","source":"predict_2 = model_2.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:19:28.650094Z","iopub.execute_input":"2022-08-18T13:19:28.650620Z","iopub.status.idle":"2022-08-18T13:19:28.684972Z","shell.execute_reply.started":"2022-08-18T13:19:28.650576Z","shell.execute_reply":"2022-08-18T13:19:28.683572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Viewing the accuracy of our model","metadata":{}},{"cell_type":"code","source":"count_model_2 = 0\nfor i in range(len(predict_2)):\n    if predict_2[i] == y_test_values[i]:\n        count_model_2 = count_model_2 + 1","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:22:02.485130Z","iopub.execute_input":"2022-08-18T13:22:02.485680Z","iopub.status.idle":"2022-08-18T13:22:02.495698Z","shell.execute_reply.started":"2022-08-18T13:22:02.485620Z","shell.execute_reply":"2022-08-18T13:22:02.494350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_model_2, len(predict_2)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:22:26.396629Z","iopub.execute_input":"2022-08-18T13:22:26.397509Z","iopub.status.idle":"2022-08-18T13:22:26.405223Z","shell.execute_reply.started":"2022-08-18T13:22:26.397454Z","shell.execute_reply":"2022-08-18T13:22:26.403894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_model_2/len(predict_2)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:22:56.056552Z","iopub.execute_input":"2022-08-18T13:22:56.057371Z","iopub.status.idle":"2022-08-18T13:22:56.064478Z","shell.execute_reply.started":"2022-08-18T13:22:56.057322Z","shell.execute_reply":"2022-08-18T13:22:56.063053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,predict_2))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T13:20:10.076747Z","iopub.execute_input":"2022-08-18T13:20:10.077217Z","iopub.status.idle":"2022-08-18T13:20:10.108745Z","shell.execute_reply.started":"2022-08-18T13:20:10.077181Z","shell.execute_reply":"2022-08-18T13:20:10.107215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# So, we got an accuracy of 85.55% using DecisionTreeClassifier","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}