Stochastic Gradient Descent (SGD) for linear classification and regression.
Classes
Stochastic Gradient Descent classifier for large-scale linear classification.
partial_fit method.__init__( self, loss: str = 'hinge', learning_rate: float = 0.01, regularization: str = 'l2', lambda_: float = 0.0001, n_epochs: int = 100, random_state: Optional[int] = None, shuffle: bool = True, batch_size: int = 256, )
Overview
The algorithm trains a linear classifier using mini-batch SGD:
- Initialize weights and bias to zero
- For each epoch, optionally shuffle the training data
- Process data in mini-batches for vectorized gradient computation
- Compute the loss gradient and regularization gradient for each batch
- Update weights using the learning rate (with inverse scaling schedule)
- For multiclass problems, train one-vs-all binary classifiers
Theory
At each step, the weights are updated using the gradient of the loss function with regularization:
The supported loss functions are:
- Hinge loss (linear SVM): L = \max(0, 1 - y \cdot w^T x)
- Log loss (logistic regression): L = \log(1 + e^{-y \cdot w^T x})
- Modified Huber: a smooth approximation combining hinge and log loss
Parameters
loss
The loss function to be used:
-
"hinge"- Gives a linear SVM. -
"log"- Gives logistic regression, a probabilistic classifier. -
"modified_huber"- Smooth loss that brings tolerance to outliers
learning_rate
regularization
lambda_
n_epochs
random_state
shuffle
batch_size
Attributes
weights_
bias_
classes_
Notes
Complexity:
- Training: O(n \cdot m \cdot \text{n\_epochs}) where n is
- Prediction: O(m) per sample.
- Large-scale datasets where batch solvers are too slow
-
Online or streaming learning scenarios (via
partial_fit) - When you want to switch between SVM and logistic regression by changing the loss
- When L1 regularization (sparsity) is desired
References
See Also
Binary classification with SGD using log loss:
>>> import numpy as np
>>> from tuiml.algorithms.linear import SGDClassifier
>>>
>>> X = np.array([[0, 0], [1, 1], [2, 2], [3, 3]])
>>> y = np.array([0, 0, 1, 1])
>>>
>>> clf = SGDClassifier(loss='log', learning_rate=0.01)
>>> clf.fit(X, y)
>>>
>>> clf.predict([[2.5, 2.5]])
array([1])
Methods
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
get_capabilities
(cls) -> List[str]
get_complexity
(cls) -> str
get_references
(cls) -> List[str]
partial_fit
(self, X: np.ndarray, y: np.ndarray) -> 'SGDClassifier'
partial_fit
(self, X: np.ndarray, y: np.ndarray) -> 'SGDClassifier'
Update classifier with new samples (online learning).
Parameters
X
y
Returns
self
__repr__
(self) -> str
Stochastic Gradient Descent regressor for large-scale linear regression.
partial_fit method.__init__( self, loss: str = 'squared_error', learning_rate: float = 0.01, regularization: str = 'l2', lambda_: float = 0.0001, n_epochs: int = 100, epsilon: float = 0.1, random_state: Optional[int] = None, shuffle: bool = True, batch_size: int = 256, )
Overview
The algorithm trains a linear regression model using mini-batch SGD:
- Initialize weights and bias to zero
- For each epoch, optionally shuffle the training data
- Process data in mini-batches for vectorized gradient computation
- Compute the loss gradient (squared error or Huber) and regularization gradient
- Update weights using the learning rate (with inverse scaling schedule)
-
Repeat for
n_epochspasses over the data
Theory
The model fits a linear function f(x) = w^T x + b by minimizing the regularized empirical risk:
Squared error loss:
Huber loss (robust to outliers):
The weight update rule at each step is:
with the inverse scaling learning rate schedule \eta_t = \eta_0 / (1 + t \cdot 10^{-4}).
Parameters
loss
The loss function to be used:
-
"squared_error"- Ordinary least squares. -
"huber"- Huber loss for robustness to outliers.
learning_rate
regularization
lambda_
n_epochs
epsilon
random_state
shuffle
batch_size
Attributes
weights_
bias_
Notes
Complexity:
- Training: O(n \cdot m \cdot \text{n\_epochs}) where n is
- Prediction: O(m) per sample.
- Large-scale regression datasets where closed-form OLS is too slow
-
Online or streaming regression scenarios (via
partial_fit) - When Huber loss is needed for robustness to outliers
- When L1 regularization (sparse solutions) is desired
References
See Also
Regression with SGD using squared error loss:
>>> import numpy as np
>>> from tuiml.algorithms.linear import SGDRegressor
>>>
>>> # Generate simple linear data
>>> X = np.array([[1], [2], [3], [4], [5]])
>>> y = np.array([2.0, 4.0, 6.0, 8.0, 10.0])
>>>
>>> # Fit the model
>>> reg = SGDRegressor(learning_rate=0.01, n_epochs=1000, random_state=42)
>>> reg.fit(X, y)
>>>
>>> # Predict
>>> reg.predict([[6]])
Methods
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
get_capabilities
(cls) -> List[str]
__repr__
(self) -> str