I'm trying to understand how to replicate the poly() function in R using scikit-learn (or other module).
For example, let's say I have a vector in R:
a <- c(1:10)
And I want to generate 3rd degree polynomial:
polynomial <- poly(a, 3)
I get the following:
1 2 3
[1,] -0.49543369 0.52223297 -0.4534252
[2,] -0.38533732 0.17407766 0.1511417
[3,] -0.27524094 -0.08703883 0.3778543
[4,] -0.16514456 -0.26111648 0.3346710
[5,] -0.05504819 -0.34815531 0.1295501
[6,] 0.05504819 -0.34815531 -0.1295501
[7,] 0.16514456 -0.26111648 -0.3346710
[8,] 0.27524094 -0.08703883 -0.3778543
[9,] 0.38533732 0.17407766 -0.1511417
[10,] 0.49543369 0.52223297 0.4534252
I'm relatively new to python and I'm trying understand how to utilize the PolynomiaFeatures
function in sklearn to replicate this. I've spent time time looking at examples at the PolynomialFeatures
documentation but I'm still a bit confused.
Any insight would be greatly appreciated. Thanks!
It turns out that you can replicate the result of R's
poly(x,p)
function by performing a QR decomposition of a matrix whose columns are the powers of the input vectorx
from the 0th power (all ones) up to thep
th power. The Q matrix, minus the first constant column, gives you the result you want.So, the following should work:
In particular: