【问题标题】:Python equivalent for R StepAIC for Logistic Regression (direction='Backwards')用于逻辑回归的 R StepAIC 的 Python 等效项(方向='Backwards')
【发布时间】:2018-03-26 14:08:26
【问题描述】:

我试图在 R 中“手动”模拟 stepAIC 函数,但它需要很长时间(我只发布了前两次尝试)。 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())

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

【问题讨论】:

    标签: python logistic-regression statsmodels coefficients


    【解决方案1】:

    从 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_)  
    

    【讨论】:

    【解决方案2】:

    stepAIC 只是找到降低 AIC 的特征组合:AIC 越低越好。所以我认为如果你有固定数量的你想要的功能,你可以使用 OLS 明确比较 AIC

    import statsmodels.api as sm
    #you can explicitly change x, x can be changed with number of features
    regressor_OLS = sm.OLS(Y, x).fit() 
    regressor_OLS.summary()
    regressor_OLS.aic #return AIC value
    

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2016-12-01
      相关资源
      最近更新 更多