{"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":"Libs","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:30.237182Z","iopub.execute_input":"2022-05-02T21:03:30.237610Z","iopub.status.idle":"2022-05-02T21:03:30.252889Z","shell.execute_reply.started":"2022-05-02T21:03:30.237560Z","shell.execute_reply":"2022-05-02T21:03:30.252076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading the Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:30.254377Z","iopub.execute_input":"2022-05-02T21:03:30.254705Z","iopub.status.idle":"2022-05-02T21:03:36.372467Z","shell.execute_reply.started":"2022-05-02T21:03:30.254659Z","shell.execute_reply":"2022-05-02T21:03:36.371210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.astype(np.uint8)\ntest = test.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.373759Z","iopub.execute_input":"2022-05-02T21:03:36.376534Z","iopub.status.idle":"2022-05-02T21:03:36.432031Z","shell.execute_reply.started":"2022-05-02T21:03:36.376474Z","shell.execute_reply":"2022-05-02T21:03:36.430680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train),len(test)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.433661Z","iopub.execute_input":"2022-05-02T21:03:36.433920Z","iopub.status.idle":"2022-05-02T21:03:36.443774Z","shell.execute_reply.started":"2022-05-02T21:03:36.433883Z","shell.execute_reply":"2022-05-02T21:03:36.442294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(columns=['label'])      # Reading the images by not including the labels\ny = train['label']                     # Reading all the labels","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.446281Z","iopub.execute_input":"2022-05-02T21:03:36.446740Z","iopub.status.idle":"2022-05-02T21:03:36.553133Z","shell.execute_reply.started":"2022-05-02T21:03:36.446675Z","shell.execute_reply":"2022-05-02T21:03:36.551590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Splitting the data into train and validation steps","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import ShuffleSplit\nsss = ShuffleSplit(n_splits=1, test_size=0.20, random_state=0)\n\ntrain_index, val_index = next(iter(sss.split(X, y)))\nX_train, X_test = X.iloc[train_index], X.iloc[val_index]\ny_train, y_test = y.iloc[train_index], y.iloc[val_index]","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.556873Z","iopub.execute_input":"2022-05-02T21:03:36.557343Z","iopub.status.idle":"2022-05-02T21:03:36.835566Z","shell.execute_reply.started":"2022-05-02T21:03:36.557271Z","shell.execute_reply":"2022-05-02T21:03:36.834278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train),len(X_test),len(y_train),len(y_test)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-05-02T21:03:36.837741Z","iopub.execute_input":"2022-05-02T21:03:36.838183Z","iopub.status.idle":"2022-05-02T21:03:36.847888Z","shell.execute_reply.started":"2022-05-02T21:03:36.838129Z","shell.execute_reply":"2022-05-02T21:03:36.846179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating the model","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.850115Z","iopub.execute_input":"2022-05-02T21:03:36.850463Z","iopub.status.idle":"2022-05-02T21:03:36.866775Z","shell.execute_reply.started":"2022-05-02T21:03:36.850426Z","shell.execute_reply":"2022-05-02T21:03:36.865751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_jobs=-1,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.868589Z","iopub.execute_input":"2022-05-02T21:03:36.868873Z","iopub.status.idle":"2022-05-02T21:03:36.886996Z","shell.execute_reply.started":"2022-05-02T21:03:36.868842Z","shell.execute_reply":"2022-05-02T21:03:36.885571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:36.888738Z","iopub.execute_input":"2022-05-02T21:03:36.889344Z","iopub.status.idle":"2022-05-02T21:03:46.864504Z","shell.execute_reply.started":"2022-05-02T21:03:36.889273Z","shell.execute_reply":"2022-05-02T21:03:46.863157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:46.866213Z","iopub.execute_input":"2022-05-02T21:03:46.867034Z","iopub.status.idle":"2022-05-02T21:03:47.574215Z","shell.execute_reply.started":"2022-05-02T21:03:46.866987Z","shell.execute_reply":"2022-05-02T21:03:47.572827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:47.576868Z","iopub.execute_input":"2022-05-02T21:03:47.577151Z","iopub.status.idle":"2022-05-02T21:03:47.809095Z","shell.execute_reply.started":"2022-05-02T21:03:47.577121Z","shell.execute_reply":"2022-05-02T21:03:47.808188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(X_test)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-05-02T21:03:47.810958Z","iopub.execute_input":"2022-05-02T21:03:47.811300Z","iopub.status.idle":"2022-05-02T21:03:48.045252Z","shell.execute_reply.started":"2022-05-02T21:03:47.811262Z","shell.execute_reply":"2022-05-02T21:03:48.044428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = model.predict(X_test)\n# Convert predictions classes to one hot vectors \n\n\n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_pred, y_test) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10)) ","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:03:48.046504Z","iopub.execute_input":"2022-05-02T21:03:48.047478Z","iopub.status.idle":"2022-05-02T21:03:49.056008Z","shell.execute_reply.started":"2022-05-02T21:03:48.047426Z","shell.execute_reply":"2022-05-02T21:03:49.054962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating the submission.csv file","metadata":{}},{"cell_type":"code","source":"submissions = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-05-02T21:05:43.743133Z","iopub.execute_input":"2022-05-02T21:05:43.744016Z","iopub.status.idle":"2022-05-02T21:05:43.758628Z","shell.execute_reply.started":"2022-05-02T21:05:43.743941Z","shell.execute_reply":"2022-05-02T21:05:43.757420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:05:44.463338Z","iopub.execute_input":"2022-05-02T21:05:44.463811Z","iopub.status.idle":"2022-05-02T21:05:45.028719Z","shell.execute_reply.started":"2022-05-02T21:05:44.463761Z","shell.execute_reply":"2022-05-02T21:05:45.027543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"Submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:05:45.186015Z","iopub.execute_input":"2022-05-02T21:05:45.186388Z","iopub.status.idle":"2022-05-02T21:05:45.248670Z","shell.execute_reply.started":"2022-05-02T21:05:45.186339Z","shell.execute_reply":"2022-05-02T21:05:45.247512Z"},"trusted":true},"execution_count":null,"outputs":[]}]}