{"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":"# Plot a Confusion Matrix\n\nI find it helpful to see how well a classifier is doing by plotting a confusion matrix. This function produces both 'regular' and normalized confusion matrices.","metadata":{"_cell_guid":"ec96b202-b504-a30d-7690-b456905dd198"}},{"cell_type":"code","source":"import numpy as np\n\n\ndef plot_confusion_matrix(cm,\n                          target_names,\n                          title='Confusion matrix',\n                          cmap=None,\n                          normalize=True):\n    \"\"\"\n    given a sklearn confusion matrix (cm), make a nice plot\n\n    Arguments\n    ---------\n    cm:           confusion matrix from sklearn.metrics.confusion_matrix\n\n    target_names: given classification classes such as [0, 1, 2]\n                  the class names, for example: ['high', 'medium', 'low']\n\n    title:        the text to display at the top of the matrix\n\n    cmap:         the gradient of the values displayed from matplotlib.pyplot.cm\n                  see http://matplotlib.org/examples/color/colormaps_reference.html\n                  plt.get_cmap('jet') or plt.cm.Blues\n\n    normalize:    If False, plot the raw numbers\n                  If True, plot the proportions\n\n    Usage\n    -----\n    plot_confusion_matrix(cm           = cm,                  # confusion matrix created by\n                                                              # sklearn.metrics.confusion_matrix\n                          normalize    = True,                # show proportions\n                          target_names = y_labels_vals,       # list of names of the classes\n                          title        = best_estimator_name) # title of graph\n\n    Citiation\n    ---------\n    http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html\n\n    \"\"\"\n    import matplotlib.pyplot as plt\n    import numpy as np\n    import itertools\n\n    accuracy = np.trace(cm) / float(np.sum(cm))\n    misclass = 1 - accuracy\n\n    if cmap is None:\n        cmap = plt.get_cmap('Blues')\n\n    plt.figure(figsize=(8, 6))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n\n    if target_names is not None:\n        tick_marks = np.arange(len(target_names))\n        plt.xticks(tick_marks, target_names, rotation=45)\n        plt.yticks(tick_marks, target_names)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n\n    thresh = cm.max() / 1.5 if normalize else cm.max() / 2\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        if normalize:\n            plt.text(j, i, \"{:0.4f}\".format(cm[i, j]),\n                     horizontalalignment=\"center\",\n                     color=\"white\" if cm[i, j] > thresh else \"black\")\n        else:\n            plt.text(j, i, \"{:,}\".format(cm[i, j]),\n                     horizontalalignment=\"center\",\n                     color=\"white\" if cm[i, j] > thresh else \"black\")\n\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label\\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))\n    plt.show()","metadata":{"_cell_guid":"35eff6dc-5645-ac99-ff4e-98f9dc8c305b","execution":{"iopub.status.busy":"2021-11-15T05:35:15.064346Z","iopub.execute_input":"2021-11-15T05:35:15.065516Z","iopub.status.idle":"2021-11-15T05:35:15.105207Z","shell.execute_reply.started":"2021-11-15T05:35:15.065384Z","shell.execute_reply":"2021-11-15T05:35:15.104376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(cm           = np.array([[350,0,0,0,0,0,0,0],\n[0,334,0,8,0,8,0,0],\n[0,0,342,0,5,0,3,0],\n[0,5,0,330,0,7,0,8],\n[0,0,2,0,342,0,6,0],\n[0,2,0,2,0,344,1,1],\n[0,0,0,0,1,0,349,0],\n[0,8,0,5,0,6,0,331],\n]), \n                      normalize    = False,\n                      target_names = ['AMD', 'CNV', 'CSR', 'DME', 'DR', 'DRUSEN', 'MH', 'NORMAL'],\n                      title        = \"Confusion Matrix\")","metadata":{"_cell_guid":"0dcab899-c365-9e50-61aa-e7fb8812a37c","execution":{"iopub.status.busy":"2021-11-15T05:35:15.107152Z","iopub.execute_input":"2021-11-15T05:35:15.107925Z","iopub.status.idle":"2021-11-15T05:35:15.746084Z","shell.execute_reply.started":"2021-11-15T05:35:15.107880Z","shell.execute_reply":"2021-11-15T05:35:15.745193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(cm           = np.array([[350,0,0,0,0,0,0,0],\n[0,334,0,8,0,8,0,0],\n[0,0,342,0,5,0,3,0],\n[0,5,0,330,0,7,0,8],\n[0,0,2,0,342,0,6,0],\n[0,2,0,2,0,344,1,1],\n[0,0,0,0,1,0,349,0],\n[0,8,0,5,0,6,0,331],\n]), \n                      normalize    = True,\n                      target_names = ['AMD', 'CNV', 'CSR', 'DME', 'DR', 'DRUSEN', 'MH', 'NORMAL'],\n                      title        = \"Confusion Matrix, Normalized\")","metadata":{"_cell_guid":"09ebd590-50f7-f5ce-4f86-89c7e2fd0994","execution":{"iopub.status.busy":"2021-11-15T05:35:15.747631Z","iopub.execute_input":"2021-11-15T05:35:15.747855Z","iopub.status.idle":"2021-11-15T05:35:16.408087Z","shell.execute_reply.started":"2021-11-15T05:35:15.747826Z","shell.execute_reply":"2021-11-15T05:35:16.407150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_cell_guid":"8c4295de-8b14-4fef-d04a-412743c7a51c"},"execution_count":null,"outputs":[]}]}