Skip to content

glassbox.models.linear_model._base

Abstract BaseLinearModel.


BaseLinearModel

BaseLinearModel(
    learning_rate=0.01,
    max_epochs=1000,
    tol=1e-06,
    schedule=CONSTANT,
)

Bases: BaseModel

Abstract base class for linear models trained with gradient-based optimization.

Parameters:

Name Type Description Default
learning_rate float

Initial learning rate used by the optimizer.

0.01
max_epochs int

Maximum number of optimization epochs.

1000
tol float

Convergence tolerance used by stopping criteria.

1e-6
schedule LearningSchedule

Strategy used to update the learning rate across epochs.

LearningSchedule.CONSTANT

Initialize shared linear-model hyperparameters and learned coefficients.

Parameters:

Name Type Description Default
learning_rate float

Initial learning rate used by the optimizer.

0.01
max_epochs int

Maximum number of optimization epochs.

1000
tol float

Convergence tolerance used by stopping criteria.

1e-6
schedule LearningSchedule

Strategy used to update the learning rate across epochs.

LearningSchedule.CONSTANT
Source code in glassbox/models/linear_model/_base.py
def __init__(
    self,
    learning_rate: float = 0.01,
    max_epochs: int = 1000,
    tol: float = 1e-6,
    schedule: LearningSchedule = LearningSchedule.CONSTANT,
) -> None:
    """
    Initialize shared linear-model hyperparameters and learned coefficients.

    Parameters
    ----------
    learning_rate : float, default=0.01
        Initial learning rate used by the optimizer.
    max_epochs : int, default=1000
        Maximum number of optimization epochs.
    tol : float, default=1e-6
        Convergence tolerance used by stopping criteria.
    schedule : LearningSchedule, default=LearningSchedule.CONSTANT
        Strategy used to update the learning rate across epochs.
    """
    if learning_rate <= 0:
        raise ValueError("learning_rate must be strictly positive")
    if max_epochs <= 0:
        raise ValueError("max_epochs must be strictly positive")
    if tol < 0:
        raise ValueError("tol must be non-negative")

    self.learning_rate = learning_rate
    self.max_epochs = max_epochs
    self.tol = tol
    self.schedule = schedule
    self.weights: np.ndarray = np.array([])
    self.bias: float = 0.0

fit abstractmethod

fit(X, y)

Fit the linear model to training data.

Parameters:

Name Type Description Default
X ndarray

Training feature matrix of shape (n_samples, n_features).

required
y ndarray

Training target vector of shape (n_samples,).

required

Returns:

Type Description
Self

The fitted model instance.

Source code in glassbox/models/linear_model/_base.py
@abstractmethod
def fit(self, X: np.ndarray, y: np.ndarray) -> Self:
    """
    Fit the linear model to training data.

    Parameters
    ----------
    X : np.ndarray
        Training feature matrix of shape (n_samples, n_features).
    y : np.ndarray
        Training target vector of shape (n_samples,).

    Returns
    -------
    Self
        The fitted model instance.
    """
    raise NotImplementedError

predict abstractmethod

predict(X, **kwargs)

Predict target values for input samples.

Parameters:

Name Type Description Default
X ndarray

Input feature matrix of shape (n_samples, n_features).

required
**kwargs Any

Additional keyword arguments for prediction.

{}

Returns:

Type Description
ndarray

Predicted values of shape (n_samples,).

Source code in glassbox/models/linear_model/_base.py
@abstractmethod
def predict(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
    """
    Predict target values for input samples.

    Parameters
    ----------
    X : np.ndarray
        Input feature matrix of shape (n_samples, n_features).
    **kwargs : Any
        Additional keyword arguments for prediction.

    Returns
    -------
    np.ndarray
        Predicted values of shape (n_samples,).
    """
    raise NotImplementedError