漏掉一个测试

2024-04-26 06:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用一个20x300的小数据集。由于我的数据点太少,我想知道是否可以使用类似的方法,将交叉验证排除在外,但用于测试。你知道吗

我是这么想的:

  1. 训练/测试分割数据,测试集中只有一个数据点。你知道吗
  2. 根据训练数据训练模型,可能使用 网格搜索/交叉验证
  3. 使用第二步到第二步中的最佳模型 对一个数据点进行预测并保存在 数组
  4. 重复上述步骤,直到所有数据点 在测试集中
  5. 计算您选择的首选指标 (准确度、f1分数、auc等)使用这些预测

这种方法的优点是:

  • 你不必为了测试而“保留/浪费”数据,这样你就可以训练了 有更多的数据点。你知道吗

缺点是:

  • 这种方法有潜在的缺陷(?)数据泄漏。你知道吗
  • 您正在根据一系列预测计算精度指标 这可能来自不同的模型,由于网格搜索,所以我不确定它会有多准确。你知道吗

我尝试过训练/测试分割的标准方法,但由于我需要拿出至少5分进行测试,因此我没有足够的分数进行训练,ROC AUC变得非常糟糕。你知道吗

下面是代码,你可以看看我在说什么。你知道吗

for train_index, test_index in loo.split(X):

    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    param_grid = {'C': [1e2, 5e2, 1e3, 5e3, 1e4, 5e4, 1e5, 5e6, 1e6],
              'gamma': [0.00001, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1, 1],
                  'degree': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
                 'kernel': ['rbf', 'linear', 'poly'],
                 'class_weight': ['balanced', None]}

    model = SVC(probability = True)

    skf = StratifiedKFold(n_splits=num_cv_folds)

    grid_search = GridSearchCV(model, param_grid,
            n_jobs=-1,
            cv=skf,
            scoring='roc_auc',
            verbose=0,
            refit=True,
            iid=False)

    grid_search.fit(X_train, y_train)

    best_model = grid_search.best_estimator_
    y_pred_test = best_model.predict_proba(X_test)

    y_preds.append(y_pred_test[0][0])

fpr, tpr, thresholds = roc_curve(y, y_preds_, pos_label=1)
auc_roc1 = auc(fpr, tpr)

我真的很想得到一些关于这种方法是否切实可行以及原因的反馈。你知道吗


Tags: 数据方法模型test网格searchindexmodel