---
title: "Polynomial Regression in JavaScript with @kanaries/ml"
description: "Fit nonlinear numeric trends with the PolynomialRegression JavaScript and TypeScript implementation in @kanaries/ml for browser and Node.js regression workflows."
canonical_url: "https://ml.kanaries.net/docs/apis/linear/polynomialRegression"
markdown_url: "https://ml.kanaries.net/docs/apis/linear/polynomialRegression.md"
---
# Polynomial Regression in JavaScript

## Algorithm overview

Polynomial regression expands each numeric feature into powers of that feature, then fits a linear model on the expanded matrix. It is useful when a linear baseline is too simple but the target still follows a smooth curve.

Use it for small numeric regression problems where the degree of curvature is known or easy to tune.

## JavaScript implementation

`@kanaries/ml` provides `Linear.PolynomialRegression` as a JavaScript estimator with `fit` and `predict`. It runs in browser or Node.js and keeps polynomial feature expansion inside the model.

## Interactive polynomial regression playground

Adjust the polynomial degree, data shape, and noise below. The curve is fitted live with `Linear.PolynomialRegression`; click the chart to test how an added observation changes the fit.

> The HTML version includes an interactive Polynomial Regression playground powered by @kanaries/ml. You can change the dataset, noise, and model controls; add observations; and inspect live predictions plus train and holdout metrics. The runnable guide and API reference continue below. [Open the HTML page](https://ml.kanaries.net/docs/apis/linear/polynomialRegression).

## Quick start example

```ts
import { Linear } from '@kanaries/ml';

const X = [[0], [1], [2], [3]];
const y = [1, 2, 5, 10];

const model = new Linear.PolynomialRegression({ degree: 2 });
model.fit(X, y);
const pred = model.predict([[4]]);
console.log(pred);
```

## Detailed API reference

```ts
new Linear.PolynomialRegression(props?: { degree?: number })
```

Options:

- `degree?: number`, default `2`. Must be an integer greater than or equal to 1.

Methods:

- `fit(X: number[][], Y: number[]): void`
- `predict(X: number[][]): number[]`

The implementation expands every input feature to powers from `1` through `degree`, fits ordinary least squares, and stores an intercept plus coefficients internally.
