代码之家  ›  专栏  ›  技术社区  ›  jmuhlenkamp

如何从python pygam.lineargam中提取截距参数

  •  0
  • jmuhlenkamp  · 技术社区  · 5 年前

    我想从pygam的模型拟合中提取拟合参数。这是一个可重复的例子。

    from pygam import LinearGAM, s, f
    from pygam.datasets import wage
    X, y = wage()
    gam = LinearGAM(s(0) + s(1) + f(2)).fit(X, y)
    

    以下是我尝试过的一些事情。

    #gam.summary() ## This does not show it.
    #gam.intercept_ ## This does not exit.
    #gam.terms.info ## This does not contain it.
    #gam.partial_dependence(-1) ## This raises an error.
    

    下面是一个相关的github问题,它似乎没有在顶部实现: https://github.com/dswah/pyGAM/issues/85

    0 回复  |  直到 5 年前
        1
  •  2
  •   jmuhlenkamp    5 年前

    DR

    • 默认值将截距存储为系数的最后一个,并且可以通过 gam.coef_[-1]
    • 这个 terms 可以打印属性来验证此行为。
    • 导入可以更明确 pygam.intercept 包括在你的公式中(例如 gam = LinearGAM(intercept + s(0) + s(1) + f(2)).fit(X, y) )

    违约行为和条款

    默认值将截距存储为系数的最后一个,并且可以通过 伽玛系数[1] . 打印 条款 属性来验证。

    from pygam import LinearGAM, s, f
    from pygam.datasets import wage
    X, y = wage()
    gam = LinearGAM(s(0) + s(1) + f(2)).fit(X, y)
    print(gam.terms)
    # s(0) + s(1) + f(2) + intercept
    print(gam.coef_[-1])
    # 96.31496573750117
    

    显式声明截获

    在公式中显式地包含截距是一个好主意,这样就不会依赖截距作为系数的最后一个元素。

    from pygam import intercept
    gam = LinearGAM(intercept + s(0) + s(1) + f(2)).fit(X, y)
    print(gam.terms)
    # intercept + s(0) + s(1) + f(2)
    print(gam.coef_[0])
    # 96.31499924945388