我分别从sklearn的RandomForestClassifier和roc_curve,auc方法获得不同的ROC-AUC分数。
以下代码让我获得了0.878的ROC-AUC(即gs.best_score_):
def train_model(mod = None, params = None, features = None,
outcome = ...outcomes array..., metric = 'roc_auc'):
gs = GridSearchCV(mod, params, scoring=metric, loss_func=None, score_func=None,
fit_params=None, n_jobs=-1, iid=True, refit=True, cv=10, verbose=0,
pre_dispatch='2*n_jobs', error_score='raise')
gs.fit(...feature set df..., outcome)
print gs.best_score_
print gs.best_params_
return gs
model = RandomForestClassifier(random_state=2000, n_jobs=-1)
features_to_include = [...list of column names...]
parameters = {
'n_estimators': [...list...], 'max_depth':[...list...],
'min_samples_split':[...list...], 'min_samples_leaf':[...list...]
}
gs = train_model(mod = model, params = parameters, features = features_to_include)
然而,下面的代码让我的ROC-AUC为0.97:
fpr = dict()
tpr = dict()
roc_auc = dict()
fpr['micro'], tpr['micro'], _ = roc_curve(...outcomes array...,
gs.predict_proba(...feature set df...)[:, 1])
roc_auc['micro'] = auc(fpr['micro'], tpr['micro'])
为什么会有这样的差异?我的代码有问题吗?
谢谢! 克里斯