LASSO_REG

Lasso regression adds an L1 penalty to squared-error fitting, which can shrink some coefficients exactly to zero and provide sparse linear models. It is useful when feature selection and prediction need to be combined in one estimator.

The model estimates coefficients by minimizing the objective:

\min_{w} \frac{1}{2 n_{\text{samples}}} ||y - Xw||^2_2 + \alpha ||w||_1

where \alpha is the L1 regularization strength.

This wrapper accepts tabular feature data with rows as samples and columns as features, plus a numeric target supplied as a single row or single column. It returns the training R^2 together with fitted predictions, residuals, and learned coefficient arrays.

Excel Usage

=LASSO_REG(data, target, alpha, fit_intercept, max_iter, tol, coord_selection, random_state)
  • data (list[list], required): 2D array of numeric feature data with rows as samples and columns as features.
  • target (list[list], required): Numeric target values as a single row, single column, or scalar when only one sample is present.
  • alpha (float, optional, default: 1): L1 regularization strength applied to the regression model.
  • fit_intercept (bool, optional, default: true): Whether to include an intercept term in the linear model.
  • max_iter (int, optional, default: 1000): Maximum number of coordinate-descent iterations.
  • tol (float, optional, default: 0.0001): Optimization tolerance used for convergence checks.
  • coord_selection (str, optional, default: “cyclic”): Coordinate update order used by the optimizer.
  • random_state (int, optional, default: null): Integer seed used when random coordinate selection is enabled. Leave blank for the estimator default.

Returns (dict): Excel data type containing training R^2, predictions, residuals, and fitted coefficient arrays.

Example 1: Fit lasso regression on a two-feature linear trend

Inputs:

data target alpha fit_intercept max_iter tol coord_selection random_state
0 0 1 0.01 true 5000 0.0001 cyclic 0
1 0 3
0 1 4
1 1 6
2 1 8
2 2 11

Excel formula:

=LASSO_REG({0,0;1,0;0,1;1,1;2,1;2,2}, {1;3;4;6;8;11}, 0.01, TRUE, 5000, 0.0001, "cyclic", 0)

Expected output:

{"type":"Double","basicValue":0.999979,"properties":{"training_r2":{"type":"Double","basicValue":0.999979},"mean_squared_error":{"type":"Double","basicValue":0.000231079},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.0204}],[{"type":"Double","basicValue":3.01371}],[{"type":"Double","basicValue":4.00395}],[{"type":"Double","basicValue":5.99726}],[{"type":"Double","basicValue":7.99057}],[{"type":"Double","basicValue":10.9741}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.0204023}],[{"type":"Double","basicValue":-0.0137109}],[{"type":"Double","basicValue":-0.00394922}],[{"type":"Double","basicValue":0.00274219}],[{"type":"Double","basicValue":0.00943359}],[{"type":"Double","basicValue":0.0258867}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.99331},{"type":"Double","basicValue":2.98355}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":1.0204}]]},"dual_gap":{"type":"Double","basicValue":0.000109009},"iteration_count":{"type":"Double","basicValue":10}}}

Example 2: Flatten a single-row numeric target range for lasso regression

Inputs:

data target alpha fit_intercept max_iter tol coord_selection random_state
0 1 3 5 7 9 11 0.01 true 5000 0.0001 cyclic 0
1
2
3
4
5

Excel formula:

=LASSO_REG({0;1;2;3;4;5}, {1,3,5,7,9,11}, 0.01, TRUE, 5000, 0.0001, "cyclic", 0)

Expected output:

{"type":"Double","basicValue":0.999997,"properties":{"training_r2":{"type":"Double","basicValue":0.999997},"mean_squared_error":{"type":"Double","basicValue":0.0000342857},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.00857}],[{"type":"Double","basicValue":3.00514}],[{"type":"Double","basicValue":5.00171}],[{"type":"Double","basicValue":6.99829}],[{"type":"Double","basicValue":8.99486}],[{"type":"Double","basicValue":10.9914}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.00857143}],[{"type":"Double","basicValue":-0.00514286}],[{"type":"Double","basicValue":-0.00171429}],[{"type":"Double","basicValue":0.00171429}],[{"type":"Double","basicValue":0.00514286}],[{"type":"Double","basicValue":0.00857143}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.99657}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":1.00857}]]},"dual_gap":{"type":"Double","basicValue":-2.80086e-19},"iteration_count":{"type":"Double","basicValue":2}}}

Example 3: Fit a no-intercept lasso model on a two-feature plane

Inputs:

data target alpha fit_intercept max_iter tol coord_selection random_state
1 0 2 0.01 false 5000 0.0001 cyclic 0
0 1 3
1 1 5
2 1 7
1 2 8
2 2 10

Excel formula:

=LASSO_REG({1,0;0,1;1,1;2,1;1,2;2,2}, {2;3;5;7;8;10}, 0.01, FALSE, 5000, 0.0001, "cyclic", 0)

Expected output:

{"type":"Double","basicValue":0.999993,"properties":{"training_r2":{"type":"Double","basicValue":0.999993},"mean_squared_error":{"type":"Double","basicValue":0.0000582248},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.99754}],[{"type":"Double","basicValue":2.99656}],[{"type":"Double","basicValue":4.9941}],[{"type":"Double","basicValue":6.99163}],[{"type":"Double","basicValue":7.99066}],[{"type":"Double","basicValue":9.98819}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":0.00246392}],[{"type":"Double","basicValue":0.00343861}],[{"type":"Double","basicValue":0.00590253}],[{"type":"Double","basicValue":0.00836645}],[{"type":"Double","basicValue":0.00934114}],[{"type":"Double","basicValue":0.0118051}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.99754},{"type":"Double","basicValue":2.99656}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":0}]]},"dual_gap":{"type":"Double","basicValue":0.000648993},"iteration_count":{"type":"Double","basicValue":22}}}

Example 4: Use random coordinate updates on correlated features

Inputs:

data target alpha fit_intercept max_iter tol coord_selection random_state
0 0.1 1 0.05 true 5000 0.0001 random 0
1 0.9 3.1
2 2.1 5.8
3 2.9 7.9
4 4.1 10.2
5 4.9 12.1

Excel formula:

=LASSO_REG({0,0.1;1,0.9;2,2.1;3,2.9;4,4.1;5,4.9}, {1;3.1;5.8;7.9;10.2;12.1}, 0.05, TRUE, 5000, 0.0001, "random", 0)

Expected output:

{"type":"Double","basicValue":0.998857,"properties":{"training_r2":{"type":"Double","basicValue":0.998857},"mean_squared_error":{"type":"Double","basicValue":0.0169803},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.16159}],[{"type":"Double","basicValue":3.17116}],[{"type":"Double","basicValue":5.67855}],[{"type":"Double","basicValue":7.68812}],[{"type":"Double","basicValue":10.1955}],[{"type":"Double","basicValue":12.2051}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.161595}],[{"type":"Double","basicValue":-0.0711582}],[{"type":"Double","basicValue":0.121448}],[{"type":"Double","basicValue":0.211885}],[{"type":"Double","basicValue":0.00449154}],[{"type":"Double","basicValue":-0.105072}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.0139},{"type":"Double","basicValue":1.24457}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":1.03714}]]},"dual_gap":{"type":"Double","basicValue":0.00147917},"iteration_count":{"type":"Double","basicValue":1367}}}

Python Code

import numpy as np
from sklearn.linear_model import Lasso as SklearnLasso

def lasso_reg(data, target, alpha=1, fit_intercept=True, max_iter=1000, tol=0.0001, coord_selection='cyclic', random_state=None):
    """
    Fit a lasso regression model and return training predictions.

    See: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): 2D array of numeric feature data with rows as samples and columns as features.
        target (list[list]): Numeric target values as a single row, single column, or scalar when only one sample is present.
        alpha (float, optional): L1 regularization strength applied to the regression model. Default is 1.
        fit_intercept (bool, optional): Whether to include an intercept term in the linear model. Default is True.
        max_iter (int, optional): Maximum number of coordinate-descent iterations. Default is 1000.
        tol (float, optional): Optimization tolerance used for convergence checks. Default is 0.0001.
        coord_selection (str, optional): Coordinate update order used by the optimizer. Valid options: Cyclic, Random. Default is 'cyclic'.
        random_state (int, optional): Integer seed used when random coordinate selection is enabled. Leave blank for the estimator default. Default is None.

    Returns:
        dict: Excel data type containing training $R^2$, predictions, residuals, and fitted coefficient arrays.
    """
    def py(value):
        return value.item() if isinstance(value, np.generic) else value

    def cell(value):
        value = py(value)
        if isinstance(value, bool):
            return {"type": "Boolean", "basicValue": bool(value)}
        if isinstance(value, (int, float)) and not isinstance(value, bool):
            return {"type": "Double", "basicValue": float(value)}
        return {"type": "String", "basicValue": str(value)}

    def col(values):
        return [[cell(value)] for value in values]

    def mat(values):
        return [[cell(value) for value in row] for row in values]

    def parse_data(value):
        value = [[value]] if not isinstance(value, list) else value
        if not isinstance(value, list) or not value or not all(isinstance(row, list) and row for row in value):
            return None, "Error: data must be a non-empty 2D list"
        if len({len(row) for row in value}) != 1:
            return None, "Error: data must be a rectangular 2D list"
        data_np = np.array(value, dtype=float)
        if data_np.ndim != 2 or data_np.size == 0:
            return None, "Error: data must be a non-empty 2D list"
        if not np.isfinite(data_np).all():
            return None, "Error: data must contain only finite numeric values"
        return data_np, None

    def parse_target(value, sample_count):
        if not isinstance(value, list):
            labels = [value]
        elif not value:
            return None, "Error: target must be non-empty"
        elif all(not isinstance(item, list) for item in value):
            labels = value
        elif len(value) == 1:
            labels = value[0]
        elif all(isinstance(row, list) and len(row) == 1 for row in value):
            labels = [row[0] for row in value]
        else:
            return None, "Error: target must be a single row or column"

        if len(labels) != sample_count:
            return None, "Error: target length must match sample count"

        parsed = []
        for item in labels:
            item = py(item)
            if isinstance(item, bool) or not isinstance(item, (int, float)):
                return None, "Error: target values must be finite numeric scalars"
            if not np.isfinite(float(item)):
                return None, "Error: target values must be finite numeric scalars"
            parsed.append(float(item))
        return np.array(parsed, dtype=float), None

    def flat_float_list(values):
        return [float(py(item)) for item in np.asarray(values).reshape(-1).tolist()]

    try:
        data_np, error = parse_data(data)
        if error:
            return error

        target_np, error = parse_target(target, data_np.shape[0])
        if error:
            return error

        selection_value = str(coord_selection).strip().lower()
        if selection_value not in {"cyclic", "random"}:
            return "Error: coord_selection must be 'cyclic' or 'random'"
        if float(alpha) < 0:
            return "Error: alpha must be non-negative"
        if int(max_iter) < 1:
            return "Error: max_iter must be at least 1"
        if float(tol) <= 0:
            return "Error: tol must be greater than 0"

        fitted = SklearnLasso(
            alpha=float(alpha),
            fit_intercept=bool(fit_intercept),
            max_iter=int(max_iter),
            tol=float(tol),
            selection=selection_value,
            random_state=None if random_state in (None, "") else int(random_state)
        ).fit(data_np, target_np)

        prediction_array = np.asarray(fitted.predict(data_np), dtype=float)
        residual_array = target_np - prediction_array
        predictions = flat_float_list(prediction_array)
        residuals = flat_float_list(residual_array)
        training_r2 = float(fitted.score(data_np, target_np))
        mse = float(np.mean(np.square(residual_array)))
        dual_gap = float(np.atleast_1d(fitted.dual_gap_).reshape(-1)[0])
        iteration_count = float(np.atleast_1d(fitted.n_iter_).reshape(-1)[0])

        return {
            "type": "Double",
            "basicValue": training_r2,
            "properties": {
                "training_r2": {"type": "Double", "basicValue": training_r2},
                "mean_squared_error": {"type": "Double", "basicValue": mse},
                "sample_count": {"type": "Double", "basicValue": float(data_np.shape[0])},
                "feature_count": {"type": "Double", "basicValue": float(data_np.shape[1])},
                "predictions": {"type": "Array", "elements": col(predictions)},
                "residuals": {"type": "Array", "elements": col(residuals)},
                "coefficients": {"type": "Array", "elements": mat(np.atleast_2d(fitted.coef_).tolist())},
                "intercepts": {"type": "Array", "elements": col(np.atleast_1d(fitted.intercept_).tolist())},
                "dual_gap": {"type": "Double", "basicValue": dual_gap},
                "iteration_count": {"type": "Double", "basicValue": iteration_count}
            }
        }
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of numeric feature data with rows as samples and columns as features.
Numeric target values as a single row, single column, or scalar when only one sample is present.
L1 regularization strength applied to the regression model.
Whether to include an intercept term in the linear model.
Maximum number of coordinate-descent iterations.
Optimization tolerance used for convergence checks.
Coordinate update order used by the optimizer.
Integer seed used when random coordinate selection is enabled. Leave blank for the estimator default.