python - 将多项式回归从 R 移植到 python

标签 python r numpy

我正在尝试将多项式回归从 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/

相关文章:

python - 在 Python 中创建 3 路数据张量并执行 PARAFAC 分解

python - 将稀疏数组从 numpy 读取到分块 dask 数组

python - Python 中 numpy.random 和 random.random 的性能差异

python - 数组中所有一维点与 python diff() 之间的区别?

python - SVM Scikit-Learn : Why prediction time decrease with SVC when increasing parameter C?

python - Pandas Dataframe - 向下移动行并维护数据

python - Windows 上的 Neovim 找不到 python 提供程序

R pdf 设置边距

r - 根据R中上一行的条件将行添加到数据框

仅当列存在时才将其删除