{"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 necessary modules:\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import ConfusionMatrixDisplay","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:54.468730Z","iopub.execute_input":"2023-02-02T15:42:54.469588Z","iopub.status.idle":"2023-02-02T15:42:55.017072Z","shell.execute_reply.started":"2023-02-02T15:42:54.469460Z","shell.execute_reply":"2023-02-02T15:42:55.015840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import train & test data from kaggle:\ndata_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndata_test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:55.019229Z","iopub.execute_input":"2023-02-02T15:42:55.019606Z","iopub.status.idle":"2023-02-02T15:42:59.139160Z","shell.execute_reply.started":"2023-02-02T15:42:55.019550Z","shell.execute_reply":"2023-02-02T15:42:59.137807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dataframe preview:\ndata_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.140608Z","iopub.execute_input":"2023-02-02T15:42:59.140958Z","iopub.status.idle":"2023-02-02T15:42:59.164218Z","shell.execute_reply.started":"2023-02-02T15:42:59.140928Z","shell.execute_reply":"2023-02-02T15:42:59.163219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Divide train and test data:\nX_train = data_train.sample(frac=0.8, replace=False)\nX_test = data_train.drop(X_train.index, axis=0)\n\n#Divide data and target:\ny_train = X_train['label']\nX_train = X_train.drop('label', axis=1)\n\ny_test = X_test['label']\nX_test = X_test.drop('label', axis=1)\n\nprint('X_train shape: ', X_train.shape)\nprint('X_test shape: ', X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.166250Z","iopub.execute_input":"2023-02-02T15:42:59.167227Z","iopub.status.idle":"2023-02-02T15:42:59.505084Z","shell.execute_reply.started":"2023-02-02T15:42:59.167184Z","shell.execute_reply":"2023-02-02T15:42:59.503647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Number preview:\ndigit_binary = X_train.iloc[55, ]['pixel0' : 'pixel783']\ndigit_array = digit_binary.to_numpy().reshape(28,28)\nplt.imshow(digit_array, interpolation='nearest', cmap=plt.cm.binary)\nplt.title('Label number: {}'.format(y_train.iloc[55,]))\nplt.colorbar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.506440Z","iopub.execute_input":"2023-02-02T15:42:59.506868Z","iopub.status.idle":"2023-02-02T15:42:59.776808Z","shell.execute_reply.started":"2023-02-02T15:42:59.506833Z","shell.execute_reply":"2023-02-02T15:42:59.775529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Hyperparams optimization:\nn_estimators = np.linspace(start = 100, stop = 1000, num = 10, dtype = 'int')\nmax_features = ['sqrt']\nmax_depth = np.linspace(start = 10, stop = 100, num = 10, dtype = 'int')\nmin_samples_split = np.linspace(start = 2, stop = 10, num = 5, dtype = 'int')\nmin_samples_leaf = np.linspace(start = 2, stop = 6, num = 5, dtype = 'int')\nbootstrap = [True, False]\n\nhyper_param = {'n_estimators': n_estimators,\n               'max_features': max_features,\n               'max_depth': max_depth,\n               'min_samples_split': min_samples_split,\n               'min_samples_leaf': min_samples_leaf,\n               'bootstrap': bootstrap}","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.778263Z","iopub.execute_input":"2023-02-02T15:42:59.778675Z","iopub.status.idle":"2023-02-02T15:42:59.787818Z","shell.execute_reply.started":"2023-02-02T15:42:59.778640Z","shell.execute_reply":"2023-02-02T15:42:59.786309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create random forest classifier:\nrfc = RandomForestClassifier(random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.789744Z","iopub.execute_input":"2023-02-02T15:42:59.790244Z","iopub.status.idle":"2023-02-02T15:42:59.804105Z","shell.execute_reply.started":"2023-02-02T15:42:59.790194Z","shell.execute_reply":"2023-02-02T15:42:59.803052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Configure random search:\nrs = RandomizedSearchCV(estimator=rfc,\n                  param_distributions=hyper_param,\n                  scoring='accuracy',\n                  cv=5,\n                  n_jobs=-1,\n                  verbose=0,\n                  n_iter=3\n                  )","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.805255Z","iopub.execute_input":"2023-02-02T15:42:59.806154Z","iopub.status.idle":"2023-02-02T15:42:59.818103Z","shell.execute_reply.started":"2023-02-02T15:42:59.806117Z","shell.execute_reply":"2023-02-02T15:42:59.816911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Run random search:\nrs.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:42:59.820179Z","iopub.execute_input":"2023-02-02T15:42:59.820715Z","iopub.status.idle":"2023-02-02T15:55:04.750147Z","shell.execute_reply.started":"2023-02-02T15:42:59.820667Z","shell.execute_reply":"2023-02-02T15:55:04.748821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Best params configuration:\nrs.best_params_","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:04.754202Z","iopub.execute_input":"2023-02-02T15:55:04.754611Z","iopub.status.idle":"2023-02-02T15:55:04.762892Z","shell.execute_reply.started":"2023-02-02T15:55:04.754556Z","shell.execute_reply":"2023-02-02T15:55:04.761502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Run prediction:\ny_pred = rs.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:04.764927Z","iopub.execute_input":"2023-02-02T15:55:04.765412Z","iopub.status.idle":"2023-02-02T15:55:06.249561Z","shell.execute_reply.started":"2023-02-02T15:55:04.765365Z","shell.execute_reply":"2023-02-02T15:55:06.248183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calc. classification report:\nprint(classification_report(y_pred, y_test, zero_division=0))","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:06.251631Z","iopub.execute_input":"2023-02-02T15:55:06.252112Z","iopub.status.idle":"2023-02-02T15:55:06.283646Z","shell.execute_reply.started":"2023-02-02T15:55:06.252064Z","shell.execute_reply":"2023-02-02T15:55:06.282628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Print confusion matrix:\nfig, ax = plt.subplots(figsize=(10, 10))\ncm = confusion_matrix(y_pred, y_test)\nConfusionMatrixDisplay(cm).plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:06.285012Z","iopub.execute_input":"2023-02-02T15:55:06.285963Z","iopub.status.idle":"2023-02-02T15:55:06.918538Z","shell.execute_reply.started":"2023-02-02T15:55:06.285922Z","shell.execute_reply":"2023-02-02T15:55:06.916910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Predict test dataset:\nY_pred = rs.predict(data_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:06.920417Z","iopub.execute_input":"2023-02-02T15:55:06.921221Z","iopub.status.idle":"2023-02-02T15:55:11.448866Z","shell.execute_reply.started":"2023-02-02T15:55:06.921168Z","shell.execute_reply":"2023-02-02T15:55:11.447411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Submission\nsubmission = pd.concat([pd.Series(range(1,28001),name = 'ImageId'),pd.Series(Y_pred, name = 'Label')],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-02T15:55:11.450437Z","iopub.execute_input":"2023-02-02T15:55:11.451667Z","iopub.status.idle":"2023-02-02T15:55:11.496785Z","shell.execute_reply.started":"2023-02-02T15:55:11.451579Z","shell.execute_reply":"2023-02-02T15:55:11.495487Z"},"trusted":true},"execution_count":null,"outputs":[]}]}