{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-24T04:53:36.025985Z","iopub.execute_input":"2021-11-24T04:53:36.02631Z","iopub.status.idle":"2021-11-24T04:53:36.054837Z","shell.execute_reply.started":"2021-11-24T04:53:36.026226Z","shell.execute_reply":"2021-11-24T04:53:36.053935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#8. Overall final prediction performance\n\nprint(\"Overall Performance Prediction:\");\nprint(\"Sensitivity: {0}%\".format(round(mean(store_sens)*100, 4)));\nprint(\"Specificity: {0}%\".format(round(mean(store_spec)*100, 4)));\nprint(\"Accuracy: {0}%\".format(round(mean(store_acc)*100, 4)));\nprint(\"Balanced Accuracy: {0}%\".format(round(mean(store_bal_acc)*100, 4)));\nprint(\"Precision: {0}%\".format(round(mean(store_prec)*100, 4)));\nprint(\"F1-Score: {0}%\".format(round(mean(store_fscore)*100, 4)))\nprint(\"MCC: {0}\\n\".format(round(mean(store_mcc), 4)))","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.056651Z","iopub.execute_input":"2021-11-24T04:53:36.05713Z","iopub.status.idle":"2021-11-24T04:53:36.128044Z","shell.execute_reply.started":"2021-11-24T04:53:36.057089Z","shell.execute_reply":"2021-11-24T04:53:36.126541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2. load testing and training dataset\n\ndf_train = pd.read_csv(\"../input/digit-recognizer/train.csv\",header=0)\ndf_test = pd.read_csv(\"../input/digit-recognizer/test.csv\",header=0)\ndf_sample_submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\",header=0)\n\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.129309Z","iopub.status.idle":"2021-11-24T04:53:36.129947Z","shell.execute_reply.started":"2021-11-24T04:53:36.129677Z","shell.execute_reply":"2021-11-24T04:53:36.129705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_label = set(df_train['label'].tolist())\nprint(\"All labels: {0}\".format(all_label))\n\nall_label_sub = set(df_sample_submission['Label'].tolist())\nprint(\" 'sample_submission.csv' pnly has one class: {0}\".format(all_label_sub))\n\ndst_train = Counter(df_train['label'])\nprint(dst_train)\n\nsns.countplot(df_train['label'])","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.131252Z","iopub.status.idle":"2021-11-24T04:53:36.131894Z","shell.execute_reply.started":"2021-11-24T04:53:36.131616Z","shell.execute_reply":"2021-11-24T04:53:36.131642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#3. Share to test and train data \nx = df_train.iloc[:, 1:]\ny = df_train['label'].tolist()\n\nx_test = x.iloc[0:10000, :].values.astype('float32')\ny_test = y[0:10000]\nx_train = x.iloc[10000:, :].values.astype('float32')\ny_train = y[10000:]\n\n#df_train.iloc[0:1, 1:].values","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.133085Z","iopub.status.idle":"2021-11-24T04:53:36.1337Z","shell.execute_reply.started":"2021-11-24T04:53:36.133436Z","shell.execute_reply":"2021-11-24T04:53:36.133461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#4. Previes 10 image data\n\n%matplotlib inline\nplt.figure(figsize=(12,10))\nx_cor, y_cor = 4, 4\nfor i in range(10):\n    arr_img = np.asarray(x_train[i].reshape((28,28)));\n    plt.subplot(y_cor, x_cor, i+1)\n    plt.imshow(arr_img, cmap='gray')\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.134891Z","iopub.status.idle":"2021-11-24T04:53:36.13549Z","shell.execute_reply.started":"2021-11-24T04:53:36.135237Z","shell.execute_reply":"2021-11-24T04:53:36.135263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#5. Feature normalization for both (test & train)\n\nprint((min(x_train[2]), max(x_train[2])))\n\nx_train = x_train/255.0\nx_test = x_test/255.0\n\nprint('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.136675Z","iopub.status.idle":"2021-11-24T04:53:36.137295Z","shell.execute_reply.started":"2021-11-24T04:53:36.137045Z","shell.execute_reply":"2021-11-24T04:53:36.13707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#6. Train random forest algorithm\n#model_clf = RandomForestClassifier(n_estimators=500, max_depth=2, random_state=0)\n#model_clf.fit(x_train, y_train)\n#y_pred = model_clf.predict(x_test)\n\nmodel_clf = Perceptron(max_iter=6000, eta0=100, random_state=0)\nmodel_clf.fit(x_train, y_train)\ny_pred = model_clf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.143343Z","iopub.execute_input":"2021-11-24T04:53:36.143556Z","iopub.status.idle":"2021-11-24T04:53:36.156718Z","shell.execute_reply.started":"2021-11-24T04:53:36.143532Z","shell.execute_reply":"2021-11-24T04:53:36.15577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#7. Multiclass confusion matrix for each class\n\nlst_actual_class = y_test\nlst_predicted_class = y_pred\n\nlst_classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\narr_out_matrix = multilabel_confusion_matrix(lst_actual_class, lst_predicted_class, labels=lst_classes)\n\nstore_sens = [];\nstore_spec = [];\nstore_acc = [];\nstore_bal_acc = [];\nstore_prec = [];\nstore_fscore = [];\nstore_mcc = [];\n\nfor no_class in range(len(lst_classes)):\n    arr_data = arr_out_matrix[no_class];\n    print(\"Predicted performance of digit label/class: {0}\".format(no_class));\n    \n    tp = arr_data[1][1]\n    fp = arr_data[0][1]\n    tn = arr_data[0][0]\n    fn = arr_data[1][0]\n    \n    sensitivity = round(tp/(tp+fn), 3);\n    specificity = round(tn/(tn+fp), 3);\n    accuracy = round((tp+tn)/(tp+fp+tn+fn), 3);\n    balanced_accuracy = round((sensitivity+specificity)/2, 3);\n    precision = round(tp/(tp+fp), 3)\n    fscore = round((2 * ((precision * sensitivity) / (precision + sensitivity))), 3)\n    mcc = round((((tp * tn)-(fp * fn))/ math.sqrt((tp + fp)*(tp + fn)*(tn + fp)*(tn + fn))),3)\n\n    store_sens.append(sensitivity);\n    store_spec.append(specificity);\n    store_acc.append(accuracy);\n    store_bal_acc.append(balanced_accuracy);\n    store_prec.append(precision);\n    store_fscore.append(fscore);\n    store_mcc.append(mcc);\n    \n    print(\"TP={0}, FP={1}, TN={2}, FN={3}\".format(tp, fp, tn, fn));\n    print(\"Sensitivity: {0}\".format(sensitivity));\n    print(\"Specificity: {0}\".format(specificity));\n    print(\"Accuracy: {0}\".format(accuracy));\n    print(\"Balanced Accuracy: {0}\".format(balanced_accuracy));\n    print(\"Precision: {0}\".format(precision));\n    print(\"F1-Score: {0}\".format(fscore))\n    print(\"MCC: {0}\\n\".format(mcc))","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.213155Z","iopub.execute_input":"2021-11-24T04:53:36.213814Z","iopub.status.idle":"2021-11-24T04:53:36.236946Z","shell.execute_reply.started":"2021-11-24T04:53:36.213776Z","shell.execute_reply":"2021-11-24T04:53:36.235456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#8. Overall final prediction performance\n\nprint(\"Overall Performance Prediction:\");\nprint(\"Sensitivity: {0}%\".format(round(mean(store_sens)*100, 4)));\nprint(\"Specificity: {0}%\".format(round(mean(store_spec)*100, 4)));\nprint(\"Accuracy: {0}%\".format(round(mean(store_acc)*100, 4)));\nprint(\"Balanced Accuracy: {0}%\".format(round(mean(store_bal_acc)*100, 4)));\nprint(\"Precision: {0}%\".format(round(mean(store_prec)*100, 4)));\nprint(\"F1-Score: {0}%\".format(round(mean(store_fscore)*100, 4)))\nprint(\"MCC: {0}\\n\".format(round(mean(store_mcc), 4)))","metadata":{"execution":{"iopub.status.busy":"2021-11-24T04:53:36.237831Z","iopub.status.idle":"2021-11-24T04:53:36.238502Z","shell.execute_reply.started":"2021-11-24T04:53:36.238307Z","shell.execute_reply":"2021-11-24T04:53:36.238329Z"},"trusted":true},"execution_count":null,"outputs":[]}]}