LinearRegression
API and practical guide for LinearRegression in @kanaries/ml, including when to use it in JavaScript and TypeScript ML workflows.
Linear.LinearRegression
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.
Example
const lr = new LinearRegression();
lr.fit([[0], [1]], [1, 3]);
const pred = lr.predict([[2]]); // about 5Practical guide: LinearRegression in JavaScript and TypeScript
LinearRegression models continuous targets with an interpretable linear relationship between features and outputs.
When to use LinearRegression
- 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.
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
- Prefer feature scaling for distance-based and gradient-based algorithms to improve stability.
- In browser apps, run heavy training in Web Workers to keep UI interactions smooth.
- Keep a simple baseline from the same module as a fallback model for comparison.
Search intents this page targets
LinearRegression JavaScriptLinearRegression TypeScriptLinearRegression browser machine learning@kanaries/ml LinearRegression
FAQ
What problem does LinearRegression solve in JavaScript machine learning projects?
LinearRegression helps teams implement production-ready ML workflows in browser and Node.js environments with a familiar scikit-learn-style API.
When should I choose LinearRegression instead of other Linear algorithms?
Use LinearRegression when it best matches your data shape, labeling strategy, and runtime constraints. Benchmark against at least one alternative in the same module before finalizing defaults.
Can I run LinearRegression in both browser and Node.js with @kanaries/ml?
Yes. @kanaries/ml is designed for JavaScript and TypeScript runtimes across browser applications, server-side Node.js services, and edge-friendly workflows.