【问题标题】:Porting polynomial regression from R to python将多项式回归从 R 移植到 python
【发布时间】:2016-03-05 09:34:08
【问题描述】:

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

我错过了什么?

【问题讨论】:

  • x**2 在 python 中
  • @cel 谢谢!就是这样!请将您的评论作为答案,以便我接受。

标签: python r numpy


【解决方案1】:

只是为了完整性,因为@cel 还没有把它作为答案。 你必须在python中写y = x**2 + 3*x + 7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2020-05-08
    • 2018-12-19
    • 1970-01-01
    • 2020-01-31
    • 2012-03-20
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多