Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multivariate polynomial regression in javascript?

How can multivariate linear regression be adapted to do multivariate polynomial regression in Javascript? This means that the input X is a 2-D array, predicting a y target that is a 1-D array.

The python way is to do it with sklearn.preprocessing.PolynomialFeatures, followed by a Linear Regression: http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html

The ml.js library only does simple polynomial regression, that is it can only take in a 1-D input and 1-D output. https://github.com/mljs/regression-polynomial

Here is an example of working code in Python scikit-learn for multivariate polynomial regression, where X is a 2-D array and y is a 1-D vector.

Here is example code:

const math = require('mathjs');
const PolynomialRegression = require('ml-regression-polynomial');

const a1 = math.random([10,2]);
const a2 = math.reshape(math.range(0, 20, 1), [10, 2]);
const x = math.add(a1, a2).valueOf();
const y = [];
for (i = 0; i<5; i++){ y.push(0); }
for (i = 5; i<10; i++){ y.push(1); }
const poly = new PolynomialRegression(x, y, 2);
console.log(poly.predict([[3,3],[4,4]]))

outputs

[ NaN, NaN ]
like image 226
mikal94305 Avatar asked Aug 16 '26 10:08

mikal94305


1 Answers

Might be too late for the OP but maybe I can help others with my answer. I had the same problem when I had to develop a data science related app at work. Since there was no library for multivariate polynomial regression (in javascript) I implemented my own:

https://www.npmjs.com/package/@rainij/polynomial-regression-js

It is not as popular as big libraries like tensorflow but it is well tested and my current company uses it. It is also actively maintained and I plan to maintain it for the foreseeable future.

The implementation is inspired by scikit-learn. The main class PolynomialRegressor is actually implemented in terms of another class PolynomialFeatures. But instead of directly using a library for linear regression I depend on another library from mljs which provides singular value decomposition.

like image 130
Reinhard Stahn Avatar answered Aug 17 '26 22:08

Reinhard Stahn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!