code: def cooks_distance(X, y, ax=None):
      """Plots cooks distance.
  
      Parameters
      ----------
      X : array-like, 2D
          Training data
          Refer https://numpy.org/doc/stable/glossary.html#term-array-like
  
      y : array-like, 1D
          Target data
          Refer https://numpy.org/doc/stable/glossary.html#term-array-like
  
      Returns
      -------
      ax: matplotlib Axes
          Axes containing the plot
  
      Examples
      --------
      .. plot:: ../examples/cooks_distance.py
  
      Notes
      -----
      .. versionadded:: 0.8.4
      """
      model = LinearRegression()
      model.fit(X, y)
      leverage = (X * np.linalg.pinv(X).T).sum(1)
      rank = np.linalg.matrix_rank(X)
      df = X.shape[0] - rank
      residuals = y - model.predict(X)
      mse = np.dot(residuals, residuals) / df
      residuals_studentized = (
          residuals / np.sqrt(mse) / np.sqrt(1 - leverage)
      )
      distance_ = residuals_studentized**2 / X.shape[1]
      distance_ *= leverage / (1 - leverage)
      influence_threshold_ = 4 / X.shape[0]
      outlier_percentage_ = (
          sum(distance_ > influence_threshold_) / X.shape[0]
      )
      outlier_percentage_ *= 100.0
  
      if ax is None:
          ax = plt.gca()
      _, _, baseline = ax.stem(
          distance_,
          linefmt="C0-",
          markerfmt=",",
          use_line_collection=True,
      )
  
      ax.set_xlim(0, len(distance_))
  
      label = r"{:0.2f}% > $I_t$ ($I_t=\frac {{4}} {{n}}$)".format(
          outlier_percentage_
      )
      ax.axhline(
          influence_threshold_,
          ls="--",
          label=label,
          c=baseline.get_color(),
          lw=baseline.get_linewidth(),
      )
      ax.set_title("Cook's Distance Outlier Detection")
      ax.set_xlabel("instance index")
      ax.set_ylabel("influence (I)")
      ax.legend(loc="best", frameon=True)
      return ax
  
file_path: sklearn-evaluation/src/sklearn_evaluation/plot/regression.py
imports: import matplotlib.pyplot
  import numpy
  import pandas
  from sklearn_evaluation.telemetry import SKLearnEvaluationLogger
  from sklearn.linear_model import LinearRegression
  from ploomber_core.exceptions import modify_exceptions
label_desc: Use image node_id 5 for calling a global function with example usage: cooks_distance(X, y, ax) and returns: ax