Logistic回归的R StepAIC的Python等价物(direction='Backwards')

2024-10-01 11:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图模仿R中的stepAIC函数“手动”执行它,但它花了很多时间(我只发布了前两次尝试)。对于logistic回归,python中是否有类似于stepAIC函数(即在迭代时消除一个p值最高的变量并最小化AIC)?在

#create model with double interactions
>datapol = data.drop(['flag'], axis=1) #elimino colonna flag dai dati
>poly=sklearn.preprocessing.PolynomialFeatures(interaction_only=True,include_bias = >False)

#calculate AIC for model with double interactions 
>m_sat=poly.fit_transform(datapol)
>m1=sm.Logit(np.asarray(flag.astype(int)),m_sat.astype(int))
>m1.fit()
>print(m1.fit().summary2())

#create new model without variable that has p-value>0.05
>mx1=pd.DataFrame(m_sat)
>mx2=np.asarray(mx1.drop(mx1.columns[[3]], axis=1))
>m2=sm.Logit(np.asarray(flag.astype(int)),mx2.astype(int))
>m2.fit()
>print(m2.fit().summary2())

编辑:我发现了一个使用正向https://qiita.com/mytk0u0/items/aa2e3f5a66fe9e2895fa模拟stepAIC的算法


Tags: 函数modelcreatenpaicsatfitint
1条回答
网友
1楼 · 发布于 2024-10-01 11:27:04

检查sklearn包中名为RFE的函数。在

# Running RFE with the output number of the variable equal to 9
lm = LinearRegression()
rfe = RFE(lm, 9)             # running RFE
rfe = rfe.fit(X_train, y_train)
print(rfe.support_)           # Printing the boolean results
print(rfe.ranking_)  

相关问题 更多 >