Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14699

Porting polynomial regression from R to python

I am trying to replicate a polynomial regression from R to python, but I am not getting the same results:

R example:

x = seq(1,100)
y = x^2 + 3*x + 7
fit = lm(y~poly(x,2,raw=TRUE))
> fit

Call:
lm(formula = y ~ poly(x, 2, raw = TRUE))

Coefficients:
            (Intercept)  poly(x, 2, raw = TRUE)1  poly(x, 2, raw = TRUE)2  
                      7                        3                        1  

Python example

>>> import numpy as np 
>>> x = np.arange(1,101)
>>> y = x^2 + 3*x + 7
>>> fit = np.polyfit(x,y,2)
>>> fit
array([  2.14390707e-02,   1.00652305e+00,   3.49914904e+01])

What am I missing?

Upvotes: 2

Views: 133

Answers (1)

Stefan Reinhardt
Stefan Reinhardt

Reputation: 622

Just for the completeness as @cel didn't put it as answer yet. You have to write y = x**2 + 3*x + 7 in python

Upvotes: 2

Related Questions