使用Python中的标准回归系数选择重要的预测因子

发布于 2025-02-10 08:44:10 字数 2501 浏览 2 评论 0原文

Minitab博客表示不要使用常规回归系数或p值来确定可变/特征重要性。它说使用标准化的回归系数或R-²的变化。

我想找出如何计算Python中的标准化回归系数,并发现了这个旧的SO 问题使用此代码答案。

import statsmodels.api as sm
from scipy.stats.mstats import zscore

print(sm.OLS(zscore(y), zscore(x)).fit().summary())

示例1-使用我的套索模型

lasso_model = Lasso(alpha = 0.01)    
selected_columns = list(X.columns)
lasso_model.fit(X, y)
list(zip(selected_columns, lasso_model.coef_))

[Out]:

[('AGE', -0.00013116073118093452),
 ('DISTANCE', 2.2924058071269675e-05),
 ('TOTDELAY', -0.0002569583237660659),
 ('STD/STA', -0.01334152988447677),
 ('WEBRSVN', -0.020335870566292973),
 ('WEBCI', 0.08571155146491262),
 ('AIRPTCI', 0.0327097907845398)...]

示例2-使用statsmodels示例从中使用satsmodels示例

import statsmodels.api as sm
from scipy.stats.mstats import zscore

sm.OLS(zscore(y), zscore(X)).fit().summary()

[Out]:

               coef   std err         t    P>|t|    [0.025    0.975]
AGE         -0.0297     0.002   -14.843    0.000    -0.034    -0.026
DISTANCE    -0.0005     0.003    -0.181    0.856    -0.006     0.005
TOTDELAY    -0.0391     0.002   -20.945    0.000    -0.043    -0.035
STD/STA     -0.0528     0.002   -22.003    0.000    -0.058    -0.048
WEBRSVN     -0.1155     0.003   -37.834    0.000    -0.121    -0.110
WEBCI        0.2147     0.003    81.803    0.000     0.210     0.220
AIRPTCI      0.0958     0.002    46.530    0.000     0.092     0.100
...
  1. 是正确的方法,还是自2015年该帖子以来使用Python库有一种最新的方法?这仅在模型选择之前完成,还是可以迭代地完成?
  2. 博客说,使用绝对值(因为可以存在 - +相关性)来确定预测变量是否重要。用于选择重要预测变量的标准COEF值范围是什么?我的COEF都不大于0.3,所以这表明什么?

说要选择预测变量,请确保您在执行回归之前已将数据归一化,并且要获得系数的绝对值。您还可以查看R平方值的更改。

这是否意味着我只需要标准化数据,然后运行套索模型,然后可以使用这些COEF?

这是标准标准()的目的吗?

std = StandardScaler()
std.fit(X.values)
X_tr = std.transform(X.values)

This minitab blog says not to use regular regression coefficients or p-values to determine variable/feature importance. It says to use standardized regression coefficients or changes in R-² instead.

I wanted to find out how to calculate standardized regression coefficients in Python and found this old SO question with this code answer.

import statsmodels.api as sm
from scipy.stats.mstats import zscore

print(sm.OLS(zscore(y), zscore(x)).fit().summary())

Example1 - Using my lasso model

lasso_model = Lasso(alpha = 0.01)    
selected_columns = list(X.columns)
lasso_model.fit(X, y)
list(zip(selected_columns, lasso_model.coef_))

[Out]:

[('AGE', -0.00013116073118093452),
 ('DISTANCE', 2.2924058071269675e-05),
 ('TOTDELAY', -0.0002569583237660659),
 ('STD/STA', -0.01334152988447677),
 ('WEBRSVN', -0.020335870566292973),
 ('WEBCI', 0.08571155146491262),
 ('AIRPTCI', 0.0327097907845398)...]

Example2 - Using statsmodels example from SO

import statsmodels.api as sm
from scipy.stats.mstats import zscore

sm.OLS(zscore(y), zscore(X)).fit().summary()

[Out]:

               coef   std err         t    P>|t|    [0.025    0.975]
AGE         -0.0297     0.002   -14.843    0.000    -0.034    -0.026
DISTANCE    -0.0005     0.003    -0.181    0.856    -0.006     0.005
TOTDELAY    -0.0391     0.002   -20.945    0.000    -0.043    -0.035
STD/STA     -0.0528     0.002   -22.003    0.000    -0.058    -0.048
WEBRSVN     -0.1155     0.003   -37.834    0.000    -0.121    -0.110
WEBCI        0.2147     0.003    81.803    0.000     0.210     0.220
AIRPTCI      0.0958     0.002    46.530    0.000     0.092     0.100
...
  1. Is this the correct method or is there a more recent way using a python library since that post in 2015? This is done only before model selection or can it be done iteratively?
  2. The blog says to use the absolute value (since there can be -+ correlation) to determine if a predictor is important or not. What is the standard coef value range used to select important predictors? None of my coef are larger than 0.3 so what does that indicate?

This statsexchange answer says to select predictors make sure you have normalized the data before you perform regression and you take absolute value of coefficients. You can also look at the change in the R-squared value.

Does that mean I only have to normalize my data then run my lasso model and I can then use those coef?

Is this the purpose of StandardScaler()?

std = StandardScaler()
std.fit(X.values)
X_tr = std.transform(X.values)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文