Skip to content

getml.predictors.LinearRegression dataclass

LinearRegression(
    learning_rate: float = 0.9, reg_lambda: float = 1e-10
)

Bases: _Predictor

Simple predictor for regression problems.

Learns a simple linear relationship using ordinary least squares (OLS) regression:

\[ \hat{y} = w_0 + w_1 * feature_1 + w_2 * feature_2 + ... \]

The weights are optimized by minimizing the squared loss of the predictions \(\hat{y}\) w.r.t. the target \(y\).

\[ L(y,\hat{y}) = \frac{1}{n} \sum_{i=1}^{n} (y_i -\hat{y}_i)^2 \]

Linear regressions can be trained arithmetically or numerically. Training arithmetically is more accurate, but suffers worse scalability.

If you decide to pass categorical features to the LinearRegression, it will be trained numerically. Otherwise, it will be trained arithmetically.

PARAMETER DESCRIPTION
learning_rate

The learning rate used for training numerically (only relevant when categorical features are included). Range: (0, ∞]

TYPE: float DEFAULT: 0.9

reg_lambda

L2 regularization parameter. Range: [0, ∞]

TYPE: float DEFAULT: 1e-10

validate

validate(params: Optional[dict] = None)

Checks both the types and the values of all instance variables and raises an exception if something is off.

PARAMETER DESCRIPTION
params

A dictionary containing the parameters to validate. If nothing is passed, the default parameters will be validated.

TYPE: Optional[dict] DEFAULT: None

Example
l = getml.predictors.LinearRegression()
l.learning_rate = 8.1
l.validate()
Note

This method is called at end of the __init__ constructor and every time before the predictor - or a class holding it as an instance variable - is sent to the getML Engine.

Source code in getml/predictors/linear_regression.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def validate(self, params: Optional[dict] = None):
    """Checks both the types and the values of all instance
    variables and raises an exception if something is off.

    Args:
        params: A dictionary containing
            the parameters to validate. If nothing is passed,
            the default parameters will be validated.

    ??? example
        ```python
        l = getml.predictors.LinearRegression()
        l.learning_rate = 8.1
        l.validate()
        ```

    Note:
        This method is called at end of the `__init__` constructor
        and every time before the predictor - or a class holding
        it as an instance variable - is sent to the getML Engine.
    """

    if params is None:
        params = self.__dict__
    else:
        params = {**self.__dict__, **params}

    if not isinstance(params, dict):
        raise ValueError("params must be None or a dictionary!")

    _validate_linear_model_parameters(params)