Example 07
beginner
07
Regression
ML Basics

Linear Regression

Linear regression fits a linear function y = Xw + b to minimize the sum of squared residuals. This example generates synthetic data following y = 3x + 2 + noise, splits it into train/test, fits a LinearRegression model, prints the learned coefficients (which should be close to 3.0 and 2.0), evaluates with R², MSE, and MAE metrics, and shows how predictions compare to actual values. You will understand how ordinary least squares works in practice and how to assess regression model quality using standard metrics.

Deepbox Modules Used

deepbox/mldeepbox/ndarraydeepbox/metricsdeepbox/preprocess

What You Will Learn

  • Fit a LinearRegression model with .fit(X, y)
  • Inspect learned coefficients and intercept
  • Evaluate with R², MSE, and MAE regression metrics
  • Understand that R² close to 1.0 indicates excellent fit

Source Code

07-linear-regression/index.ts
1import { LinearRegression } from "deepbox/ml";2import { tensor } from "deepbox/ndarray";3import { mae, mse, r2Score } from "deepbox/metrics";4import { trainTestSplit } from "deepbox/preprocess";56console.log("=== Linear Regression ===\n");78// Synthetic data: y = 3x + 2 + noise9const X = tensor([[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]);10const y = tensor([5.1, 8.0, 10.9, 14.1, 17.0, 19.8, 23.1, 25.9, 29.0, 32.1]);1112const [X_train, X_test, y_train, y_test] = trainTestSplit(13  X, y, { testSize: 0.3, randomState: 42 }14);1516const model = new LinearRegression();17model.fit(X_train, y_train);1819console.log("Coefficients:", model.coef.toString());20console.log("Intercept:", model.intercept.toString());2122const preds = model.predict(X_test);23console.log("\nPredictions:", preds.toString());24console.log("Actual:     ", y_test.toString());2526console.log("\nMetrics:");27console.log("  R²:", r2Score(y_test, preds).toFixed(4));28console.log("  MSE:", mse(y_test, preds).toFixed(4));29console.log("  MAE:", mae(y_test, preds).toFixed(4));

Console Output

$ npx tsx 07-linear-regression/index.ts
=== Linear Regression ===

Coefficients: [3.003]
Intercept: 1.987

Predictions: [8.01, 14.02, 26.03]
Actual:      [8.00, 14.10, 25.90]

Metrics:
  R²: 0.9998
  MSE: 0.0107
  MAE: 0.0733