Why is the negative logarithm?

I applied sklearn log loss for logistic regression: http://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html

My code looks something like this:

def perform_cv(clf, X, Y, scoring): kf = KFold(X.shape[0], n_folds=5, shuffle=True) kf_scores = [] for train, _ in kf: X_sub = X[train,:] Y_sub = Y[train] #Apply 'log_loss' as a loss function scores = cross_validation.cross_val_score(clf, X_sub, Y_sub, cv=5, scoring='log_loss') kf_scores.append(scores.mean()) return kf_scores 

However, I wonder why the resulting logarithmic losses are negative. I expect them to be positive since in the documentation (see My link above) the logarithmic loss is multiplied by -1 to turn it into a positive number.

Am I doing something wrong here?

+6
source share
4 answers

a similar discussion can be found here .

Thus, a higher score means better performance (less loss).

+5
source

Yes, this is bound to happen. This is not a β€œmistake” as others have suggested. Actual log loss is simply the positive version of the number you get.

The SK-Learn API with a unified score always maximizes the score, so the points that need to be minimized are reset so that the unified scoring API works correctly. The returned result is therefore denied when it is an estimate that should be minimized and left positive if it is an estimate that should be maximum.

This is also described in sklearn GridSearchCV with Pipeline and scikit-learn cross-validation, negative values ​​with a mean square error

+3
source

The loss logarithm should be near zero for a good forecasting algorithm, a large negative value will mean that the predictive analysis is disabled and needs to be rethought.

0
source

I cross-checked the implementation of sklearn with several other methods. This seems to be a real mistake in the framework. Instead, consider the following code to calculate log loss:

 import scipy as sp def llfun(act, pred): epsilon = 1e-15 pred = sp.maximum(epsilon, pred) pred = sp.minimum(1-epsilon, pred) ll = sum(act*sp.log(pred) + sp.subtract(1,act)*sp.log(sp.subtract(1,pred))) ll = ll * -1.0/len(act) return ll 

Also note that the sizes of act and pred must have column vectors Nx1.

-1
source

Source: https://habr.com/ru/post/976505/


All Articles