python逐步回归调包
时间: 2023-10-30 08:05:55 浏览: 80
利用python实现逐步回归
5星 · 资源好评率100%
在Python中,可以使用statsmodels.api模块中的OLS函数来进行逐步回归的调包。以下是一个示例代码:
```python
import statsmodels.api as sm
def stepwise(X, y, alpha_in=0.1, alpha_out=0.15):
included = []
while True:
changed = False
excluded = list(set(X.columns) - set(included))
p_val = pd.Series(index=excluded)
for new_column in excluded:
model = sm.OLS(y, sm.add_constant(X[included + [new_column]])).fit()
p_val[new_column = model.pvalues[new_column]
min_pval = p_val.min()
# forward step
if min_pval < alpha_in:
changed = True
add_feature = p_val.idxmin()
included.append(add_feature)
print("Add {:20} with p_value {:.6}".format(add_feature, min_pval))
# backward step
model = sm.OLS(y, sm.add_constant(X[included])).fit()
p_val = model.pvalues.iloc
阅读全文