{ "cells": [ { "cell_type": "markdown", "id": "0cbb38dc", "metadata": { "tags": [] }, "source": [ "# Getting Started: fitting a Lasso model \n", "\n", "The purpose of this tutorial is to show the basics of `glum`. It assumes a working knowledge of python, regularized linear models, and machine learning. The API is very similar to scikit-learn. After all, `glum` is based on a fork of scikit-learn.\n", "\n", "If you have not done so already, please refer to our [installation instructions](../install.rst) for installing `glum`." ] }, { "cell_type": "code", "execution_count": 1, "id": "0b0a7790", "metadata": { "execution": { "iopub.execute_input": "2026-04-21T09:13:13.935556Z", "iopub.status.busy": "2026-04-21T09:13:13.935348Z", "iopub.status.idle": "2026-04-21T09:13:14.965229Z", "shell.execute_reply": "2026-04-21T09:13:14.964779Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "import sklearn\n", "from sklearn.datasets import fetch_openml\n", "from glum import GeneralizedLinearRegressor, GeneralizedLinearRegressorCV" ] }, { "cell_type": "markdown", "id": "3f664566", "metadata": {}, "source": [ "## Data\n", "\n", "We start by loading the King County housing dataset from openML and splitting it into training and test sets. For simplicity, we don't go into any details regarding exploration or data cleaning." ] }, { "cell_type": "code", "execution_count": 2, "id": "896a2486", "metadata": { "execution": { "iopub.execute_input": "2026-04-21T09:13:14.966705Z", "iopub.status.busy": "2026-04-21T09:13:14.966588Z", "iopub.status.idle": "2026-04-21T09:13:14.995434Z", "shell.execute_reply": "2026-04-21T09:13:14.995074Z" } }, "outputs": [], "source": [ "house_data = fetch_openml(name=\"house_sales\", version=3, as_frame=True)\n", "\n", "# Use only select features\n", "X = house_data.data[\n", " [\n", " \"bedrooms\",\n", " \"bathrooms\",\n", " \"sqft_living\",\n", " \"floors\",\n", " \"waterfront\",\n", " \"view\",\n", " \"condition\",\n", " \"grade\",\n", " \"yr_built\",\n", " ]\n", "].copy()\n", "\n", "# Targets\n", "y = house_data.target" ] }, { "cell_type": "code", "execution_count": 3, "id": "a65eff50", "metadata": { "execution": { "iopub.execute_input": "2026-04-21T09:13:14.996642Z", "iopub.status.busy": "2026-04-21T09:13:14.996574Z", "iopub.status.idle": "2026-04-21T09:13:15.090998Z", "shell.execute_reply": "2026-04-21T09:13:15.090512Z" } }, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(\n", " X, y, test_size = 0.3, random_state=5\n", ")" ] }, { "cell_type": "markdown", "id": "31f29f1b", "metadata": {}, "source": [ "## GLM basics: fitting and predicting using the normal family\n", "\n", "We'll use `glum.GeneralizedLinearRegressor` to predict the house prices using the available predictors. \n", "\n", "We set three key parameters:\n", "\n", "- `family`: the family parameter specifies the distributional assumption of the GLM and, as a consequence, the loss function to be minimized. Accepted strings are 'normal', 'poisson', 'gamma', 'inverse.gaussian', and 'binomial'. You can also pass in an instantiated `glum` distribution (e.g. `glum.TweedieDistribution(1.5)` )\n", "- `alpha`: the constant multiplying the penalty term that determines regularization strength. (*Note*: `GeneralizedLinearRegressor` also has an alpha-search option. See the `GeneralizedLinearRegressorCV` example below for details on how alpha-search works).\n", "- `l1_ratio`: the elastic net mixing parameter (`0 <= l1_ratio <= 1`). For `l1_ratio = 0`, the penalty is the L2 penalty (ridge). ``For l1_ratio = 1``, it is an L1 penalty (lasso). For ``0 < l1_ratio < 1``, the penalty is a combination of L1 and L2.\n", "\n", "To be precise, we will be minimizing the function with respect to the parameters, $\\beta$:\n", "\n", "\\begin{equation}\n", "\\frac{1}{N}(\\mathbf{X}\\beta - y)^2 + \\alpha\\|\\beta\\|_1\n", "\\end{equation}" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa90b816", "metadata": { "execution": { "iopub.execute_input": "2026-04-21T09:13:15.092186Z", "iopub.status.busy": "2026-04-21T09:13:15.092119Z", "iopub.status.idle": "2026-04-21T09:13:15.093713Z", "shell.execute_reply": "2026-04-21T09:13:15.093439Z" } }, "outputs": [], "source": [ "glm = GeneralizedLinearRegressor(family=\"normal\", alpha=0.1, l1_ratio=1)" ] }, { "cell_type": "markdown", "id": "b4dee7fb", "metadata": {}, "source": [ "The `GeneralizedLinearRegressor.fit()` method follows typical sklearn API style and accepts two primary inputs:\n", "\n", "1. `X`: the design matrix with shape `(n_samples, n_features)`.\n", "2. `y`: the `n_samples` length array of target data." ] }, { "cell_type": "code", "execution_count": 5, "id": "ae60a126", "metadata": { "execution": { "iopub.execute_input": "2026-04-21T09:13:15.094724Z", "iopub.status.busy": "2026-04-21T09:13:15.094667Z", "iopub.status.idle": "2026-04-21T09:13:15.118489Z", "shell.execute_reply": "2026-04-21T09:13:15.118147Z" } }, "outputs": [ { "data": { "text/html": [ "
GeneralizedLinearRegressor(alpha=0.1, l1_ratio=1)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
| \n", " | alpha | \n", "0.1 | \n", "
| \n", " | l1_ratio | \n", "1 | \n", "
| \n", " | P1 | \n", "'identity' | \n", "
| \n", " | P2 | \n", "'identity' | \n", "
| \n", " | fit_intercept | \n", "True | \n", "
| \n", " | family | \n", "'normal' | \n", "
| \n", " | link | \n", "'auto' | \n", "
| \n", " | solver | \n", "'auto' | \n", "
| \n", " | max_iter | \n", "100 | \n", "
| \n", " | max_inner_iter | \n", "100000 | \n", "
| \n", " | gradient_tol | \n", "None | \n", "
| \n", " | step_size_tol | \n", "None | \n", "
| \n", " | hessian_approx | \n", "0.0 | \n", "
| \n", " | warm_start | \n", "False | \n", "
| \n", " | alpha_search | \n", "False | \n", "
| \n", " | alphas | \n", "None | \n", "
| \n", " | n_alphas | \n", "100 | \n", "
| \n", " | min_alpha_ratio | \n", "None | \n", "
| \n", " | min_alpha | \n", "None | \n", "
| \n", " | start_params | \n", "None | \n", "
| \n", " | selection | \n", "'cyclic' | \n", "
| \n", " | random_state | \n", "None | \n", "
| \n", " | copy_X | \n", "None | \n", "
| \n", " | check_input | \n", "True | \n", "
| \n", " | verbose | \n", "0 | \n", "
| \n", " | scale_predictors | \n", "False | \n", "
| \n", " | lower_bounds | \n", "None | \n", "
| \n", " | upper_bounds | \n", "None | \n", "
| \n", " | A_ineq | \n", "None | \n", "
| \n", " | b_ineq | \n", "None | \n", "
| \n", " | force_all_finite | \n", "True | \n", "
| \n", " | drop_first | \n", "False | \n", "
| \n", " | robust | \n", "True | \n", "
| \n", " | expected_information | \n", "False | \n", "
| \n", " | formula | \n", "None | \n", "
| \n", " | interaction_separator | \n", "':' | \n", "
| \n", " | categorical_format | \n", "'{name}[{category}]' | \n", "
| \n", " | cat_missing_method | \n", "'fail' | \n", "
| \n", " | cat_missing_name | \n", "'(MISSING)' | \n", "