Linear Regression
Learn what Linear Regression does, when to use it, and how to run LinearRegression in JavaScript or TypeScript with @kanaries/ml for browser and Node.js applications.
View as MarkdownAlgorithm overview
LinearRegression models continuous targets with an interpretable linear relationship between features and outputs.
This algorithm is especially useful when:
- You need a transparent baseline for numeric prediction tasks.
- Feature-target relationships are approximately linear after transformation.
- You want fast training and low-latency inference in JavaScript.
JavaScript implementation
@kanaries/ml gives JavaScript teams a straightforward linear regression implementation for numeric prediction tasks that need to stay in the application layer. This is useful for browser-side demos, pricing calculators, forecasting helpers, and Node.js APIs that want transparent coefficients instead of opaque black-box behavior.
Because the model is simple and explainable, it fits especially well in product contexts where engineers and stakeholders need to reason about why a prediction changed.
Interactive linear regression playground
Change the dataset and noise, then click the chart to add your own observation. The fitted line and holdout metrics update immediately using Linear.LinearRegression in your browser.
LinearRegression playground
Adjust the data and model, then click the chart to add a training observation.
Quick start
LinearRegression in Python vs JavaScript / TypeScript
If you searched for "LinearRegression in JavaScript" or "LinearRegression in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.
from sklearn.linear_model import LinearRegression
X = [[0], [1], [2], [3]]
y = [1, 3, 5, 7]
reg = LinearRegression()
reg.fit(X, y)
pred = reg.predict([[4], [5]])import { Linear } from '@kanaries/ml';
const X = [[0], [1], [2], [3]];
const y = [1, 3, 5, 7];
const reg = new Linear.LinearRegression();
reg.fit(X, y);
const pred = reg.predict([[4], [5]]);Quick JavaScript example
import { Linear } from '@kanaries/ml';
const X = [[0], [1], [2], [3]];
const y = [1, 3, 5, 7];
const reg = new Linear.LinearRegression();
reg.fit(X, y);
const pred = reg.predict([[4], [5]]);
console.log(pred);Detailed API reference
constructor()This class implements ordinary least squares linear regression. It estimates coefficients for a linear model by minimizing the squared error between predicted and actual values.
Methods
fit(X: number[][], Y: number[]): voidpredict(X: number[][]): number[]
fit
X- Feature matrix of shape[nSamples, nFeatures].Y- Target values of lengthnSamples.
predict
X- Feature matrix for which to compute predictions.
Implementation workflow
- Prepare numeric features and split into train/validation sets.
- Fit the model and inspect residual patterns for systematic errors.
- Iterate on feature engineering when residuals show non-linear structure.
JavaScript deployment notes
- Use linear regression as a baseline before moving to more complex non-linear models.
- Inspect residuals to decide whether feature engineering or a different model family is needed.
- This estimator is a strong fit for small to medium tabular problems where interpretability matters.
Linear Models
Explore linear regression, logistic regression, regularized regression, and linear classification in JavaScript and TypeScript with @kanaries/ml.
Logistic Regression
Learn what logistic regression does, when to use it, and how to run logistic regression in JavaScript or TypeScript with @kanaries/ml in the browser or Node.js.