code: def ridge_regression(
      X,
      y,
      alpha,
      sample_weight=None,
      solver="auto",
      max_iter=None,
      tol=1e-3,
      verbose=0,
      random_state=None,
      return_n_iter=False,
      return_intercept=False,
  ):
      """Solve the ridge equation by the method of normal equations.
  
      Read more in the :ref:`User Guide <ridge_regression>`.
  
      Parameters
      ----------
      X : {array-like, sparse matrix, LinearOperator},
          shape = [n_samples, n_features]
          Training data
  
      y : array-like, shape = [n_samples] or [n_samples, n_targets]
          Target values
  
      alpha : {float, array-like},
          shape = [n_targets] if array-like
          Regularization strength; must be a positive float. Regularization
          improves the conditioning of the problem and reduces the variance of
          the estimates. Larger values specify stronger regularization.
          Alpha corresponds to ``C^-1`` in other linear models such as
          LogisticRegression or LinearSVC. If an array is passed, penalties are
          assumed to be specific to the targets. Hence they must correspond in
          number.
  
      max_iter : int, optional
          Maximum number of iterations for conjugate gradient solver.
          For 'sparse_cg' and 'lsqr' solvers, the default value is determined
          by scipy.sparse.linalg. For 'sag' solver, the default value is 1000.
  
      sample_weight : float or numpy array of shape [n_samples]
          Individual weights for each sample. If sample_weight is not None and
          solver='auto', the solver will be set to 'cholesky'.
  
          .. versionadded:: 0.17
  
      solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg'}
          Solver to use in the computational routines:
  
          - 'auto' chooses the solver automatically based on the type of data.
  
          - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
            coefficients. More stable for singular matrices than
            'cholesky'.
  
          - 'cholesky' uses the standard scipy.linalg.solve function to
            obtain a closed-form solution via a Cholesky decomposition of
            dot(X.T, X)
  
          - 'sparse_cg' uses the conjugate gradient solver as found in
            scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
            more appropriate than 'cholesky' for large-scale data
            (possibility to set `tol` and `max_iter`).
  
          - 'lsqr' uses the dedicated regularized least-squares routine
            scipy.sparse.linalg.lsqr. It is the fastest but may not be available
            in old scipy versions. It also uses an iterative procedure.
  
          - 'sag' uses a Stochastic Average Gradient descent. It also uses an
            iterative procedure, and is often faster than other solvers when
            both n_samples and n_features are large. Note that 'sag' fast
            convergence is only guaranteed on features with approximately the
            same scale. You can preprocess the data with a scaler from
            sklearn.preprocessing.
  
          All last four solvers support both dense and sparse data. However,
          only 'sag' supports sparse input when `fit_intercept` is True.
  
          .. versionadded:: 0.17
             Stochastic Average Gradient descent solver.
  
      tol : float
          Precision of the solution.
  
      verbose : int
          Verbosity level. Setting verbose > 0 will display additional
          information depending on the solver used.
  
      random_state : int seed, RandomState instance, or None (default)
          The seed of the pseudo random number generator to use when
          shuffling the data. Used only in 'sag' solver.
  
      return_n_iter : boolean, default False
          If True, the method also returns `n_iter`, the actual number of
          iteration performed by the solver.
  
          .. versionadded:: 0.17
  
      return_intercept : boolean, default False
          If True and if X is sparse, the method also returns the intercept,
          and the solver is automatically changed to 'sag'. This is only a
          temporary fix for fitting the intercept with sparse data. For dense
          data, use sklearn.linear_model._preprocess_data before your regression.
  
          .. versionadded:: 0.17
  
      Returns
      -------
      coef : array, shape = [n_features] or [n_targets, n_features]
          Weight vector(s).
  
      n_iter : int, optional
          The actual number of iteration performed by the solver.
          Only returned if `return_n_iter` is True.
  
      intercept : float or array, shape = [n_targets]
          The intercept of the model. Only returned if `return_intercept`
          is True and if X is a scipy sparse array.
  
      Notes
      -----
      This function won't compute the intercept.
      """
      if return_intercept and sparse.issparse(X) and solver != "sag":
          if solver != "auto":
              warnings.warn(
                  "In Ridge, only 'sag' solver can currently fit the "
                  "intercept when X is sparse. Solver has been "
                  "automatically changed into 'sag'."
              )
          solver = "sag"
  
      # SAG needs X and y columns to be C-contiguous and np.float64
      if solver == "sag":
          X = check_array(
              X, accept_sparse=["csr"], dtype=np.float64, order="C"
          )
          y = check_array(
              y, dtype=np.float64, ensure_2d=False, order="F"
          )
      else:
          X = check_array(
              X, accept_sparse=["csr", "csc", "coo"], dtype=np.float64
          )
          y = check_array(y, dtype="numeric", ensure_2d=False)
      check_consistent_length(X, y)
  
      n_samples, n_features = X.shape
  
      if y.ndim > 2:
          raise ValueError(
              "Target y has the wrong shape %s" % str(y.shape)
          )
  
      ravel = False
      if y.ndim == 1:
          y = y.reshape(-1, 1)
          ravel = True
  
      n_samples_, n_targets = y.shape
  
      if n_samples != n_samples_:
          raise ValueError(
              "Number of samples in X and y does not correspond:"
              " %d != %d" % (n_samples, n_samples_)
          )
  
      has_sw = sample_weight is not None
  
      if solver == "auto":
          # cholesky if it's a dense array and cg in any other case
          if not sparse.issparse(X) or has_sw:
              solver = "cholesky"
          else:
              solver = "sparse_cg"
  
      elif solver == "lsqr" and not hasattr(sp_linalg, "lsqr"):
          warnings.warn(
              """lsqr not available on this machine, falling back
                        to sparse_cg."""
          )
          solver = "sparse_cg"
  
      if has_sw:
          if np.atleast_1d(sample_weight).ndim > 1:
              raise ValueError(
                  "Sample weights must be 1D array or scalar"
              )
  
          if solver != "sag":
              # SAG supports sample_weight directly. For other solvers,
              # we implement sample_weight via a simple rescaling.
              X, y = _rescale_data(X, y, sample_weight)
  
      # There should be either 1 or n_targets penalties
      alpha = np.asarray(alpha).ravel()
      if alpha.size not in [1, n_targets]:
          raise ValueError(
              "Number of targets and number of penalties "
              "do not correspond: %d != %d" % (alpha.size, n_targets)
          )
  
      if alpha.size == 1 and n_targets > 1:
          alpha = np.repeat(alpha, n_targets)
  
      if solver not in ("sparse_cg", "cholesky", "svd", "lsqr", "sag"):
          raise ValueError("Solver %s not understood" % solver)
  
      n_iter = None
      if solver == "sparse_cg":
          coef = _solve_sparse_cg(X, y, alpha, max_iter, tol, verbose)
  
      elif solver == "lsqr":
          coef, n_iter = _solve_lsqr(X, y, alpha, max_iter, tol)
  
      elif solver == "cholesky":
          if n_features > n_samples:
              K = safe_sparse_dot(X, X.T, dense_output=True)
              try:
                  dual_coef = _solve_cholesky_kernel(K, y, alpha)
  
                  coef = safe_sparse_dot(
                      X.T, dual_coef, dense_output=True
                  ).T
              except linalg.LinAlgError:
                  # use SVD solver if matrix is singular
                  solver = "svd"
  
          else:
              try:
                  coef = _solve_cholesky(X, y, alpha)
              except linalg.LinAlgError:
                  # use SVD solver if matrix is singular
                  solver = "svd"
  
      elif solver == "sag":
          # precompute max_squared_sum for all targets
          max_squared_sum = row_norms(X, squared=True).max()
  
          coef = np.empty((y.shape[1], n_features))
          n_iter = np.empty(y.shape[1], dtype=np.int32)
          intercept = np.zeros((y.shape[1],))
          for i, (alpha_i, target) in enumerate(zip(alpha, y.T)):
              init = {
                  "coef": np.zeros(
                      (n_features + int(return_intercept), 1)
                  )
              }
              coef_, n_iter_, _ = sag_solver(
                  X,
                  target.ravel(),
                  sample_weight,
                  "squared",
                  alpha_i,
                  max_iter,
                  tol,
                  verbose,
                  random_state,
                  False,
                  max_squared_sum,
                  init,
              )
              if return_intercept:
                  coef[i] = coef_[:-1]
                  intercept[i] = coef_[-1]
              else:
                  coef[i] = coef_
              n_iter[i] = n_iter_
  
          if intercept.shape[0] == 1:
              intercept = intercept[0]
          coef = np.asarray(coef)
  
      if solver == "svd":
          if sparse.issparse(X):
              raise TypeError(
                  "SVD solver does not support sparse"
                  " inputs currently"
              )
          coef = _solve_svd(X, y, alpha)
  
      if ravel:
          # When y was passed as a 1d-array, we flatten the coefficients.
          coef = coef.ravel()
  
      if return_n_iter and return_intercept:
          return coef, n_iter, intercept
      elif return_intercept:
          return coef, intercept
      elif return_n_iter:
          return coef, n_iter
      else:
          return coef
  
imports: from abc import ABCMeta, abstractmethod
  import warnings
  import numpy
  from scipy import linalg
  from scipy import sparse
  from scipy.sparse import linalg
  from .base import LinearClassifierMixin, LinearModel, _rescale_data
  from .sag import sag_solver
  from ..base import RegressorMixin
  from ..utils.extmath import safe_sparse_dot
  from ..utils.extmath import row_norms
  from ..utils import check_X_y
  from ..utils import check_array
  from ..utils import check_consistent_length
  from ..utils import compute_sample_weight
  from ..utils import column_or_1d
  from ..preprocessing import LabelBinarizer
  from ..model_selection import GridSearchCV
  from ..externals import six
  from ..metrics.scorer import check_scoring
label_desc: Use image node_id 6 for calling a global function with example usage: ridge_regression(X, y, alpha, sample_weight, solver, max_iter, tol, verbose, random_state, return_n_iter, return_intercept) and returns: coef,  n_iter,  intercept, coef,  intercept, coef,  n_iter, coef