作为安然项目的一部分,构建了附加模型,下面是步骤的摘要,
以下型号可提供非常完美的分数
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 dataset
for 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 dataset
for 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将GridM与GridSearch一起使用
-
使用best_estimator_来预测和计算精度和召回率 .
问题是估算器正在吐出完美的分数,在某些情况下是1
但是当我在训练数据上重新设置最佳分类器然后运行测试时它给出了合理的分数 .
我的疑问/问题是GridSearch在使用我们发送给它的Shuffle拆分对象进行拆分后对测试数据做了什么 . 我认为它不适合测试数据,如果确实如此,那么当我预测使用相同的测试数据时,它不应该给出这么高的分数 . 因为我使用了random_state值,所以shufflesplit应该为Grid适合和预测创建相同的副本 .
那么,是否使用相同的Shufflesplit两个错误?
2 回答
基本上网格搜索将:
尝试参数网格的每个组合
对于他们每个人,它将进行K折交叉验证
选择最佳可用 .
所以你的第二个案例是好的 . 否则,您实际上是在预测您训练过的数据(在第二个选项中不是这种情况,您只保留gridsearch中的最佳参数)
GridSearchCV作为@ Gauthier Feuillen说用于搜索给定数据的估计器的最佳参数 . GridSearchCV的描述: -
gcv = GridSearchCV(pipe, clf_params,cv=cv)
gcv.fit(features,labels)
将扩展
clf_params
以使用ParameterGrid分离所有可能的组合 .features
现在将使用cv
拆分为features_train
和features_test
.labels
相同现在将使用
features_train
和labels_inner
训练gridSearch估算器(管道),并使用features_test
和labels_test
进行评分 .For each possible combination of parameters 在步骤3中, steps 4 and 5 will be repeated 为
cv_iterations
. 将计算cv迭代中的平均得分,其将被分配给该参数组合 . 这可以使用gridSearch的cv_results_
属性访问 .对于给出最佳分数的参数,内部估算器将提供给它(特征和标签) .
Because of last step, you are getting different scores in first and second approach . 因为在第一种方法中,所有数据都用于训练,并且您仅预测该数据 . 第二种方法对先前看不见的数据进行预测 .