code: def visualize_tree(
      estimator, X, y, boundaries=True, xlim=None, ylim=None
  ):
      estimator.fit(X, y)
  
      if xlim is None:
          xlim = (X[:, 0].min() - 0.1, X[:, 0].max() + 0.1)
      if ylim is None:
          ylim = (X[:, 1].min() - 0.1, X[:, 1].max() + 0.1)
  
      x_min, x_max = xlim
      y_min, y_max = ylim
      xx, yy = np.meshgrid(
          np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)
      )
      Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()])
  
      # Put the result into a color plot
      Z = Z.reshape(xx.shape)
      plt.figure()
      plt.pcolormesh(xx, yy, Z, alpha=0.2, cmap="rainbow")
      plt.clim(y.min(), y.max())
  
      # Plot also the training points
      plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap="rainbow")
      plt.axis("off")
  
      plt.xlim(x_min, x_max)
      plt.ylim(y_min, y_max)
      plt.clim(y.min(), y.max())
  
      # Plot the decision boundaries
      def plot_boundaries(i, xlim, ylim):
          if i < 0:
              return
  
          tree = estimator.tree_
  
          if tree.feature[i] == 0:
              plt.plot(
                  [tree.threshold[i], tree.threshold[i]], ylim, "-k"
              )
              plot_boundaries(
                  tree.children_left[i],
                  [xlim[0], tree.threshold[i]],
                  ylim,
              )
              plot_boundaries(
                  tree.children_right[i],
                  [tree.threshold[i], xlim[1]],
                  ylim,
              )
  
          elif tree.feature[i] == 1:
              plt.plot(
                  xlim, [tree.threshold[i], tree.threshold[i]], "-k"
              )
              plot_boundaries(
                  tree.children_left[i],
                  xlim,
                  [ylim[0], tree.threshold[i]],
              )
              plot_boundaries(
                  tree.children_right[i],
                  xlim,
                  [tree.threshold[i], ylim[1]],
              )
  
      if boundaries:
          plot_boundaries(0, plt.xlim(), plt.ylim())
  
file_path: data-science-ipython-notebooks/scikit-learn/fig_code/figures.py
imports: import numpy
  import matplotlib.pyplot
  import warnings
label_desc: Use image node_id 3 for calling a global function with example usage: visualize_tree(estimator, X, y, boundaries, xlim, ylim) without return types