如何解决嵌套交叉验证:不确定我是否理解这段代码,以及它是否真的在最后展示了最好的模型
所以我正在做一个嵌套交叉验证来检查优化器、batch_size 和数据集上的时代数。
我正在使用此处解释的代码:https://machinelearningmastery.com/nested-cross-validation-for-machine-learning-with-python/
我们的教授展示了一些与此类似的代码,并暗示它将在内循环和外循环结束时返回最佳模型。但我怎么看,它只是内部循环中最好的 10 个。还是我读错了?
如果我是对的,想在嵌套交叉验证结束时选择一个模型,我该怎么做?我应该选择返回最好结果的模型吗?
或者我读错了,是最后一个恢复的模型,这应该是最好的模型(我认为这不是来自博客文章和教授解释的内容,这不清楚,他们将这段代码呈现为返回最佳模型):
# enumerate splits
outer_results = list()
for train_ix,test_ix in cv_outer.split(X):
# split data
X_train,X_test = X[train_ix,:],X[test_ix,:]
y_train,y_test = Y[train_ix],Y[test_ix]
# configure the inner cross-validation procedure
cv_inner = KFold(n_splits=10,shuffle=True,random_state=1)
# define the model
model = my_model
# define search space : A dictionary containing the parameters with the values we want the model to try on and apply grid search
space = {'batch_size': [10],'nb_epoch': [25],'optimizer': ['RMSprop']}
# define search
search = gridsearchcv(model,space,scoring='accuracy',cv=cv_inner,refit=True) #,cv=cv_inner
# execute search
result = search.fit(X_train,y_train)
# get the best performing model fit on the whole training set
best_model = result.best_estimator_
# evaluate model on the hold out dataset
yhat = best_model.predict(X_test)
# evaluate the model
acc = accuracy_score(y_test,yhat)
# store the result
outer_results.append(acc)
# report progress
print('>acc=%.3f,est=%.3f,cfg=%s' % (acc,result.best_score_,result.best_params_))
# summarize the estimated performance of the model
print('Accuracy: %.3f (%.3f)' % (mean(outer_results),std(outer_results)))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。