Sklearn, gridsearch: how to print out progress during the execution?

后端 未结 3 1280
自闭症患者
自闭症患者 2021-02-01 11:38

I am using GridSearch from sklearn to optimize parameters of the classifier. There is a lot of data, so the whole process of optimization takes a while

3条回答
  •  滥情空心
    2021-02-01 12:11

    Check out the GridSearchCVProgressBar

    Just found it right now and I'm using it. Very into it:

    In [1]: GridSearchCVProgressBar
    Out[1]: pactools.grid_search.GridSearchCVProgressBar
    
    In [2]:
    
    In [2]: ??GridSearchCVProgressBar
    Init signature: GridSearchCVProgressBar(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise', return_train_score='warn')
    Source:
    class GridSearchCVProgressBar(model_selection.GridSearchCV):
        """Monkey patch Parallel to have a progress bar during grid search"""
    
        def _get_param_iterator(self):
            """Return ParameterGrid instance for the given param_grid"""
    
            iterator = super(GridSearchCVProgressBar, self)._get_param_iterator()
            iterator = list(iterator)
            n_candidates = len(iterator)
    
            cv = model_selection._split.check_cv(self.cv, None)
            n_splits = getattr(cv, 'n_splits', 3)
            max_value = n_candidates * n_splits
    
            class ParallelProgressBar(Parallel):
                def __call__(self, iterable):
                    bar = ProgressBar(max_value=max_value, title='GridSearchCV')
                    iterable = bar(iterable)
                    return super(ParallelProgressBar, self).__call__(iterable)
    
            # Monkey patch
            model_selection._search.Parallel = ParallelProgressBar
    
            return iterator
    File:           ~/anaconda/envs/python3/lib/python3.6/site-packages/pactools/grid_search.py
    Type:           ABCMeta
    
    In [3]: ?GridSearchCVProgressBar
    Init signature: GridSearchCVProgressBar(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise', return_train_score='warn')
    Docstring:      Monkey patch Parallel to have a progress bar during grid search
    File:           ~/anaconda/envs/python3/lib/python3.6/site-packages/pactools/grid_search.py
    Type:           ABCMeta
    

提交回复
热议问题