|
2 | 2 | # Mathieu Blondel (partial_fit support) |
3 | 3 | # |
4 | 4 | # License: BSD Style. |
5 | | -"""Implementation of Stochastic Gradient Descent (SGD).""" |
| 5 | +"""Classification and regression using Stochastic Gradient Descent (SGD).""" |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import scipy.sparse as sp |
|
29 | 29 |
|
30 | 30 |
|
31 | 31 | class BaseSGD(BaseEstimator): |
32 | | - """Base class for dense and sparse SGD.""" |
| 32 | + """Base class for SGD classification and regression.""" |
33 | 33 |
|
34 | 34 | __metaclass__ = ABCMeta |
35 | 35 |
|
@@ -96,10 +96,6 @@ def _set_learning_rate(self, learning_rate): |
96 | 96 | raise ValueError("learning rate %s" |
97 | 97 | "is not supported. " % learning_rate) |
98 | 98 |
|
99 | | - def _set_loss_function(self, loss): |
100 | | - """Get concrete LossFunction""" |
101 | | - raise NotImplementedError("BaseSGD is an abstract class.") |
102 | | - |
103 | 99 | def _set_penalty_type(self, penalty): |
104 | 100 | penalty_types = {"none": 0, "l2": 2, "l1": 1, "elasticnet": 3} |
105 | 101 | try: |
@@ -176,10 +172,11 @@ def _allocate_parameter_mem(self, n_classes, n_features, coef_init=None, |
176 | 172 | else: |
177 | 173 | self.intercept_ = np.zeros(1, dtype=np.float64, order="C") |
178 | 174 |
|
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.") |
183 | 180 |
|
184 | 181 |
|
185 | 182 | 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, |
389 | 386 | y = np.asarray(y) |
390 | 387 |
|
391 | 388 | n_samples, n_features = X.shape |
392 | | - self._check_fit_data(X, y) |
| 389 | + _check_fit_data(X, y) |
393 | 390 |
|
394 | 391 | if self.classes_ is None and classes is None: |
395 | 392 | 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, |
497 | 494 | y = np.asarray(y) |
498 | 495 |
|
499 | 496 | n_samples, n_features = X.shape |
500 | | - self._check_fit_data(X, y) |
| 497 | + _check_fit_data(X, y) |
501 | 498 |
|
502 | 499 | # np.unique sorts in asc order; largest class id is positive class |
503 | 500 | classes = np.unique(y) |
@@ -813,7 +810,7 @@ def _partial_fit(self, X, y, n_iter, sample_weight=None, |
813 | 810 | check_ccontiguous=True, dtype=np.float64) |
814 | 811 |
|
815 | 812 | n_samples, n_features = X.shape |
816 | | - self._check_fit_data(X, y) |
| 813 | + _check_fit_data(X, y) |
817 | 814 |
|
818 | 815 | # Allocate datastructures from input arguments |
819 | 816 | sample_weight = self._validate_sample_weight(sample_weight, n_samples) |
|
0 commit comments