Skip to content

Commit 913e222

Browse files
committed
COSMIT minor refactoring of SGD
1 parent 0695e83 commit 913e222

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

sklearn/linear_model/stochastic_gradient.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Mathieu Blondel (partial_fit support)
33
#
44
# License: BSD Style.
5-
"""Implementation of Stochastic Gradient Descent (SGD)."""
5+
"""Classification and regression using Stochastic Gradient Descent (SGD)."""
66

77
import numpy as np
88
import scipy.sparse as sp
@@ -29,7 +29,7 @@
2929

3030

3131
class BaseSGD(BaseEstimator):
32-
"""Base class for dense and sparse SGD."""
32+
"""Base class for SGD classification and regression."""
3333

3434
__metaclass__ = ABCMeta
3535

@@ -96,10 +96,6 @@ def _set_learning_rate(self, learning_rate):
9696
raise ValueError("learning rate %s"
9797
"is not supported. " % learning_rate)
9898

99-
def _set_loss_function(self, loss):
100-
"""Get concrete LossFunction"""
101-
raise NotImplementedError("BaseSGD is an abstract class.")
102-
10399
def _set_penalty_type(self, penalty):
104100
penalty_types = {"none": 0, "l2": 2, "l1": 1, "elasticnet": 3}
105101
try:
@@ -176,10 +172,11 @@ def _allocate_parameter_mem(self, n_classes, n_features, coef_init=None,
176172
else:
177173
self.intercept_ = np.zeros(1, dtype=np.float64, order="C")
178174

179-
def _check_fit_data(self, X, y):
180-
n_samples, _ = X.shape
181-
if n_samples != y.shape[0]:
182-
raise ValueError("Shapes of X and y do not match.")
175+
176+
def _check_fit_data(X, y):
177+
n_samples, _ = X.shape
178+
if n_samples != y.shape[0]:
179+
raise ValueError("Shapes of X and y do not match.")
183180

184181

185182
def _make_dataset(X, y_i, sample_weight):
@@ -389,7 +386,7 @@ def _partial_fit(self, X, y, n_iter, classes=None, sample_weight=None,
389386
y = np.asarray(y)
390387

391388
n_samples, n_features = X.shape
392-
self._check_fit_data(X, y)
389+
_check_fit_data(X, y)
393390

394391
if self.classes_ is None and classes is None:
395392
raise ValueError("classes must be passed on the first call "
@@ -497,7 +494,7 @@ def fit(self, X, y, coef_init=None, intercept_init=None,
497494
y = np.asarray(y)
498495

499496
n_samples, n_features = X.shape
500-
self._check_fit_data(X, y)
497+
_check_fit_data(X, y)
501498

502499
# np.unique sorts in asc order; largest class id is positive class
503500
classes = np.unique(y)
@@ -813,7 +810,7 @@ def _partial_fit(self, X, y, n_iter, sample_weight=None,
813810
check_ccontiguous=True, dtype=np.float64)
814811

815812
n_samples, n_features = X.shape
816-
self._check_fit_data(X, y)
813+
_check_fit_data(X, y)
817814

818815
# Allocate datastructures from input arguments
819816
sample_weight = self._validate_sample_weight(sample_weight, n_samples)

0 commit comments

Comments
 (0)