Reputation: 1855
After using sklearn.linear_model.LogisticRegression
to fit a training data set, I would like to obtain the value of the cost function for the training data set and a cross validation data set.
Is it possible to have sklearn
simply give me the value (at the fit minimum) of the function it minimized?
The function is stated in the documentation at http://scikit-learn.org/stable/modules/linear_model.html#logistic-regression (depending on the regularization one has chosen). But I can't find how to get sklearn
to give me the value of this function.
I would have thought this is what LogisticRegression.score
does, but that simply returns the accuracy (the fraction of data points its prediction classifies correctly).
I have found sklearn.metrics.log_loss
, but of course this is not the actual function being minimized.
Upvotes: 6
Views: 12419
Reputation: 33
I have the following suggestions. You can write the codes for the loss function of logistic regression as a function. After you get your predicted labels of data, you can revoke your defined function to calculate the cost values.
Upvotes: 0
Reputation: 143
I used below code to calculate cost value.
import numpy as np
cost = np.sum((reg.predict(x) - y) ** 2)
where reg
is your learned LogisticRegression
Upvotes: 0
Reputation: 66825
Unfortunately there is no "nice" way to do so, but there is a private function
_logistic_loss(w, X, y, alpha, sample_weight=None)
in https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/linear_model/logistic.py, thus you can call it by hand
from sklearn.linear_model.logistic import _logistic_loss
print _logistic_loss(clf.coef_, X, y, 1 / clf.C)
where clf
is your learned LogisticRegression
Upvotes: 13