Skip to content

Commit 7ea873b

Browse files
pprettGaelVaroquaux
authored andcommitted
fix the predict_proba w/ sparse matrix regression by using shape instead of len
1 parent b7f6f19 commit 7ea873b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

sklearn/linear_model/stochastic_gradient.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ def decision_function(self, X):
537537
The signed 'distances' to the hyperplane(s).
538538
"""
539539
X = atleast2d_or_csr(X)
540+
return self._decision_function(X)
541+
542+
def _decision_function(self, X):
543+
"""predict decision function assuming that X is either sparse
544+
matrix or array-like.
545+
"""
540546
scores = safe_sparse_dot(X, self.coef_.T) + self.intercept_
541547
if self.classes_.shape[0] == 2:
542548
return np.ravel(scores)
@@ -588,12 +594,13 @@ def predict_proba(self, X):
588594
raise NotImplementedError("predict_(log_)proba only supported"
589595
" for binary classification")
590596

591-
proba = np.ones((len(X), 2), dtype=np.float64)
597+
X = atleast2d_or_csr(X)
598+
proba = np.ones((X.shape[0], 2), dtype=np.float64)
592599
if self.loss == "log":
593-
proba[:, 1] = 1.0 / (1.0 + np.exp(-self.decision_function(X)))
600+
proba[:, 1] = 1.0 / (1.0 + np.exp(-self._decision_function(X)))
594601
elif self.loss == "modified_huber":
595602
proba[:, 1] = np.minimum(1, np.maximum(-1,
596-
self.decision_function(X)))
603+
self._decision_function(X)))
597604
proba[:, 1] += 1
598605
proba[:, 1] /= 2
599606
else:

0 commit comments

Comments
 (0)