{"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 multilabel_confusion_matrix\nfrom math import sqrt\nfrom statistics import mean","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":1.048586,"end_time":"2021-11-14T21:04:49.173156","exception":false,"start_time":"2021-11-14T21:04:48.124570","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:28.938554Z","iopub.execute_input":"2021-11-24T13:39:28.938957Z","iopub.status.idle":"2021-11-24T13:39:28.946440Z","shell.execute_reply.started":"2021-11-24T13:39:28.938920Z","shell.execute_reply":"2021-11-24T13:39:28.944144Z"},"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":{"papermill":{"duration":5.785082,"end_time":"2021-11-14T21:04:54.966164","exception":false,"start_time":"2021-11-14T21:04:49.181082","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:28.948162Z","iopub.execute_input":"2021-11-24T13:39:28.949768Z","iopub.status.idle":"2021-11-24T13:39:33.343185Z","shell.execute_reply.started":"2021-11-24T13:39:28.949714Z","shell.execute_reply":"2021-11-24T13:39:33.342333Z"},"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:]","metadata":{"papermill":{"duration":0.086915,"end_time":"2021-11-14T21:04:55.060690","exception":false,"start_time":"2021-11-14T21:04:54.973775","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:33.344952Z","iopub.execute_input":"2021-11-24T13:39:33.345499Z","iopub.status.idle":"2021-11-24T13:39:33.421438Z","shell.execute_reply.started":"2021-11-24T13:39:33.345445Z","shell.execute_reply":"2021-11-24T13:39:33.420514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 4. TRAIN PERCEPTRON LEARNING ALGORITHM\n##########################################################\n# Create a perceptron object with the parameters over the data\nmodel_clf = Perceptron(max_iter=2000000, eta0=0.1, random_state=0)\n\n# Train the perceptron\nmodel_clf.fit(x_train, y_train)","metadata":{"papermill":{"duration":7.384921,"end_time":"2021-11-14T21:05:02.453000","exception":false,"start_time":"2021-11-14T21:04:55.068079","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:33.422419Z","iopub.execute_input":"2021-11-24T13:39:33.422650Z","iopub.status.idle":"2021-11-24T13:39:40.797494Z","shell.execute_reply.started":"2021-11-24T13:39:33.422622Z","shell.execute_reply":"2021-11-24T13:39:40.796612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 5. 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":{"papermill":{"duration":0.084856,"end_time":"2021-11-14T21:05:02.545697","exception":false,"start_time":"2021-11-14T21:05:02.460841","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:40.799666Z","iopub.execute_input":"2021-11-24T13:39:40.799896Z","iopub.status.idle":"2021-11-24T13:39:40.869271Z","shell.execute_reply.started":"2021-11-24T13:39:40.799867Z","shell.execute_reply":"2021-11-24T13:39:40.868271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##########################################################\n# 6. CONFUSION MATRIX\n##########################################################\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# Temp store results\nstore_sens = [];\nstore_spec = [];\nstore_acc = [];\nstore_bal_acc = [];\nstore_prec = [];\nstore_fscore = [];\nstore_mcc = [];\nfor no_class in range(len(lst_classes)):\n    arr_data = arr_out_matrix[no_class];\n    print(\"Print 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    \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    \n    precision = round(tp/(tp+fp), 3);\n    f1Score = round((2*tp/(2*tp + fp + fn)), 3);\n    \n    x = (tp+fp) * (tp+fn) * (tn+fp) * (tn+fn)\n    MCC = round(((tp * tn) - (fp * fn)) / sqrt(x), 3)\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(f1Score);\n    store_mcc.append(MCC);\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    \n    print(\"Precision: {0}\".format(precision));\n    print(\"F1-Score: {0}\".format(f1Score));\n    print(\"MCC: {0}\\n\".format(MCC));# 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","metadata":{"papermill":{"duration":0.082574,"end_time":"2021-11-14T21:05:02.642175","exception":false,"start_time":"2021-11-14T21:05:02.559601","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-24T13:39:40.871011Z","iopub.execute_input":"2021-11-24T13:39:40.872007Z","iopub.status.idle":"2021-11-24T13:39:40.936216Z","shell.execute_reply.started":"2021-11-24T13:39:40.871950Z","shell.execute_reply":"2021-11-24T13:39:40.935235Z"},"trusted":true},"execution_count":null,"outputs":[]}]}