Skip to content

SGDClassifier

Module: strata.linear_model • Kind: struct • Traits: Classifier, Copyable, Movable
Source: strata/linear_model/sgd_classifier.mojo

struct SGDClassifier[compute_dtype: DType = DType.float64](Classifier, Copyable, Movable)
from strata.linear_model import SGDClassifier

Linear classifier with SGD training.

Supports linear SVM (loss='hinge'), Logistic Regression (loss='log_loss'), Modified Huber (loss='modified_huber'), and Squared Hinge (loss='squared_hinge'). Minimizes the regularized loss:

min⁡W,b1N∑i=1NL(Wxi+b,yi)+αR(W)\min_{W, b} \frac{1}{N} \sum_{i=1}^N \mathcal{L}(W x_i + b, y_i) + \alpha \mathcal{R}(W)
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(
out self,
loss: String = "hinge",
penalty: String = "l2",
alpha: Scalar[Self.compute_dtype] = 1e-4,
l1_ratio: Scalar[Self.compute_dtype] = 0.15,
fit_intercept: Bool = True,
max_iter: Int = 1000,
tol: Scalar[Self.compute_dtype] = 1e-3,
shuffle_data: Bool = True,
epsilon: Scalar[Self.compute_dtype] = 0.1,
random_state: Int = 42,
learning_rate: String = "optimal",
eta0: Scalar[Self.compute_dtype] = 0.01,
power_t: Scalar[Self.compute_dtype] = 0.5,
)

Initialize the SGDClassifier estimator.

Argument Type Description
loss String Loss function to use (‘hinge’, ‘log_loss’, ‘modified_huber’, ‘squared_hinge’). Default ‘hinge’.
penalty String Regularization penalty (‘l2’, ‘l1’, ‘elasticnet’, ‘none’). Default ‘l2’.
alpha Scalar[Self.compute_dtype] Regularization constant multiplier (α≥0\alpha \ge 0). Default 1e-4.
l1_ratio Scalar[Self.compute_dtype] ElasticNet mixing parameter in [0, 1]. Default 0.15.
fit_intercept Bool Whether to estimate an independent intercept term. Default True.
max_iter Int Maximum number of passes over training data (epochs). Default 1000.
tol Scalar[Self.compute_dtype] Stopping criterion threshold for loss changes. Default 1e-3.
shuffle_data Bool Whether to shuffle data per epoch. Default True.
epsilon Scalar[Self.compute_dtype] Epsilon parameter for huber loss. Default 0.1.
random_state Int Seed for random shuffling. Default 42.
learning_rate String Learning rate schedule (‘optimal’, ‘constant’, ‘invscaling’, ‘adaptive’). Default ‘optimal’.
eta0 Scalar[Self.compute_dtype] Initial learning rate. Default 0.0.
power_t Scalar[Self.compute_dtype] Exponent for inverse scaling schedule. Default 0.5.

Attribute Description
classes_ Unique sorted class labels observed in training data.
coef_ Learned weights matrix of shape (K,D)(K, D) (or (1,D)(1, D) for binary).
intercept_ Learned bias vector of length KK (or 1 for binary).
n_iter_ Actual number of epochs executed before convergence.
is_fitted Boolean flag indicating if estimator has been fitted.
n_features_in_ Number of features seen during fitting.

Method Description
SGDClassifier.fit() Fit linear model classifier with Stochastic Gradient Descent.
SGDClassifier.decision_function() Predict linear margin decision function.
SGDClassifier.predict_proba() Probability estimates for each class.
SGDClassifier.predict() Predict class labels for samples in X.

def fit[feat_dtype: DType, in_target_dtype: DType](
mut self,
X: Matrix[feat_dtype],
y: List[Scalar[in_target_dtype]],
)
def fit[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
)

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Fit linear model classifier with Stochastic Gradient Descent.

Parameter Type Description
X Matrix[feat_dtype] Feature matrix.
y List[Scalar[in_target_dtype]] Target vector / class labels.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y))

def decision_function[feat_dtype: DType](
self,
X: Matrix[feat_dtype],
) -> Matrix[feat_dtype]

Predict linear margin decision function.

Parameter Type Description
X Matrix[feat_dtype] Feature matrix.

Returns: Matrix[feat_dtype]


def predict_proba[feat_dtype: DType](
self,
X: Matrix[feat_dtype],
) -> Matrix[feat_dtype]
def predict_proba[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Matrix[feat_dtype]

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Probability estimates for each class.

Parameter Type Description
X Matrix[feat_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y))

Returns: Matrix[feat_dtype]


def predict[feat_dtype: DType](self, X: Matrix[feat_dtype]) -> List[Int]
def predict[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> List[Int]

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Predict class labels for samples in X.

Parameter Type Description
X Matrix[feat_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y))
from strata.linear_model import SGDClassifier
from strata.core import Matrix
var clf = SGDClassifier[DType.float64](loss="log_loss", penalty="l2")
clf.fit(X_train, y_train)
var preds = clf.predict(X_test)