{"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":"##########################################################\n# 1. IMPORT ALL PACKAGES\n##########################################################\nimport pandas as pd\nimport numpy as np\nfrom sklearn.linear_model import Perceptron\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport math\nfrom collections import Counter\nimport seaborn as sns\nimport matplotlib.pyplot as plt #for plotting\nfrom sklearn.svm import SVC\nfrom sklearn import svm\nfrom sklearn.metrics import multilabel_confusion_matrix\n# importing mean() \nfrom statistics import mean ","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-06-23T01:38:12.627023Z","iopub.execute_input":"2022-06-23T01:38:12.627660Z","iopub.status.idle":"2022-06-23T01:38:13.814011Z","shell.execute_reply.started":"2022-06-23T01:38:12.627615Z","shell.execute_reply":"2022-06-23T01:38:13.812869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 2. LOAD TESTING AND TRAINING DATASET\n##########################################################\ndf_train = pd.read_csv(\"../input/digit-recognizer/train.csv\",header=0)# header 0 means the first row is name of the coloumn \ndf_test = pd.read_csv(\"../input/digit-recognizer/test.csv\",header=0)# header 0 means the first row is name of the coloumn \ndf_sample_submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\",header=0)# header 0 means the first row is name of the coloumn \n\n# View sample data\ndf_train.head(10) ","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:13.816352Z","iopub.execute_input":"2022-06-23T01:38:13.816716Z","iopub.status.idle":"2022-06-23T01:38:20.123724Z","shell.execute_reply.started":"2022-06-23T01:38:13.816673Z","shell.execute_reply":"2022-06-23T01:38:20.122815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check all classes/labels in trainiing data\nall_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' only has one class: {0}\".format(all_label_sub))\n\n# Data distribution for each class\ndst_train = Counter(df_train['label'])\nprint(dst_train)\n\n# Plot distribution\nsns.countplot(df_train['label'])","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:20.125156Z","iopub.execute_input":"2022-06-23T01:38:20.125455Z","iopub.status.idle":"2022-06-23T01:38:20.349835Z","shell.execute_reply.started":"2022-06-23T01:38:20.125424Z","shell.execute_reply":"2022-06-23T01:38:20.348910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 3. SHARE TO TEST AND TRAIN DATA\n##########################################################\n# We share train data because this \"sample_submission.csv\" only has one class/label\nx = df_train.iloc[:, 1:]\ny = df_train['label'].tolist()\n\n# # Select 10000 rows data as a testing dataset\nx_test = x.iloc[0:10000, :].values.astype('float32') # all pixel values \ny_test = y[0:10000] # Select label for testing data\nx_train = x.iloc[10000:, :].values.astype('float32') # all pixel values \ny_train = y[10000:]\n\n# View one row data\n#df_train.iloc[0:1, 1:].values","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:20.351468Z","iopub.execute_input":"2022-06-23T01:38:20.351858Z","iopub.status.idle":"2022-06-23T01:38:20.429937Z","shell.execute_reply.started":"2022-06-23T01:38:20.351827Z","shell.execute_reply":"2022-06-23T01:38:20.429027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n##########################################################\n# 4. PREVIEW 10 IMAGE DATA\n##########################################################\n%matplotlib inline\n# 10 selected rows\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, interpolation='nearest')\n    plt.imshow(arr_img, cmap='gray') # Displaying a grayscale image\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:20.433108Z","iopub.execute_input":"2022-06-23T01:38:20.433549Z","iopub.status.idle":"2022-06-23T01:38:21.572567Z","shell.execute_reply.started":"2022-06-23T01:38:20.433503Z","shell.execute_reply":"2022-06-23T01:38:21.571695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 5. FEATURE NORMALIZATION FOR BOTH (TEST & TRAIN)\n##########################################################\n# Proceed to normalize the features because the pixel intensities are currently between the range of 0 and 255\nprint((min(x_train[2]), max(x_train[2])))\n\n# Normalizing the data\nx_train = x_train/255.0\nx_test = x_test/255.0\n\n# Printing the shape of the Datasets\nprint('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')\n","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:21.573893Z","iopub.execute_input":"2022-06-23T01:38:21.574175Z","iopub.status.idle":"2022-06-23T01:38:21.629407Z","shell.execute_reply.started":"2022-06-23T01:38:21.574147Z","shell.execute_reply":"2022-06-23T01:38:21.628507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 6. TRAIN USING SUPPORT VECTOR MACHINE ALGORITHM\n##########################################################\n# SVM Parameters\n# Parameter kernel : {‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’} or callable, default=’rbf’\n# Parameter C : float, default=1.0\n# Paramter gamma: gamma{‘scale’, ‘auto’} or float, default=’scale’\n# Parameter probability: bool, default=False\n\n#model_clf = svm.SVC(kernel='linear') # Linear kernel\n#model_clf = svm.SVC(kernel='rbf', gamma=0.1, C=1.0, probability=False) # Rbf kernel\nmodel_clf = svm.SVC(kernel='linear', gamma=0.1, C=1.0, probability=False) # Rbf kernel\n\n# Train the Random Forest algorithm\nmodel_clf.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:38:21.631077Z","iopub.execute_input":"2022-06-23T01:38:21.631474Z","iopub.status.idle":"2022-06-23T01:41:04.691441Z","shell.execute_reply.started":"2022-06-23T01:38:21.631430Z","shell.execute_reply":"2022-06-23T01:41:04.689790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 6. APPLY THE TRAINED LEARNER TO TEST NEW DATA\n##########################################################\n# Apply the trained perceptron to make prediction of test data\ny_pred = model_clf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:41:04.692701Z","iopub.execute_input":"2022-06-23T01:41:04.693069Z","iopub.status.idle":"2022-06-23T01:42:24.066242Z","shell.execute_reply.started":"2022-06-23T01:41:04.693034Z","shell.execute_reply":"2022-06-23T01:42:24.065317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 7. MULTI-CLASS CONFUSION MATRIX FOR EACH CLASS\n##########################################################\n# Actual and predicted classes\nlst_actual_class = y_test\nlst_predicted_class = y_pred\n\n# Class = digit labels 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\nlst_classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Must in order\n\n# Compute multi-class confusion matrix\narr_out_matrix = multilabel_confusion_matrix(lst_actual_class, lst_predicted_class, labels=lst_classes)\n\n# Temp store results\nstore_sens = [];\nstore_spec = [];\nstore_acc = [];\nstore_bal_acc = [];\nstore_prec = [];\nstore_fscore = [];\nstore_mcc = [];\n\n# Loop for each taget label\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":"2022-06-23T01:42:24.067378Z","iopub.execute_input":"2022-06-23T01:42:24.067822Z","iopub.status.idle":"2022-06-23T01:42:24.108466Z","shell.execute_reply.started":"2022-06-23T01:42:24.067777Z","shell.execute_reply":"2022-06-23T01:42:24.107596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 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)))\n","metadata":{"execution":{"iopub.status.busy":"2022-06-23T01:42:24.109557Z","iopub.execute_input":"2022-06-23T01:42:24.109852Z","iopub.status.idle":"2022-06-23T01:42:24.126860Z","shell.execute_reply.started":"2022-06-23T01:42:24.109824Z","shell.execute_reply":"2022-06-23T01:42:24.125047Z"},"trusted":true},"execution_count":null,"outputs":[]}]}