作为安然项目的一部分,构建了附件中的模型,以下是步骤摘要,
下面的模型给出高度完美的分数
cv = StratifiedShuffleSplit(n_splits = 100, test_size = 0.2, random_state = 42)gcv = GridSearchCV(pipe, clf_params,cv=cv)gcv.fit(features,labels) ---> with the full datasetfor train_ind, test_ind in cv.split(features,labels): x_train, x_test = features[train_ind], features[test_ind] y_train, y_test = labels[train_ind],labels[test_ind] gcv.best_estimator_.predict(x_test)
下面的模型给出更合理但较低的分数
cv = StratifiedShuffleSplit(n_splits = 100, test_size = 0.2, random_state = 42)gcv = GridSearchCV(pipe, clf_params,cv=cv)gcv.fit(features,labels) ---> with the full datasetfor train_ind, test_ind in cv.split(features,labels): x_train, x_test = features[train_ind], features[test_ind] y_train, y_test = labels[train_ind],labels[test_ind] gcv.best_estimator_.fit(x_train,y_train) gcv.best_estimator_.predict(x_test)
-
使用Kbest来找出分数并对特征进行排序,尝试高分和低分的组合。
-
使用带有StratifiedShuffle的GridSearch的SVM
-
使用best_estimator_进行预测并计算精度和召回率。
问题是估计器输出了完美的分数,在某些情况下是1
但是当我重新拟合最佳分类器到训练数据上然后运行测试时,它给出了合理的分数。
我的疑问/问题是,网格搜索在使用我们发送给它的Shuffle split对象进行分割后,到底对测试数据做了什么。我假设它不会在测试数据上进行任何拟合,如果这是真的,那么当我使用相同的测试数据进行预测时,不应该得到这么高的分数,对吗?因为我使用了random_state值,shufflesplit应该为网格拟合和预测创建了相同的副本。
那么,使用相同的Shufflesplit两次是否有问题?
回答:
基本上,网格搜索会:
- 尝试你的参数网格中的每一种组合
- 对于每一种组合,它将进行K折交叉验证
- 选择最佳的可用选项。
所以你的第二种情况是正确的。否则,你实际上是在预测你已经训练过的数据(而在第二种选项中则不是这样,你只保留了来自网格搜索的最佳参数)