使用Sklearn,可以使用GridSearchCV测试分类器功能的多个变量,例如:
parameters = {
'learning_rate': [0.001,0.005,0.003],
'n_estimators': [300,800,1200],
'criterion': ['friedman_mse','mse','mae'],
'verbose':[1],
'loss' : ['deviance','exponential'],
'random_state':[0]
}
GBC = GradientBoostingClassifier()
grid = GridSearchCV(GBC, parameters)
grid.fit(X,y ) # X = data, y = result
best_est = grid.best_estimator_
print(best_est)
predictions = best_est.predict(T) # T contains data to apply it on.
但是如果要进行交叉验证怎么办?例如。以类似于train_test_split
的方式:
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=41)
在这里,我们有一个random_state
(可能会产生很大的影响)。
是否可以将GridSearchCV包含一些随机数的数组,以确保它在某些数据的训练/测试拆分的“大多数”随机状态下可以最佳地工作?
出于记录,我知道这不在GridSearchCV内(或据我所知),我在这里问这样的方法可能是什么样。也许有一些聪明的方法可以做到这一点?
答案 0 :(得分:1)
您可以将ShuffleSplit
指定为交叉验证生成器。
例如:
from sklearn.model_selection import GridSearchCV, ShuffleSplit
GBC = GradientBoostingClassifier()
grid = GridSearchCV(GBC,
param_grid=parameters,
cv=ShuffleSplit(train_size=X.shape[0],
test_size=.3,
n_splits=5,
random_state=41))
grid.fit(X, y)