我正在尝试为SVR模型获取最佳参数集.我想使用GridSearchCV超过不同的值C.但是,从之前的测试中我发现,分成训练/测试集高可影响整体表现(在这种情况下为r2).为了解决这个问题,我想实现重复的5倍交叉验证(10 x 5CV).是否有内置的方式来执行它GridSearchCV?
快速解决方案:
遵循sci-kit 官方文档中提出的想法,快速解决方案代表:
NUM_TRIALS = 10
scores = []
for i in range(NUM_TRIALS):
cv = KFold(n_splits=5, shuffle=True, random_state=i)
clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=cv)
scores.append(clf.best_score_)
print "Average Score: {0} STD: {1}".format(numpy.mean(scores), numpy.std(scores))
Run Code Online (Sandbox Code Playgroud) 目前我有以下代码:
我首先将数据集分为训练集和测试集。然后我运行 GridSearchCV 来尝试找到最佳参数。找到最佳参数后,我通过 cross_val_score 使用参数评估分类器。这是一个可以接受的方法吗?
作为安然项目的一部分,构建了附加模型,下面是步骤的摘要,
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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
使用Kbest查找分数并对功能进行排序并尝试更高和更低分数的组合.
使用StratifiedShuffle将SVM与GridSearch一起使用
使用best_estimator_来预测和计算精度和召回率.
问题是估算器正在吐出完美的分数,在某些情况下是1 …
python machine-learning scikit-learn cross-validation grid-search