code: def _logistic_loss_and_grad(w, X, y, alpha, sample_weight=None):
      """Computes the logistic loss and gradient.
  
      Parameters
      ----------
      w : ndarray, shape (n_features,) or (n_features + 1,)
          Coefficient vector.
  
      X : {array-like, sparse matrix}, shape (n_samples, n_features)
          Training data.
  
      y : ndarray, shape (n_samples,)
          Array of labels.
  
      alpha : float
          Regularization parameter. alpha is equal to 1 / C.
  
      sample_weight : array-like, shape (n_samples,) optional
          Array of weights that are assigned to individual samples.
          If not provided, then each sample is given unit weight.
  
      Returns
      -------
      out : float
          Logistic loss.
  
      grad : ndarray, shape (n_features,) or (n_features + 1,)
          Logistic gradient.
      """
      n_samples, n_features = X.shape
      grad = np.empty_like(w)
  
      w, c, yz = _intercept_dot(w, X, y)
  
      if sample_weight is None:
          sample_weight = np.ones(n_samples)
  
      # Logistic loss is the negative of the log of the logistic function.
      out = -np.sum(
          sample_weight * log_logistic(yz)
      ) + 0.5 * alpha * np.dot(w, w)
  
      z = expit(yz)
      z0 = sample_weight * (z - 1) * y
  
      grad[:n_features] = safe_sparse_dot(X.T, z0) + alpha * w
  
      # Case where we fit the intercept.
      if grad.shape[0] > n_features:
          grad[-1] = z0.sum()
      return out, grad
  
imports: import numbers
  import warnings
  import numpy
  from scipy import optimize, sparse
  from .base import LinearClassifierMixin, SparseCoefMixin, BaseEstimator
  from .sag import sag_solver
  from ..feature_selection.from_model import _LearntSelectorMixin
  from ..preprocessing import LabelEncoder, LabelBinarizer
  from ..svm.base import _fit_liblinear
  from ..utils import check_array, check_consistent_length, compute_class_weight
  from ..utils import check_random_state
  from ..utils.extmath import logsumexp, log_logistic, safe_sparse_dot, softmax, squared_norm
  from ..utils.extmath import row_norms
  from ..utils.optimize import newton_cg
  from ..utils.validation import check_X_y
  from ..exceptions import NotFittedError
  from ..utils.fixes import expit
  from ..utils.multiclass import check_classification_targets
  from ..externals.joblib import Parallel, delayed
  from ..model_selection import check_cv
  from ..externals import six
  from ..metrics import SCORERS
label_desc: Use image node_id 2 for calling a global function with example usage: _logistic_loss_and_grad(w, X, y, alpha, sample_weight) and returns: out,  grad