【问题标题】:Nest cross validation for predictions using groups使用组进行预测的嵌套交叉验证
【发布时间】:2020-02-10 16:59:35
【问题描述】:

我无法做某事,我想知道这是错误还是正常方式。

我正在尝试对数据集进行嵌套交叉验证,每个数据集都属于一个患者。为了避免对同一患者进行学习和测试,我看到您实施了“组”机制,而 GroupKFold 在我的情况下似乎是正确的。 当我的分类器获得不同的参数时,我继续使用 GridSearchCv 来修复我的模型的超参数。同样,我认为测试/训练必须属于不同的患者。

(对于嵌套交叉验证感兴趣的人:http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html

我是这样进行的:

pipe = Pipeline([('pca', PCA()),
                 ('clf', SVC()),
                 ])
# Find the best parameters for both the feature extraction and the classifier
grid_search = GridSearchCV(estimator=pipe, param_grid=some_param, cv=GroupKFold(n_splits=5), verbose=1)
grid_search.fit(X=features, y=labels, groups=groups)

# Nested CV with parameter optimization
predictions = cross_val_predict(grid_search, X=features, y=labels, cv=GroupKFold(n_splits=5), groups=groups)

然后得到一些:

File : _split.py", line 489, in _iter_test_indices
    raise ValueError("The 'groups' parameter should not be None.")
ValueError: The 'groups' parameter should not be None.

在代码中,_fit_and_predict() 方法似乎没有将组共享给估算器,因此无法使用所需的组。

我能提供一些线索吗? 祝你今天过得愉快, 最好的问候

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    我遇到了同样的问题,除了以更实际的方式实施之外,我找不到其他方法:

    outer_cv = GroupKFold(n_splits=4).split(X_data, y_data, groups=groups)
    nested_cv_scores = []
    for train_ids, test_ids in outer_cv:
        inner_cv = GroupKFold(n_splits=4).split(X_data[train_ids, :], y_data.iloc[train_ids], groups=groups[train_ids])
    
        rf = RandomForestClassifier()
        rf_random = RandomizedSearchCV(estimator=rf, param_distributions=random_grid, n_iter=100,
                                       cv=inner_cv, verbose=2, random_state=42,
                                       n_jobs=-1, scoring=my_squared_score)
        # Fit the random search model
        rf_random.fit(X_data[train_ids, :], y_data.iloc[train_ids])
        print(rf_random.best_params_)
    
        nested_cv_scores.append(rf_random.score(X_data[test_ids,:], y_data.iloc[test_ids]))
    
    print("Nested cv score - meta learning: " + str(np.mean(nested_cv_scores)))
    

    我希望这会有所帮助。

    最好的问候, 费利克斯

    【讨论】:

    • 谢谢你的回答,我的做法和你一样,但我不想影响人们的行为。我想确定没有“更简单”的替代方案
    猜你喜欢
    • 2017-03-16
    • 2017-12-03
    • 1970-01-01
    • 2020-03-26
    • 2018-07-04
    • 2018-03-20
    • 2019-02-07
    • 2020-03-03
    • 2020-11-07
    相关资源
    最近更新 更多