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 SGDClassifierLinear 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:
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”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 (). 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. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
classes_ |
Unique sorted class labels observed in training data. |
coef_ |
Learned weights matrix of shape (or for binary). |
intercept_ |
Learned bias vector of length (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. |
Methods Overview
Section titled “Methods Overview”| 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. |
Method Details
Section titled “Method Details”SGDClassifier.fit()
Section titled “SGDClassifier.fit()”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 unifieddataset: Datasetcontainer.
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)) |
SGDClassifier.decision_function()
Section titled “SGDClassifier.decision_function()”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]
SGDClassifier.predict_proba()
Section titled “SGDClassifier.predict_proba()”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 unifieddataset: Datasetcontainer.
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]
SGDClassifier.predict()
Section titled “SGDClassifier.predict()”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 unifieddataset: Datasetcontainer.
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)) |
Returns: List[Int]
Section titled “Returns: List[Int]”Example
Section titled “Example”from strata.linear_model import SGDClassifierfrom 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)