Skip to main content
Linear regression is a fundamental supervised learning algorithm for predicting continuous values. This guide shows you how to train and evaluate a linear regression model.

Building a Linear Regression Model

1
Generate training data
2
Create synthetic data with a linear relationship:
3
import { tensor } from "deepbox/ndarray";
import { trainTestSplit } from "deepbox/preprocess";

// Generate data: y = 2x + 3 + noise
const X_data: number[][] = [];
const y_data: number[] = [];

for (let i = 0; i < 100; i++) {
  const x = i / 10;
  const y = 2 * x + 3 + (Math.random() - 0.5) * 2;
  X_data.push([x]);
  y_data.push(y);
}

const X = tensor(X_data);
const y = tensor(y_data);

console.log(`Dataset: ${X.shape[0]} samples, ${X.shape[1]} features`);
4
Output:
5
Dataset: 100 samples, 1 features
6
Split data
7
Divide into training and test sets:
8
const [X_train, X_test, y_train, y_test] = trainTestSplit(X, y, {
  testSize: 0.2,
  randomState: 42,
});

console.log(`Training set: ${X_train.shape[0]} samples`);
console.log(`Test set: ${X_test.shape[0]} samples`);
9
Output:
10
Training set: 80 samples
Test set: 20 samples
11
Train the model
12
Create and fit a linear regression model:
13
import { LinearRegression } from "deepbox/ml";

const model = new LinearRegression();
model.fit(X_train, y_train);

console.log("Model trained!");
console.log(`Coefficients: ${model.coef?.toString()}`);
console.log(`Intercept: ${model.intercept}`);
14
Output:
15
Model trained!
Coefficients: Tensor([2.0134])
Intercept: 2.9876
16
Evaluate performance
17
Calculate metrics on the test set:
18
import { mae, mse, r2Score } from "deepbox/metrics";

const y_pred = model.predict(X_test);

const r2 = r2Score(y_test, y_pred);
const mseValue = mse(y_test, y_pred);
const maeValue = mae(y_test, y_pred);

console.log("Model Performance:");
console.log(`R² Score: ${r2.toFixed(4)}`);
console.log(`MSE: ${mseValue.toFixed(4)}`);
console.log(`MAE: ${maeValue.toFixed(4)}`);
19
Output:
20
Model Performance:
R² Score: 0.9912
MSE: 0.3421
MAE: 0.4856

Understanding the Metrics

  • R² Score: Proportion of variance explained (1.0 is perfect, 0.0 is baseline)
  • MSE (Mean Squared Error): Average squared difference between predictions and actual values
  • MAE (Mean Absolute Error): Average absolute difference between predictions and actual values

Next Steps

Logistic Regression

Learn classification with logistic regression

Regularization

Prevent overfitting with Ridge and Lasso regression

Build docs developers (and LLMs) love