嵌套交叉验证:不确定我是否理解这段代码,以及它是否真的在最后展示了最好的模型

如何解决嵌套交叉验证:不确定我是否理解这段代码,以及它是否真的在最后展示了最好的模型

所以我正在做一个嵌套交叉验证来检查优化器、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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?