Ridge
Module: strata.linear_model • Kind: struct • Traits: Copyable, Movable, Regressor, Serializable
Source: strata/linear_model/ridge.mojo
struct Ridge[compute_dtype: DType = DType.float64](Copyable, Movable, Regressor, Serializable)from strata.linear_model import RidgeRidge regression with L2 regularization.
Minimizes the penalized objective function:
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, alpha: Scalar[Self.compute_dtype] = 1.0, fit_intercept: Bool = True, solver: String = "auto",)Initialize the Ridge regression estimator.
| Argument | Type | Description |
|---|---|---|
alpha |
Scalar[Self.compute_dtype] |
Regularization strength (must be non-negative). Default 1.0. |
fit_intercept |
Bool |
Whether to calculate the intercept bias term. Default True. |
solver |
String |
Solver algorithm (‘auto’, ‘cholesky’, ‘svd’, ‘solve’). Default ‘auto’. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
coef_ |
Weight vector coefficients of length . |
intercept_ |
Independent bias intercept term. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
Ridge.fit() |
Fit the Ridge regression model from training data. |
Ridge.predict() |
Predict continuous target values using the fitted linear model. |
Ridge.serialize() |
Serializes Ridge parameters and fitted state into BufferWriter. |
Ridge.deserialize() |
Deserializes Ridge from BufferReader. |
Method Details
Section titled “Method Details”Ridge.fit()
Section titled “Ridge.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 the Ridge regression model from training data.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Training feature matrix of shape . |
y |
List[Scalar[in_target_dtype]] |
Target values vector of length . |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Ridge.predict()
Section titled “Ridge.predict()”def predict[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> List[Scalar[feat_dtype]]
def predict[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> List[Scalar[feat_dtype]]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predict continuous target values using the fitted linear model.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape to predict on. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Returns: List[Scalar[feat_dtype]] — List[Scalar[feat_dtype]]: Predicted target vector of length .
Ridge.serialize()
Section titled “Ridge.serialize()”def serialize(self, mut writer: BufferWriter)Serializes Ridge parameters and fitted state into BufferWriter.
Ridge.deserialize()
Section titled “Ridge.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes Ridge from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.linear_model import Ridgefrom strata.core import Matrix
var model = Ridge[DType.float64](alpha=0.5)model.fit(X_train, y_train)var preds = model.predict(X_test)