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, and evaluates with R², MSE, and MAE metrics.
Deepbox Modules Used
deepbox/mldeepbox/ndarraydeepbox/metricsdeepbox/preprocessWhat 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 { mae, mse, r2Score } from "deepbox/metrics";2import { LinearRegression } from "deepbox/ml";3import { tensor } from "deepbox/ndarray";4import { trainTestSplit } from "deepbox/preprocess";56console.log("=== Linear Regression ===\n");78// Generate synthetic data for demonstration9// True relationship: y = 2x + 3 + noise10const X_data: number[][] = [];11const y_data: number[] = [];1213for (let i = 0; i < 100; i++) {14 const x = i / 10;15 // Add random noise to make it realistic16 const y = 2 * x + 3 + (Math.random() - 0.5) * 2;17 X_data.push([x]);18 y_data.push(y);19}2021// Convert to tensors22const X = tensor(X_data);23const y = tensor(y_data);2425console.log(`Dataset: ${X.shape[0]} samples, ${X.shape[1]} features\n`);2627// Split data: 80% training, 20% testing28const [X_train, X_test, y_train, y_test] = trainTestSplit(X, y, {29 testSize: 0.2,30 randomState: 42,31});3233console.log(`Training set: ${X_train.shape[0]} samples`);34console.log(`Test set: ${X_test.shape[0]} samples\n`);3536// Create and train the linear regression model37const model = new LinearRegression();38model.fit(X_train, y_train);3940console.log("Model trained!");41console.log(`Coefficients: ${model.coef?.toString()}`);42console.log(`Intercept: ${model.intercept}\n`);4344// Generate predictions on test set45const y_pred = model.predict(X_test);4647// Calculate performance metrics48const r2 = r2Score(y_test, y_pred);49const mseValue = mse(y_test, y_pred);50const maeValue = mae(y_test, y_pred);5152console.log("Model Performance:");53console.log(`R² Score: ${r2.toFixed(4)}`);54console.log(`MSE: ${mseValue.toFixed(4)}`);55console.log(`MAE: ${maeValue.toFixed(4)}`);5657console.log("\n✓ Linear regression complete!");Console Output
$ npx tsx 07-linear-regression/index.ts
=== Linear Regression ===
Dataset: 100 samples, 1 features
Training set: 80 samples
Test set: 20 samples
Model trained!
Coefficients: tensor([1.964], dtype=float64)
Intercept: tensor(3.332, dtype=float64)
Model Performance:
R² Score: 0.9878
MSE: 0.4068
MAE: 0.5537
✓ Linear regression complete!