You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中提取GLM模型的系数(含截距)

问题描述

我在Python中使用statsmodels训练了如下GLM模型:

fitGlm = smf.glm( listOfInModelFeatures,
              family=sm.families.Binomial(),data=train, freq_weights = train['model_weight']).fit()

随后输出模型摘要:

print(fitGlm.summary())

得到的报告如下:

Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                 Target   No. Observations:              1065046
Model:                            GLM   Df Residuals:               4361436.81
Model Family:                Binomial   Df Model:                            8
Link Function:                  Logit   Scale:                          1.0000
Method:                          IRLS   Log-Likelihood:            -6.1870e+05
Date:                Wed, 21 Aug 2024   Deviance:                   1.2374e+06
Time:                        10:27:37   Pearson chi2:                 4.01e+06
No. Iterations:                     8   Pseudo R-squ. (CS):             0.1479
Covariance Type:            nonrobust                                         
===============================================================================
                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       3.2619      0.003   1126.728      0.000       3.256       3.268
e1_a_11_sp      0.9318      0.004    256.254      0.000       0.925       0.939
sp_g_37         0.5850      0.006    102.522      0.000       0.574       0.596
sp_f3_35        0.6510      0.005    135.114      0.000       0.642       0.660
e1_a_07_sp      0.4930      0.006     79.698      0.000       0.481       0.505
e1_e_02_sp      0.9956      0.008    120.253      0.000       0.979       1.012
e1_b_03_sp      0.7493      0.013     56.539      0.000       0.723       0.775
e2_k_02_spa     0.4996      0.014     34.512      0.000       0.471       0.528
ea5_s_01_sp     0.3305      0.008     41.524      0.000       0.315       0.346
===============================================================================

我需要获取包含截距在内的各特征系数列表,格式如下:

[3.2619,0.9318,0.5850,0.6510,0.4930,0.9956,0.7493,0.4996,0.3305]
解决方案

直接使用拟合模型对象的params属性,该属性会返回包含截距和所有特征系数的Series,之后通过tolist()方法即可转换为列表:

coef_list = fitGlm.params.tolist()
print(coef_list)

如果需要和摘要保持一致的四位小数精度,可以用列表推导式处理:

coef_list = [round(coef, 4) for coef in fitGlm.params.tolist()]
print(coef_list)

执行后就能得到目标格式的系数列表。


内容的提问来源于stack exchange,提问作者Giampaolo Levorato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 13:00:04