MuhammadLab
Machine learning studioRegression Studio — Linear vs Polynomial vs Ridge vs Lasso

Regression Studio

Compare linear, polynomial, ridge, lasso, and elastic net regression on the same dataset. This runs entirely in your browser for teaching and experimentation.

Students can see how changing the regression type or degree changes the learned equation and the fitted curve. Coefficients and metrics are computed locally.

Dataset

One dataset, many regressions

Sample points
11 points loadedBrowser-local

Model selection

Choose a regression type

Visualisation

Data + fitted curve

Blue line is the fitted model. Green points are the dataset. Changing type/degree updates the equation and curve instantly.

Equation and metrics

What did we learn?

The equation below is what the model learned during training. For inference, we plug in a new \(x\) (or encoded features) to compute \(\\hat y\).

Equation
y = 0.859091 + 0.906364·x
MSE
0.1059
MAE
0.2656
0.9873
Generated code (Python)
scikit-learn equivalent
# Regression Studio (teaching tool)
# Fit regression models and print coefficients

# Hyperparameters (edit and re-run):
REGRESSION_TYPE = "linear"  # linear | polynomial | ridge | lasso | elastic-net
DEGREE = 1
LAMBDA = 1
L1_RATIO = 0.5
X_COLUMN = "x"
Y_COLUMN = "y"
ENCODING = "numeric"  # numeric | label | onehot
STANDARDIZE_X = False

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.preprocessing import PolynomialFeatures, OneHotEncoder, StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.compose import ColumnTransformer

# Option A: use embedded sample arrays (no CSV required)
X = np.array([0,1,2,3,4,5,6,7,8,9,10]).reshape(-1, 1)
y = np.array([1.2,2.1,2.7,3.5,3.9,5.2,5.9,7.3,7.9,9.1,10.5])

# Option B (recommended for real datasets): load CSV
# df = pd.read_csv('your_data.csv')
# X_df = df[[X_COLUMN]]
# y = df[Y_COLUMN].to_numpy()

model = LinearRegression()
model.fit(X, y)

print('R^2:', model.score(X, y))
print(model)

Teaching note: coefficient display differs by model/pipeline. This studio focuses on the learned equation and fit visualisation in the browser.

Math walkthrough

Training + inference, explained

This section is intentionally larger and slower-paced so students can follow the calculations. Matrices are truncated for readability.

Mode: Numeric / polynomial
1) Model form

Polynomial model (degree 1):

\(\\hat y = \\beta_0 + \\beta_1 x + \\beta_2 x^2 + \\dots\)

Each feature is \(x^d\). The equation card uses the learned \(\\beta\) values.

2) Objective (what training minimizes)

OLS / Polynomial:

min \(\\lVert y - X\\beta \\rVert^2\)

Ridge (L2):

min \(\\lVert y - X\\beta \\rVert^2 + \\lambda \\lVert \\beta \\rVert_2^2\)

Lasso / Elastic Net:

min \(\\lVert y - X\\beta \\rVert^2 + \\lambda (\\alpha\\lVert\\beta\\rVert_1 + (1-\\alpha)\\lVert\\beta\\rVert_2^2)\\)

OLS/Ridge use matrix algebra; Lasso/Elastic Net use coordinate descent (iterative updates).

3) Design matrix X (first rows)

Showing a truncated view of X.

[1, 0]
[1, 1]
[1, 2]
[1, 3]
[1, 4]
[1, 5]
4) Normal-equation ingredients

OLS: \(\\beta = (X^T X)^-1 X^T y\). Ridge: \(X^T X + \\lambda I\) (intercept not penalized).

[11, 55]
[55, 385]
5) Inference example (plug in x)
Compute
Feature row \(\\phi(x)\):
[1, 0^1]
\(\\hat y = \\phi(x) \\cdot \\beta\) = 0.8591
Same dot-product used for predictions after training.