简体   繁体   中英

Why the auc is so different from logistic regression of sklearn and R

I use a same dataset to train logistic regression model both in R and python sklearn. The dataset is unbalanced. And I find that the auc is quite different. This is the code of python:

model_logistic = linear_model.LogisticRegression() #auc 0.623
model_logistic.fit(train_x, train_y)
pred_logistic = model_logistic.predict(test_x) #mean:0.0235 var:0.023
print "logistic auc: ", sklearn.metrics.roc_auc_score(test_y,pred_logistic)

This is the code of R:

glm_fit <- glm(label ~ watch_cnt_7 + bid_cnt_7 + vi_cnt_itm_1 + 
ITEM_PRICE  + add_to_cart_cnt_7 + offer_cnt_7 +     
 dwell_dlta_4to2 + 
 vi_cnt_itm_2 + asq_cnt_7 + watch_cnt_14to7 + dwell_dlta_6to4 + 
auct_type + vi_cnt_itm_3 + vi_cnt_itm_7 + vi_dlta_4to2 + 
vi_cnt_itm_4 + vi_dlta_6to4 + tenure + sum_SRCH_item_7 + 
vi_cnt_itm_6 + dwell_itm_3 + 
offer_cnt_14to7 + #
dwell_itm_2 + dwell_itm_6 + CNDTN_ROLLUP_ID +
dwell_itm_5 + dwell_itm_4 + dwell_itm_1+ 
bid_cnt_14to7 + item_prchsd_cnt_14to7 +  #
dwell_itm_7  + median_day_rate + vb_ratio
, data = train, family=binomial())
p_lm<-predict(glm_fit, test[1:nc-1],type = "response" )
pred_lm <- prediction(p_lm,test$label)
auc <- performance(pred_lm,'auc')@y.values

The auc of python is 0.623 while the R is 0.887. So I want to know what's wrong with sklearn logistic regression and how to fix it. Thanks.

在python脚本中,您应该使用predict_proba来获取两个类的概率估计值,并将正类的第二列作为roc_auc_score的输入,因为ROC曲线是通过改变概率阈值来绘制的。

pred_logistic = model_logistic.predict_proba(test_x)[:,1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM