LogisticRegression
Module: strata.linear_model • Kind: struct • Traits: Classifier, Copyable, Movable, Serializable
Source: strata/linear_model/logistic_regression.mojo
struct LogisticRegression[compute_dtype: DType = DType.float64](Classifier, Copyable, Movable, Serializable)from strata.linear_model import LogisticRegressionLogistic Regression classifier with L2 regularization.
Supports binary and multiclass (multinomial) classification by minimizing the regularized cross-entropy loss with gradient optimization:
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, penalty: String = "l2", C: Scalar[Self.compute_dtype] = 1.0, fit_intercept: Bool = True, max_iter: Int = 100, tol: Scalar[Self.compute_dtype] = 1e-4, learning_rate: Scalar[Self.compute_dtype] = 0.1,)Initialize the LogisticRegression estimator.
| Argument | Type | Description |
|---|---|---|
penalty |
String |
Regularization norm (‘l2’ or ‘none’). Default ‘l2’. |
C |
Scalar[Self.compute_dtype] |
Inverse regularization strength (must be strictly positive). Default 1.0. |
fit_intercept |
Bool |
Whether to calculate the intercept bias term. Default True. |
max_iter |
Int |
Maximum number of optimization iterations. Default 100. |
tol |
Scalar[Self.compute_dtype] |
Tolerance for stopping criterion. Default 1e-4. |
learning_rate |
Scalar[Self.compute_dtype] |
Initial step size for gradient updates. Default 0.1. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
classes_ |
Sorted list of unique class labels seen during fit. |
coef_ |
Learned weight coefficient matrix of shape . |
intercept_ |
Learned bias intercept vector of length . |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
LogisticRegression.fit() |
Fits the logistic regression model on training data (X, y). |
LogisticRegression.predict_proba() |
Predict class probability distributions for samples in X. |
LogisticRegression.predict() |
Predict discrete class labels for samples in X. |
LogisticRegression.serialize() |
Serializes LogisticRegression parameters and fitted state into BufferWriter. |
LogisticRegression.deserialize() |
Deserializes LogisticRegression from BufferReader. |
Method Details
Section titled “Method Details”LogisticRegression.fit()
Section titled “LogisticRegression.fit()”def fit[feat_dtype: DType, target_dtype: DType]( mut self, X: Matrix[feat_dtype], y: List[Scalar[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.
Fits the logistic regression model on training data (X, y).
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Training feature matrix (N x D). |
y |
List[Scalar[target_dtype]] |
Discrete target class labels (length N). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
LogisticRegression.predict_proba()
Section titled “LogisticRegression.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.
Predict class probability distributions for samples in X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape . |
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] — Matrix[feat_dtype]: Probability matrix of shape , where row contains the normalized probability distribution over classes.
LogisticRegression.predict()
Section titled “LogisticRegression.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 discrete class labels for samples in X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape . |
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] — List[Int]: Predicted class labels vector of length .
LogisticRegression.serialize()
Section titled “LogisticRegression.serialize()”def serialize(self, mut writer: BufferWriter)Serializes LogisticRegression parameters and fitted state into BufferWriter.
LogisticRegression.deserialize()
Section titled “LogisticRegression.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes LogisticRegression from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.linear_model import LogisticRegressionfrom strata.core import Matrix
var clf = LogisticRegression[DType.float64](C=1.0, max_iter=200)clf.fit(X_train, y_train)var probs = clf.predict_proba(X_test)var preds = clf.predict(X_test)