7

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 ]
mikal94305
  • 4,663
  • 8
  • 31
  • 40
  • Is it a possible solution to call Python from your code? – James Phillips Nov 05 '18 at 11:51
  • No, otherwise I would not be asking this question – mikal94305 Nov 05 '18 at 23:06
  • `Given the limited and immature libraries for machine learning in Javascript` what are you talking about? – Michal Nov 09 '18 at 18:12
  • The question needs more details. Can you show the code you used in sklearn? You used `PolynomialFeatures` on X (independent variables) or `y` (dependent)? What was the shape of `X` and `y` before `PolynomialFeatures`? Was it 1-d? The js library library linked does the same. It takes a 1-d X, generates polynomial upto specified degree, and use the new data to regress with `y`. Why dont you use it? Have you used it? Have you seen the coefficients learnt by it? Or are you talking about a 2-d `y` having multiple targets (dependents) in it? – Vivek Kumar Nov 13 '18 at 13:22
  • Multivariate means X is 2D – mikal94305 Nov 15 '18 at 06:11
  • Well in that case, as I said above the [mljs library calls `solve(A, b)`](https://github.com/mljs/regression-polynomial/blob/master/src/index.js#L121) from `ml-matrix` library for regression, which can take 2-d A (X in your case). So although not already available, it should not be hard to modify the code to get the polynomial features from the input X, then sending them to `solve(A, b)` to get the coefficients and apply them during prediction. Try that, show some code and error you encounter and SO can be more helpful. – Vivek Kumar Nov 15 '18 at 07:32

1 Answers1

0

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.