i使用相同的数据集在R和Python Sklearn中训练Logistic回归模型。数据集是不平衡的。我发现AUC完全不同。这是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)
这是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
Python的AUC为0.623,而R为0.887。因此,我想知道Sklearn Logistic回归以及如何修复它是什么问题。谢谢。
在python脚本中,您应该使用 predict_proba
获取两类的概率估计,并将正类别的第二列作为roc_auc_score
的输入,因为ROC曲线是通过更改绘制的概率阈值。
pred_logistic = model_logistic.predict_proba(test_x)[:,1]