MCPcopy Index your code
hub / github.com/ddbourgin/numpy-ml / _NLL

Method _NLL

numpy_ml/linear_models/logistic.py:121–141  ·  view source on GitHub ↗

r""" Penalized negative log likelihood of the targets under the current model. .. math:: \text{NLL} = -\frac{1}{N} \left[ \left( \sum_{i=0}^N y_i \log(\hat{y}_i) + (1-y_i) \log(1-\hat{y}_i) \right) - R(\mathbf{

(self, X, y, y_pred)

Source from the content-addressed store, hash-verified

119 self.beta -= lr * self._NLL_grad(X, y, y_pred)
120
121 def _NLL(self, X, y, y_pred):
122 r"""
123 Penalized negative log likelihood of the targets under the current
124 model.
125
126 .. math::
127
128 \text{NLL} = -\frac{1}{N} \left[
129 \left(
130 \sum_{i=0}^N y_i \log(\hat{y}_i) + (1-y_i) \log(1-\hat{y}_i)
131 \right) - R(\mathbf{b}, \gamma)
132 \right]
133 """
134 N, M = X.shape
135 beta, gamma = self.beta, self.gamma
136 order = 2 if self.penalty == "l2" else 1
137 norm_beta = np.linalg.norm(beta, ord=order)
138
139 nll = -np.log(y_pred[y == 1]).sum() - np.log(1 - y_pred[y == 0]).sum()
140 penalty = (gamma / 2) * norm_beta ** 2 if order == 2 else gamma * norm_beta
141 return (penalty + nll) / N
142
143 def _NLL_grad(self, X, y, y_pred):
144 """Gradient of the penalized negative log likelihood wrt beta"""

Callers 1

fitMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected