Using explicit (predefined) validation set for grid search with sklearn

前端 未结 3 1628
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 17:38

I have a dataset, which has previously been split into 3 sets: train, validation and test. These sets have to be used as given in order to compare the performance across dif

3条回答
  •  情话喂你
    2020-12-07 18:25

    Consider using the hypopt Python package (pip install hypopt) for which I am an author. It's a professional package created specifically for parameter optimization with a validation set. It works with any scikit-learn model out-of-the-box and can be used with Tensorflow, PyTorch, Caffe2, etc. as well.

    # Code from https://github.com/cgnorthcutt/hypopt
    # Assuming you already have train, test, val sets and a model.
    from hypopt import GridSearch
    param_grid = [
      {'C': [1, 10, 100], 'kernel': ['linear']},
      {'C': [1, 10, 100], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
     ]
    # Grid-search all parameter combinations using a validation set.
    opt = GridSearch(model = SVR(), param_grid = param_grid)
    opt.fit(X_train, y_train, X_val, y_val)
    print('Test Score for Optimized Parameters:', opt.score(X_test, y_test))
    

    EDIT: I (think I) received -1's on this response because I'm suggesting a package that I authored. This is unfortunate, given that the package was created specifically to solve this type of problem.

提交回复
热议问题