{"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":"<h3>Loading and visualizing the digits data</h3>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nimport plotly.graph_objects as go\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2021-10-17T09:02:02.302089Z","iopub.execute_input":"2021-10-17T09:02:02.302999Z","iopub.status.idle":"2021-10-17T09:02:02.309275Z","shell.execute_reply.started":"2021-10-17T09:02:02.302939Z","shell.execute_reply":"2021-10-17T09:02:02.308298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:51:50.630389Z","iopub.execute_input":"2021-10-17T08:51:50.630692Z","iopub.status.idle":"2021-10-17T08:51:54.326480Z","shell.execute_reply.started":"2021-10-17T08:51:50.630661Z","shell.execute_reply":"2021-10-17T08:51:54.325356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:19.993250Z","iopub.execute_input":"2021-10-17T08:19:19.993593Z","iopub.status.idle":"2021-10-17T08:19:20.026595Z","shell.execute_reply.started":"2021-10-17T08:19:19.993558Z","shell.execute_reply":"2021-10-17T08:19:20.025472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:25.353794Z","iopub.execute_input":"2021-10-17T08:19:25.354123Z","iopub.status.idle":"2021-10-17T08:19:25.361951Z","shell.execute_reply.started":"2021-10-17T08:19:25.354088Z","shell.execute_reply":"2021-10-17T08:19:25.360816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Counting unique labels in the dataframe</h3>","metadata":{}},{"cell_type":"code","source":"df_train['label'].value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:31.761906Z","iopub.execute_input":"2021-10-17T08:19:31.762190Z","iopub.status.idle":"2021-10-17T08:19:31.780869Z","shell.execute_reply.started":"2021-10-17T08:19:31.762160Z","shell.execute_reply":"2021-10-17T08:19:31.779976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Dataframe unique labels visualization</h3>","metadata":{}},{"cell_type":"code","source":"fig = px.bar(df_train['label'].value_counts().sort_index(), \n             color=df_train['label'].value_counts().index,\n            labels={\n                     \"value\": \"count\",\n                     \"index\": \"label\",\n                 }\n            )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:37.564332Z","iopub.execute_input":"2021-10-17T08:19:37.565412Z","iopub.status.idle":"2021-10-17T08:19:38.563349Z","shell.execute_reply.started":"2021-10-17T08:19:37.565351Z","shell.execute_reply":"2021-10-17T08:19:38.562478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('../input/digit-recognizer/test.csv')\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:47.731439Z","iopub.execute_input":"2021-10-17T08:19:47.732342Z","iopub.status.idle":"2021-10-17T08:19:50.049024Z","shell.execute_reply.started":"2021-10-17T08:19:47.732291Z","shell.execute_reply":"2021-10-17T08:19:50.047844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:19:55.789256Z","iopub.execute_input":"2021-10-17T08:19:55.789546Z","iopub.status.idle":"2021-10-17T08:19:55.796449Z","shell.execute_reply.started":"2021-10-17T08:19:55.789517Z","shell.execute_reply":"2021-10-17T08:19:55.795460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_reshaped = df_train.iloc[:, 1:785].values.reshape(42000,28,28)\ndf_train_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:20:09.930490Z","iopub.execute_input":"2021-10-17T08:20:09.930832Z","iopub.status.idle":"2021-10-17T08:20:09.939462Z","shell.execute_reply.started":"2021-10-17T08:20:09.930799Z","shell.execute_reply":"2021-10-17T08:20:09.938407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, axes = plt.subplots(10, 10, figsize=(16, 16),\n                        subplot_kw={'xticks':[], 'yticks':[]},\n                        gridspec_kw=dict(hspace=0.1, wspace=0.1))\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(df_train_reshaped[i], cmap='binary',\n            interpolation='nearest'),\n    ax.text(0.8, 0.8, df_train['label'].to_numpy()[i],\n        transform=ax.transAxes, color='green', fontsize=16)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:20:14.826691Z","iopub.execute_input":"2021-10-17T08:20:14.827025Z","iopub.status.idle":"2021-10-17T08:20:20.122926Z","shell.execute_reply.started":"2021-10-17T08:20:14.826990Z","shell.execute_reply":"2021-10-17T08:20:20.119820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain, Xtest, ytrain, ytest = train_test_split(df_train, df_train['label'],\n                                                random_state=0)\nmodel = RandomForestClassifier(n_estimators=1000)\nmodel.fit(Xtrain, ytrain)\nypred = model.predict(Xtest)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:52:03.950524Z","iopub.execute_input":"2021-10-17T08:52:03.951086Z","iopub.status.idle":"2021-10-17T08:55:32.124605Z","shell.execute_reply.started":"2021-10-17T08:52:03.951038Z","shell.execute_reply":"2021-10-17T08:55:32.123713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>The classification report for this classifier:</h4>","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\nprint(metrics.classification_report(ypred, ytest))","metadata":{"execution":{"iopub.status.busy":"2021-10-17T08:56:41.297087Z","iopub.execute_input":"2021-10-17T08:56:41.297677Z","iopub.status.idle":"2021-10-17T08:56:41.330510Z","shell.execute_reply.started":"2021-10-17T08:56:41.297640Z","shell.execute_reply":"2021-10-17T08:56:41.329464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Plot the confusion matrix:</h4>","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nsns.set(rc={'figure.figsize':(15,12)})\n\nmat = confusion_matrix(ytest, ypred)\nsns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False)\nplt.xlabel('true label')\nplt.ylabel('predicted label');","metadata":{"execution":{"iopub.status.busy":"2021-10-17T09:02:12.535281Z","iopub.execute_input":"2021-10-17T09:02:12.535654Z","iopub.status.idle":"2021-10-17T09:02:13.416018Z","shell.execute_reply.started":"2021-10-17T09:02:12.535608Z","shell.execute_reply":"2021-10-17T09:02:13.415259Z"},"trusted":true},"execution_count":null,"outputs":[]}]}