我正在尝试将多项式回归从 R 复制到 python,但没有得到相同的结果:
R 示例:
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 示例
>>> 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])
我错过了什么?
最佳答案
只是为了完整性,因为 @cel 尚未将其作为答案。
你必须在Python中编写y = x**2 + 3*x + 7
关于python - 将多项式回归从 R 移植到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812453/