#NEWLINE# def _logistic_loss_and_grad( w , X , y , alpha , sample_weight = None ) : #NEWLINE# #INDENT# 'Computes the logistic loss and gradient.\n\n    Parameters\n    ----------\n    w : ndarray, shape (n_features,) or (n_features + 1,)\n        Coefficient vector.\n\n    X : {array-like, sparse matrix}, shape (n_samples, n_features)\n        Training data.\n\n    y : ndarray, shape (n_samples,)\n        Array of labels.\n\n    alpha : float\n        Regularization parameter. alpha is equal to 1 / C.\n\n    sample_weight : array-like, shape (n_samples,) optional\n        Array of weights that are assigned to individual samples.\n        If not provided, then each sample is given unit weight.\n\n    Returns\n    -------\n    out : float\n        Logistic loss.\n\n    grad : ndarray, shape (n_features,) or (n_features + 1,)\n        Logistic gradient.\n    ' #NEWLINE# ( n_samples , n_features ) = X . shape #NEWLINE# grad = np . empty_like ( w ) #NEWLINE# ( w , c , yz ) = _intercept_dot ( w , X , y ) #NEWLINE# if ( sample_weight is None ) : #NEWLINE# #INDENT# sample_weight = np . ones ( n_samples ) #NEWLINE# #UNINDENT# out = ( ( - np . sum ( ( sample_weight * log_logistic ( yz ) ) ) ) + ( ( 0.5 * alpha ) * np . dot ( w , w ) ) ) #NEWLINE# z = expit ( yz ) #NEWLINE# z0 = ( ( sample_weight * ( z - 1 ) ) * y ) #NEWLINE# grad [ : n_features ] = ( safe_sparse_dot ( X . T , z0 ) + ( alpha * w ) ) #NEWLINE# if ( grad . shape [ 0 ] > n_features ) : #NEWLINE# #INDENT# grad [ ( - 1 ) ] = z0 . sum ( ) #NEWLINE# #UNINDENT# return ( out , grad )