nodes: 0, module; 1, function_definition; 2, function_name:plot_confusion_matrix_with_cv; 3, parameters; 4, block; 5, identifier:clf; 6, identifier:X; 7, identifier:y; 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, default_parameter; 20, default_parameter; 21, default_parameter; 22, default_parameter; 23, default_parameter; 24, expression_statement; 25, expression_statement; 26, if_statement; 27, expression_statement; 28, return_statement; 29, identifier:labels; 30, None; 31, identifier:true_labels; 32, None; 33, identifier:pred_labels; 34, None; 35, identifier:title; 36, None; 37, identifier:normalize; 38, False; 39, identifier:hide_zeros; 40, False; 41, identifier:x_tick_rotation; 42, integer:0; 43, identifier:do_cv; 44, True; 45, identifier:cv; 46, None; 47, identifier:shuffle; 48, True; 49, identifier:random_state; 50, None; 51, identifier:ax; 52, None; 53, identifier:figsize; 54, None; 55, identifier:cmap; 56, string; 57, identifier:title_fontsize; 58, string:"large"; 59, identifier:text_fontsize; 60, string:"medium"; 61, comment:"""Generates the confusion matrix for a given classifier and dataset.
  
      Args:
          clf: Classifier instance that implements ``fit`` and ``predict``
              methods.
  
          X (array-like, shape (n_samples, n_features)):
              Training vector, where n_samples is the number of samples and
              n_features is the number of features.
  
          y (array-like, shape (n_samples) or (n_samples, n_features)):
              Target relative to X for classification.
  
          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``
              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.
  
          do_cv (bool, optional): If True, the classifier is cross-validated on
              the dataset using the cross-validation strategy in `cv` to generate
              the confusion matrix. If False, the confusion matrix is generated
              without training or cross-validating the classifier. This assumes
              that the classifier has already been called with its `fit` method
              beforehand.
  
          cv (int, cross-validation generator, iterable, optional): Determines
              the cross-validation strategy to be used for splitting.
  
              Possible inputs for cv are:
                - None, to use the default 3-fold cross-validation,
                - integer, to specify the number of folds.
                - An object to be used as a cross-validation generator.
                - An iterable yielding train/test splits.
  
              For integer/None inputs, if ``y`` is binary or multiclass,
              :class:`StratifiedKFold` used. If the estimator is not a classifier
              or if ``y`` is neither binary nor multiclass, :class:`KFold` is
              used.
  
          shuffle (bool, optional): Used when do_cv is set to True. Determines
              whether to shuffle the training data before splitting using
              cross-validation. Default set to True.
  
          random_state (int :class:`RandomState`): Pseudo-random number generator
              state used for random sampling.
  
          ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to
              plot the learning 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:
          >>> rf = classifier_factory(RandomForestClassifier())
          >>> rf.plot_confusion_matrix(X, y, normalize=True)
          <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
          >>> plt.show()
  
          .. image:: _static/examples/plot_confusion_matrix.png
             :align: center
             :alt: Confusion matrix
      """; 62, assignment; 63, not_operator; 64, block; 65, else_clause; 66, assignment; 67, identifier:ax; 68, string_content:Blues; 69, identifier:y; 70, call; 71, identifier:do_cv; 72, expression_statement; 73, expression_statement; 74, block; 75, identifier:ax; 76, call; 77, attribute; 78, argument_list; 79, assignment; 80, assignment; 81, if_statement; 82, expression_statement; 83, expression_statement; 84, expression_statement; 85, for_statement; 86, expression_statement; 87, expression_statement; 88, attribute; 89, argument_list; 90, identifier:np; 91, identifier:array; 92, identifier:y; 93, identifier:y_pred; 94, call; 95, identifier:y_true; 96, identifier:y; 97, comparison_operator:cv is None; 98, block; 99, elif_clause; 100, else_clause; 101, assignment; 102, assignment; 103, assignment; 104, pattern_list; 105, call; 106, block; 107, assignment; 108, assignment; 109, identifier:plotters; 110, identifier:plot_confusion_matrix; 111, keyword_argument; 112, keyword_argument; 113, keyword_argument; 114, keyword_argument; 115, keyword_argument; 116, keyword_argument; 117, keyword_argument; 118, keyword_argument; 119, keyword_argument; 120, keyword_argument; 121, keyword_argument; 122, keyword_argument; 123, keyword_argument; 124, keyword_argument; 125, attribute; 126, argument_list; 127, identifier:cv; 128, None; 129, expression_statement; 130, call; 131, block; 132, block; 133, identifier:clf_clone; 134, call; 135, identifier:preds_list; 136, list; 137, identifier:trues_list; 138, list; 139, identifier:train_index; 140, identifier:test_index; 141, attribute; 142, argument_list; 143, expression_statement; 144, expression_statement; 145, expression_statement; 146, expression_statement; 147, expression_statement; 148, expression_statement; 149, identifier:y_pred; 150, call; 151, identifier:y_true; 152, call; 153, identifier:y_true; 154, identifier:y_true; 155, identifier:y_pred; 156, identifier:y_pred; 157, identifier:labels; 158, identifier:labels; 159, identifier:true_labels; 160, identifier:true_labels; 161, identifier:pred_labels; 162, identifier:pred_labels; 163, identifier:title; 164, identifier:title; 165, identifier:normalize; 166, identifier:normalize; 167, identifier:hide_zeros; 168, identifier:hide_zeros; 169, identifier:x_tick_rotation; 170, identifier:x_tick_rotation; 171, identifier:ax; 172, identifier:ax; 173, identifier:figsize; 174, identifier:figsize; 175, identifier:cmap; 176, identifier:cmap; 177, identifier:title_fontsize; 178, identifier:title_fontsize; 179, identifier:text_fontsize; 180, identifier:text_fontsize; 181, identifier:clf; 182, identifier:predict; 183, identifier:X; 184, assignment; 185, identifier:isinstance; 186, argument_list; 187, expression_statement; 188, pass_statement; 189, identifier:clone; 190, argument_list; 191, identifier:cv; 192, identifier:split; 193, identifier:X; 194, identifier:y; 195, assignment; 196, assignment; 197, call; 198, assignment; 199, call; 200, call; 201, attribute; 202, argument_list; 203, attribute; 204, argument_list; 205, identifier:cv; 206, call; 207, identifier:cv; 208, identifier:int; 209, assignment; 210, identifier:clf; 211, pattern_list; 212, expression_list; 213, pattern_list; 214, expression_list; 215, attribute; 216, argument_list; 217, identifier:preds; 218, call; 219, attribute; 220, argument_list; 221, attribute; 222, argument_list; 223, identifier:np; 224, identifier:concatenate; 225, identifier:preds_list; 226, identifier:np; 227, identifier:concatenate; 228, identifier:trues_list; 229, identifier:StratifiedKFold; 230, argument_list; 231, identifier:cv; 232, call; 233, identifier:X_train; 234, identifier:X_test; 235, subscript; 236, subscript; 237, identifier:y_train; 238, identifier:y_test; 239, subscript; 240, subscript; 241, identifier:clf_clone; 242, identifier:fit; 243, identifier:X_train; 244, identifier:y_train; 245, attribute; 246, argument_list; 247, identifier:preds_list; 248, identifier:append; 249, identifier:preds; 250, identifier:trues_list; 251, identifier:append; 252, identifier:y_test; 253, keyword_argument; 254, keyword_argument; 255, identifier:StratifiedKFold; 256, argument_list; 257, identifier:X; 258, identifier:train_index; 259, identifier:X; 260, identifier:test_index; 261, identifier:y; 262, identifier:train_index; 263, identifier:y; 264, identifier:test_index; 265, identifier:clf_clone; 266, identifier:predict; 267, identifier:X_test; 268, identifier:shuffle; 269, identifier:shuffle; 270, identifier:random_state; 271, identifier:random_state; 272, keyword_argument; 273, keyword_argument; 274, keyword_argument; 275, identifier:n_splits; 276, identifier:cv; 277, identifier:shuffle; 278, identifier:shuffle; 279, identifier:random_state; 280, identifier:random_state
code: def plot_confusion_matrix_with_cv(clf, X, y, labels=None, true_labels=None,
                                    pred_labels=None, title=None,
                                    normalize=False, hide_zeros=False,
                                    x_tick_rotation=0, do_cv=True, cv=None,
                                    shuffle=True, random_state=None, ax=None,
                                    figsize=None, cmap='Blues',
                                    title_fontsize="large",
                                    text_fontsize="medium"):
      """Generates the confusion matrix for a given classifier and dataset.
  
      Args:
          clf: Classifier instance that implements ``fit`` and ``predict``
              methods.
  
          X (array-like, shape (n_samples, n_features)):
              Training vector, where n_samples is the number of samples and
              n_features is the number of features.
  
          y (array-like, shape (n_samples) or (n_samples, n_features)):
              Target relative to X for classification.
  
          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``
              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.
  
          do_cv (bool, optional): If True, the classifier is cross-validated on
              the dataset using the cross-validation strategy in `cv` to generate
              the confusion matrix. If False, the confusion matrix is generated
              without training or cross-validating the classifier. This assumes
              that the classifier has already been called with its `fit` method
              beforehand.
  
          cv (int, cross-validation generator, iterable, optional): Determines
              the cross-validation strategy to be used for splitting.
  
              Possible inputs for cv are:
                - None, to use the default 3-fold cross-validation,
                - integer, to specify the number of folds.
                - An object to be used as a cross-validation generator.
                - An iterable yielding train/test splits.
  
              For integer/None inputs, if ``y`` is binary or multiclass,
              :class:`StratifiedKFold` used. If the estimator is not a classifier
              or if ``y`` is neither binary nor multiclass, :class:`KFold` is
              used.
  
          shuffle (bool, optional): Used when do_cv is set to True. Determines
              whether to shuffle the training data before splitting using
              cross-validation. Default set to True.
  
          random_state (int :class:`RandomState`): Pseudo-random number generator
              state used for random sampling.
  
          ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to
              plot the learning 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:
          >>> rf = classifier_factory(RandomForestClassifier())
          >>> rf.plot_confusion_matrix(X, y, normalize=True)
          <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
          >>> plt.show()
  
          .. image:: _static/examples/plot_confusion_matrix.png
             :align: center
             :alt: Confusion matrix
      """
      y = np.array(y)
  
      if not do_cv:
          y_pred = clf.predict(X)
          y_true = y
  
      else:
          if cv is None:
              cv = StratifiedKFold(shuffle=shuffle, random_state=random_state)
          elif isinstance(cv, int):
              cv = StratifiedKFold(n_splits=cv, shuffle=shuffle,
                                   random_state=random_state)
          else:
              pass
  
          clf_clone = clone(clf)
  
          preds_list = []
          trues_list = []
          for train_index, test_index in cv.split(X, y):
              X_train, X_test = X[train_index], X[test_index]
              y_train, y_test = y[train_index], y[test_index]
              clf_clone.fit(X_train, y_train)
              preds = clf_clone.predict(X_test)
              preds_list.append(preds)
              trues_list.append(y_test)
          y_pred = np.concatenate(preds_list)
          y_true = np.concatenate(trues_list)
  
      ax = plotters.plot_confusion_matrix(y_true=y_true, y_pred=y_pred,
                                          labels=labels, true_labels=true_labels,
                                          pred_labels=pred_labels,
                                          title=title, normalize=normalize,
                                          hide_zeros=hide_zeros,
                                          x_tick_rotation=x_tick_rotation, ax=ax,
                                          figsize=figsize, cmap=cmap,
                                          title_fontsize=title_fontsize,
                                          text_fontsize=text_fontsize)
  
      return ax