@rainij/polynomial-regression-js
v1.4.0
Published
JavaScript/TypeScript library for multivariate polynomial regression.
Downloads
742
Maintainers
Readme
polynomial-regression-js is a typescript library for linear and polynomial regression in multiple variables. It provides a class PolynomialRegressor for multivariate polynomial regression and a class PolynomialFeatures for transforming input features $(x_1,x_2,\ldots,x_n)$ into polynomial features $(\ldots,x_1^{k_1}x_2^{k_2}\ldots x_n^{k_n},\ldots)$.
API documentation is created using TypeDoc.
Installation
npm install --save @rainij/polynomial-regression-js
Usage
PolynomialRegressor
import { PolynomialRegressor } from '@rainij/polynomial-regression-js';
// Y0 = X0^2 + 2*X0*X1, Y1 = X1^2 + 5*X0 + 1
// Quadratric functions with two inputs need (at least) seven supporting points:
const x = [[0, 0], [1, 0], [2, 0], [0, 1], [0, 2], [1, 1], [2, 2]];
const y = [[0, 1], [1, 6], [4, 11], [0, 2], [0, 5], [3, 7], [12, 15]];
// Search for a polynomial model of degree = 2.
const model = new PolynomialRegressor(2);
model.fit(x,y) // Training
console.log(model.predict([[3, 3]]));
// [ [27, 25] ]
PolynomialFeatures
import { PolynomialFeatures } from '@rainij/polynomial-regression-js';
const x = [[3, 2]] // Two features: [[a, b]]
// Generate polynomial features up to degree 3
let polyFeatures = new PolynomialFeatures(3);
console.log(polyFeatures.fitTransform(x));
// [ [27, 18, 9, 12, 6, 3, 8, 4, 2, 1] ]
// That is: [ [a^3, a^2b, ab^2, ab, a, b^3, b^2, b, 1] ]