nodes: 0, module; 1, function_definition; 2, function_name:plot_confusion_matrix; 3, parameters; 4, block; 5, identifier:y_true; 6, identifier:y_pred; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, default_parameter; 13, default_parameter; 14, default_parameter; 15, default_parameter; 16, default_parameter; 17, default_parameter; 18, default_parameter; 19, expression_statement; 20, if_statement; 21, expression_statement; 22, if_statement; 23, if_statement; 24, if_statement; 25, if_statement; 26, if_statement; 27, expression_statement; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, expression_statement; 34, expression_statement; 35, expression_statement; 36, for_statement; 37, expression_statement; 38, expression_statement; 39, expression_statement; 40, return_statement; 41, identifier:labels; 42, None; 43, identifier:true_labels; 44, None; 45, identifier:pred_labels; 46, None; 47, identifier:title; 48, None; 49, identifier:normalize; 50, False; 51, identifier:hide_zeros; 52, False; 53, identifier:x_tick_rotation; 54, integer:0; 55, identifier:ax; 56, None; 57, identifier:figsize; 58, None; 59, identifier:cmap; 60, string; 61, identifier:title_fontsize; 62, string:"large"; 63, identifier:text_fontsize; 64, string:"medium"; 65, comment:"""Generates confusion matrix plot from predictions and true labels
  
      Args:
          y_true (array-like, shape (n_samples)):
              Ground truth (correct) target values.
  
          y_pred (array-like, shape (n_samples)):
              Estimated targets as returned by a classifier.
  
          labels (array-like, shape (n_classes), optional): List of labels to
              index the matrix. This may be used to reorder or select a subset
              of labels. If none is given, those that appear at least once in
              ``y_true`` or ``y_pred`` are used in sorted order. (new in v0.2.5)
  
          true_labels (array-like, optional): The true labels to display.
              If none is given, then all of the labels are used.
  
          pred_labels (array-like, optional): The predicted labels to display.
              If none is given, then all of the labels are used.
  
          title (string, optional): Title of the generated plot. Defaults to
              "Confusion Matrix" if `normalize` is True. Else, defaults to
              "Normalized Confusion Matrix.
  
          normalize (bool, optional): If True, normalizes the confusion matrix
              before plotting. Defaults to False.
  
          hide_zeros (bool, optional): If True, does not plot cells containing a
              value of zero. Defaults to False.
  
          x_tick_rotation (int, optional): Rotates x-axis tick labels by the
              specified angle. This is useful in cases where there are numerous
              categories and the labels overlap each other.
  
          ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to
              plot the curve. If None, the plot is drawn on a new set of axes.
  
          figsize (2-tuple, optional): Tuple denoting figure size of the plot
              e.g. (6, 6). Defaults to ``None``.
  
          cmap (string or :class:`matplotlib.colors.Colormap` instance, optional):
              Colormap used for plotting the projection. View Matplotlib Colormap
              documentation for available options.
              https://matplotlib.org/users/colormaps.html
  
          title_fontsize (string or int, optional): Matplotlib-style fontsizes.
              Use e.g. "small", "medium", "large" or integer-values. Defaults to
              "large".
  
          text_fontsize (string or int, optional): Matplotlib-style fontsizes.
              Use e.g. "small", "medium", "large" or integer-values. Defaults to
              "medium".
  
      Returns:
          ax (:class:`matplotlib.axes.Axes`): The axes on which the plot was
              drawn.
  
      Example:
          >>> import scikitplot.plotters as skplt
          >>> rf = RandomForestClassifier()
          >>> rf = rf.fit(X_train, y_train)
          >>> y_pred = rf.predict(X_test)
          >>> skplt.plot_confusion_matrix(y_test, y_pred, normalize=True)
          <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
          >>> plt.show()
  
          .. image:: _static/examples/plot_confusion_matrix.png
             :align: center
             :alt: Confusion matrix
      """; 66, comparison_operator:ax is None; 67, block; 68, assignment; 69, comparison_operator:labels is None; 70, block; 71, else_clause; 72, identifier:normalize; 73, block; 74, comparison_operator:true_labels is None; 75, block; 76, else_clause; 77, comparison_operator:pred_labels is None; 78, block; 79, else_clause; 80, identifier:title; 81, block; 82, elif_clause; 83, else_clause; 84, assignment; 85, call; 86, assignment; 87, assignment; 88, call; 89, call; 90, call; 91, call; 92, assignment; 93, pattern_list; 94, call; 95, block; 96, call; 97, call; 98, call; 99, identifier:ax; 100, string_content:Blues; 101, identifier:ax; 102, None; 103, expression_statement; 104, identifier:cm; 105, call; 106, identifier:labels; 107, None; 108, expression_statement; 109, block; 110, expression_statement; 111, expression_statement; 112, expression_statement; 113, identifier:true_labels; 114, None; 115, expression_statement; 116, block; 117, identifier:pred_labels; 118, None; 119, expression_statement; 120, block; 121, expression_statement; 122, identifier:normalize; 123, block; 124, block; 125, identifier:image; 126, call; 127, attribute; 128, argument_list; 129, identifier:x_tick_marks; 130, call; 131, identifier:y_tick_marks; 132, call; 133, attribute; 134, argument_list; 135, attribute; 136, argument_list; 137, attribute; 138, argument_list; 139, attribute; 140, argument_list; 141, identifier:thresh; 142, binary_operator:cm.max() / 2.; 143, identifier:i; 144, identifier:j; 145, attribute; 146, argument_list; 147, if_statement; 148, attribute; 149, argument_list; 150, attribute; 151, argument_list; 152, attribute; 153, argument_list; 154, assignment; 155, identifier:confusion_matrix; 156, argument_list; 157, assignment; 158, expression_statement; 159, assignment; 160, assignment; 161, assignment; 162, assignment; 163, expression_statement; 164, expression_statement; 165, expression_statement; 166, expression_statement; 167, assignment; 168, expression_statement; 169, expression_statement; 170, expression_statement; 171, expression_statement; 172, call; 173, expression_statement; 174, expression_statement; 175, attribute; 176, argument_list; 177, identifier:plt; 178, identifier:colorbar; 179, keyword_argument; 180, attribute; 181, argument_list; 182, attribute; 183, argument_list; 184, identifier:ax; 185, identifier:set_xticks; 186, identifier:x_tick_marks; 187, identifier:ax; 188, identifier:set_xticklabels; 189, identifier:pred_classes; 190, keyword_argument; 191, keyword_argument; 192, identifier:ax; 193, identifier:set_yticks; 194, identifier:y_tick_marks; 195, identifier:ax; 196, identifier:set_yticklabels; 197, identifier:true_classes; 198, keyword_argument; 199, call; 200, float:2.; 201, identifier:itertools; 202, identifier:product; 203, call; 204, call; 205, not_operator; 206, block; 207, identifier:ax; 208, identifier:set_ylabel; 209, string; 210, keyword_argument; 211, identifier:ax; 212, identifier:set_xlabel; 213, string; 214, keyword_argument; 215, identifier:ax; 216, identifier:grid; 217, string; 218, pattern_list; 219, call; 220, identifier:y_true; 221, identifier:y_pred; 222, keyword_argument; 223, identifier:classes; 224, call; 225, assignment; 226, identifier:cm; 227, binary_operator:cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]; 228, identifier:cm; 229, call; 230, subscript; 231, float:0.0; 232, identifier:true_classes; 233, identifier:classes; 234, call; 235, assignment; 236, assignment; 237, assignment; 238, identifier:pred_classes; 239, identifier:classes; 240, call; 241, assignment; 242, assignment; 243, assignment; 244, attribute; 245, argument_list; 246, call; 247, call; 248, identifier:ax; 249, identifier:imshow; 250, identifier:cm; 251, keyword_argument; 252, keyword_argument; 253, identifier:mappable; 254, identifier:image; 255, identifier:np; 256, identifier:arange; 257, call; 258, identifier:np; 259, identifier:arange; 260, call; 261, identifier:fontsize; 262, identifier:text_fontsize; 263, identifier:rotation; 264, identifier:x_tick_rotation; 265, identifier:fontsize; 266, identifier:text_fontsize; 267, attribute; 268, argument_list; 269, identifier:range; 270, argument_list; 271, identifier:range; 272, argument_list; 273, parenthesized_expression; 274, expression_statement; 275, string_content:True label; 276, identifier:fontsize; 277, identifier:text_fontsize; 278, string_content:Predicted label; 279, identifier:fontsize; 280, identifier:text_fontsize; 281, string_content:off; 282, identifier:fig; 283, identifier:ax; 284, attribute; 285, argument_list; 286, identifier:labels; 287, identifier:labels; 288, identifier:unique_labels; 289, argument_list; 290, identifier:classes; 291, call; 292, call; 293, subscript; 294, attribute; 295, argument_list; 296, identifier:cm; 297, call; 298, identifier:validate_labels; 299, argument_list; 300, identifier:true_label_indexes; 301, call; 302, identifier:true_classes; 303, subscript; 304, identifier:cm; 305, subscript; 306, identifier:validate_labels; 307, argument_list; 308, identifier:pred_label_indexes; 309, call; 310, identifier:pred_classes; 311, subscript; 312, identifier:cm; 313, subscript; 314, identifier:ax; 315, identifier:set_title; 316, identifier:title; 317, keyword_argument; 318, attribute; 319, argument_list; 320, attribute; 321, argument_list; 322, identifier:interpolation; 323, string; 324, identifier:cmap; 325, call; 326, identifier:len; 327, argument_list; 328, identifier:len; 329, argument_list; 330, identifier:cm; 331, identifier:max; 332, subscript; 333, subscript; 334, boolean_operator; 335, call; 336, identifier:plt; 337, identifier:subplots; 338, integer:1; 339, integer:1; 340, keyword_argument; 341, identifier:y_true; 342, identifier:y_pred; 343, attribute; 344, argument_list; 345, attribute; 346, argument_list; 347, call; 348, slice; 349, attribute; 350, identifier:np; 351, identifier:around; 352, identifier:cm; 353, keyword_argument; 354, attribute; 355, argument_list; 356, identifier:classes; 357, identifier:true_labels; 358, string:"true_labels"; 359, attribute; 360, argument_list; 361, identifier:classes; 362, identifier:true_label_indexes; 363, identifier:cm; 364, identifier:true_label_indexes; 365, identifier:classes; 366, identifier:pred_labels; 367, string:"pred_labels"; 368, attribute; 369, argument_list; 370, identifier:classes; 371, identifier:pred_label_indexes; 372, identifier:cm; 373, slice; 374, identifier:pred_label_indexes; 375, identifier:fontsize; 376, identifier:title_fontsize; 377, identifier:ax; 378, identifier:set_title; 379, string; 380, keyword_argument; 381, identifier:ax; 382, identifier:set_title; 383, string; 384, keyword_argument; 385, string_content:nearest; 386, attribute; 387, argument_list; 388, identifier:pred_classes; 389, identifier:true_classes; 390, attribute; 391, integer:0; 392, attribute; 393, integer:1; 394, identifier:hide_zeros; 395, comparison_operator:cm[i, j] == 0; 396, attribute; 397, argument_list; 398, identifier:figsize; 399, identifier:figsize; 400, identifier:np; 401, identifier:asarray; 402, identifier:labels; 403, identifier:cm; 404, identifier:astype; 405, string; 406, attribute; 407, argument_list; 408, identifier:np; 409, identifier:newaxis; 410, identifier:decimals; 411, integer:2; 412, identifier:np; 413, identifier:isnan; 414, identifier:cm; 415, identifier:np; 416, identifier:in1d; 417, identifier:classes; 418, identifier:true_labels; 419, identifier:np; 420, identifier:in1d; 421, identifier:classes; 422, identifier:pred_labels; 423, string_content:Normalized Confusion Matrix; 424, identifier:fontsize; 425, identifier:title_fontsize; 426, string_content:Confusion Matrix; 427, identifier:fontsize; 428, identifier:title_fontsize; 429, attribute; 430, identifier:get_cmap; 431, identifier:cmap; 432, identifier:cm; 433, identifier:shape; 434, identifier:cm; 435, identifier:shape; 436, subscript; 437, integer:0; 438, identifier:ax; 439, identifier:text; 440, identifier:j; 441, identifier:i; 442, subscript; 443, keyword_argument; 444, keyword_argument; 445, keyword_argument; 446, keyword_argument; 447, string_content:float; 448, identifier:cm; 449, identifier:sum; 450, keyword_argument; 451, identifier:plt; 452, identifier:cm; 453, identifier:cm; 454, identifier:i; 455, identifier:j; 456, identifier:cm; 457, identifier:i; 458, identifier:j; 459, identifier:horizontalalignment; 460, string:"center"; 461, identifier:verticalalignment; 462, string:"center"; 463, identifier:fontsize; 464, identifier:text_fontsize; 465, identifier:color; 466, conditional_expression:"white" if cm[i, j] > thresh else "black"; 467, identifier:axis; 468, integer:1; 469, string:"white"; 470, comparison_operator:cm[i, j] > thresh; 471, string:"black"; 472, subscript; 473, identifier:thresh; 474, identifier:cm; 475, identifier:i; 476, identifier:j
code: def plot_confusion_matrix(y_true, y_pred, labels=None, true_labels=None,
                            pred_labels=None, title=None, normalize=False,
                            hide_zeros=False, x_tick_rotation=0, ax=None,
                            figsize=None, cmap='Blues', title_fontsize="large",
                            text_fontsize="medium"):
      """Generates confusion matrix plot from predictions and true labels
  
      Args:
          y_true (array-like, shape (n_samples)):
              Ground truth (correct) target values.
  
          y_pred (array-like, shape (n_samples)):
              Estimated targets as returned by a classifier.
  
          labels (array-like, shape (n_classes), optional): List of labels to
              index the matrix. This may be used to reorder or select a subset
              of labels. If none is given, those that appear at least once in
              ``y_true`` or ``y_pred`` are used in sorted order. (new in v0.2.5)
  
          true_labels (array-like, optional): The true labels to display.
              If none is given, then all of the labels are used.
  
          pred_labels (array-like, optional): The predicted labels to display.
              If none is given, then all of the labels are used.
  
          title (string, optional): Title of the generated plot. Defaults to
              "Confusion Matrix" if `normalize` is True. Else, defaults to
              "Normalized Confusion Matrix.
  
          normalize (bool, optional): If True, normalizes the confusion matrix
              before plotting. Defaults to False.
  
          hide_zeros (bool, optional): If True, does not plot cells containing a
              value of zero. Defaults to False.
  
          x_tick_rotation (int, optional): Rotates x-axis tick labels by the
              specified angle. This is useful in cases where there are numerous
              categories and the labels overlap each other.
  
          ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to
              plot the curve. If None, the plot is drawn on a new set of axes.
  
          figsize (2-tuple, optional): Tuple denoting figure size of the plot
              e.g. (6, 6). Defaults to ``None``.
  
          cmap (string or :class:`matplotlib.colors.Colormap` instance, optional):
              Colormap used for plotting the projection. View Matplotlib Colormap
              documentation for available options.
              https://matplotlib.org/users/colormaps.html
  
          title_fontsize (string or int, optional): Matplotlib-style fontsizes.
              Use e.g. "small", "medium", "large" or integer-values. Defaults to
              "large".
  
          text_fontsize (string or int, optional): Matplotlib-style fontsizes.
              Use e.g. "small", "medium", "large" or integer-values. Defaults to
              "medium".
  
      Returns:
          ax (:class:`matplotlib.axes.Axes`): The axes on which the plot was
              drawn.
  
      Example:
          >>> import scikitplot.plotters as skplt
          >>> rf = RandomForestClassifier()
          >>> rf = rf.fit(X_train, y_train)
          >>> y_pred = rf.predict(X_test)
          >>> skplt.plot_confusion_matrix(y_test, y_pred, normalize=True)
          <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
          >>> plt.show()
  
          .. image:: _static/examples/plot_confusion_matrix.png
             :align: center
             :alt: Confusion matrix
      """
      if ax is None:
          fig, ax = plt.subplots(1, 1, figsize=figsize)
  
      cm = confusion_matrix(y_true, y_pred, labels=labels)
      if labels is None:
          classes = unique_labels(y_true, y_pred)
      else:
          classes = np.asarray(labels)
  
      if normalize:
          cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
          cm = np.around(cm, decimals=2)
          cm[np.isnan(cm)] = 0.0
  
      if true_labels is None:
          true_classes = classes
      else:
          validate_labels(classes, true_labels, "true_labels")
  
          true_label_indexes = np.in1d(classes, true_labels)
  
          true_classes = classes[true_label_indexes]
          cm = cm[true_label_indexes]
  
      if pred_labels is None:
          pred_classes = classes
      else:
          validate_labels(classes, pred_labels, "pred_labels")
  
          pred_label_indexes = np.in1d(classes, pred_labels)
  
          pred_classes = classes[pred_label_indexes]
          cm = cm[:, pred_label_indexes]
  
      if title:
          ax.set_title(title, fontsize=title_fontsize)
      elif normalize:
          ax.set_title('Normalized Confusion Matrix', fontsize=title_fontsize)
      else:
          ax.set_title('Confusion Matrix', fontsize=title_fontsize)
  
      image = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.get_cmap(cmap))
      plt.colorbar(mappable=image)
      x_tick_marks = np.arange(len(pred_classes))
      y_tick_marks = np.arange(len(true_classes))
      ax.set_xticks(x_tick_marks)
      ax.set_xticklabels(pred_classes, fontsize=text_fontsize,
                         rotation=x_tick_rotation)
      ax.set_yticks(y_tick_marks)
      ax.set_yticklabels(true_classes, fontsize=text_fontsize)
  
      thresh = cm.max() / 2.
      for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
          if not (hide_zeros and cm[i, j] == 0):
              ax.text(j, i, cm[i, j],
                      horizontalalignment="center",
                      verticalalignment="center",
                      fontsize=text_fontsize,
                      color="white" if cm[i, j] > thresh else "black")
  
      ax.set_ylabel('True label', fontsize=text_fontsize)
      ax.set_xlabel('Predicted label', fontsize=text_fontsize)
      ax.grid('off')
  
      return ax