#NEWLINE# def _logistic_grad_hess( w , X , y , alpha , sample_weight = None ) : #NEWLINE# #INDENT# 'Computes the gradient and the Hessian, in the case of a logistic loss.\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    grad : ndarray, shape (n_features,) or (n_features + 1,)\n        Logistic gradient.\n\n    Hs : callable\n        Function that takes the gradient as a parameter and returns the\n        matrix product of the Hessian and gradient.\n    ' #NEWLINE# ( n_samples , n_features ) = fit_intercept . shape #NEWLINE# grad = np . empty_like ( w ) #NEWLINE# fit_intercept = ( grad . shape [ 0 ] > n_features ) #NEWLINE# ( w , c , yz ) = _intercept_dot ( w , X , y ) #NEWLINE# if ( sample_weight is None ) : #NEWLINE# #INDENT# sample_weight = np . ones ( y . shape [ 0 ] ) #NEWLINE# #UNINDENT# z = expit ( yz ) #NEWLINE# z0 = ( ( sample_weight * ( z - 1 ) ) * y ) #NEWLINE# grad [ : n_features ] = ( safe_sparse_dot ( X . T , z0 ) + ( alpha * w ) ) #NEWLINE# if fit_intercept : #NEWLINE# #INDENT# grad [ ( - 1 ) ] = z0 . sum ( ) #NEWLINE# #UNINDENT# d = ( ( sample_weight * z ) * ( 1 - z ) ) #NEWLINE# if sparse . issparse ( X ) : #NEWLINE# #INDENT# dX = safe_sparse_dot ( sparse . dia_matrix ( ( d , 0 ) , shape = ( n_samples , n_samples ) ) , X ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dX = ( d [ : , np . newaxis ] * X ) #NEWLINE# #UNINDENT# if fit_intercept : #NEWLINE# #INDENT# dd_intercept = np . squeeze ( np . array ( dX . sum ( axis = 0 ) ) ) #NEWLINE# #UNINDENT# def Hs( s ) : #NEWLINE# #INDENT# ret = np . empty_like ( s ) #NEWLINE# ret [ : n_features ] = X . T . dot ( dX . dot ( s [ : n_features ] ) ) #NEWLINE# ret [ : n_features ] += ( alpha * s [ : n_features ] ) #NEWLINE# if fit_intercept : #NEWLINE# #INDENT# ret [ : n_features ] += ( s [ ( - 1 ) ] * dd_intercept ) #NEWLINE# ret [ ( - 1 ) ] = dd_intercept . dot ( s [ : n_features ] ) #NEWLINE# ret [ ( - 1 ) ] += ( d . sum ( ) * s [ ( - 1 ) ] ) #NEWLINE# #UNINDENT# return ret #NEWLINE# #UNINDENT# return ( grad , Hs )