"OverflowError: Python int too large to convert to C long" when running a RandomizedSearchCV with scipy distributions

"OverflowError: Python int too large to convert to C long" when running a RandomizedSearchCV with scipy distributions

我想运行以下RandomizedSearch:

from scipy.stats import reciprocal, uniform

tree_reg = DecisionTreeRegressor()

param_grid = {
    "max_depth": np.arange(1, 12, 1),
    "min_samples_leaf": np.arange(2, 2259, 10),
    "min_samples_split": np.arange(2, 2259, 2),
    "max_leaf_nodes": np.arange(2, 2259, 2),
    "max_features": np.arange(2, len(features))
    }

rnd_search_tree = RandomizedSearchCV(tree_reg, param_grid,cv=cv_split, n_iter=10000,
                                    scoring=['neg_root_mean_squared_error', 'r2'], refit='neg_root_mean_squared_error',
                                    return_train_score=True, verbose=2)

rnd_search_tree.fit(dataset_prepared_stand, dataset_labels)

其中 2259 是我拥有的数据点数。但是,我收到以下错误:

OverflowError                             Traceback (most recent call last)
<ipython-input-809-76074980f31c> in <module>
     13                                     return_train_score=True, verbose=2)
     14 
---> 15 rnd_search_tree.fit(dataset_prepared_stand, dataset_labels)

~\anaconda3\envs\data_analysis\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

~\anaconda3\envs\data_analysis\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    734                 return results
    735 
--> 736             self._run_search(evaluate_candidates)
    737 
    738         # For multi-metric evaluation, store the best_index_, best_params_ and

~\anaconda3\envs\data_analysis\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
   1529         evaluate_candidates(ParameterSampler(
   1530             self.param_distributions, self.n_iter,
-> 1531             random_state=self.random_state))

~\anaconda3\envs\data_analysis\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params)
    698 
    699             def evaluate_candidates(candidate_params):
--> 700                 candidate_params = list(candidate_params)
    701                 n_candidates = len(candidate_params)
    702 

~\anaconda3\envs\data_analysis\lib\site-packages\sklearn\model_selection\_search.py in __iter__(self)
    283                 n_iter = grid_size
    284             for i in sample_without_replacement(grid_size, n_iter,
--> 285                                                 random_state=rng):
    286                 yield param_grid[i]
    287 

sklearn\utils\_random.pyx in sklearn.utils._random.sample_without_replacement()

OverflowError: Python int too large to convert to C long

如果我只带走一个要搜索的参数(或者如果我将范围的步长减少到 1000,例如),我不明白。有没有办法通过我想尝试的所有值来解决它?

除了删除 RandomizedSearchCV,我看不到其他选择。在内部 RandomSearchCV 调用 sample_without_replacement 以从您的特征 space 中采样。当您的特征 space 大于 C 的 long 大小时,scikit-learn 的 sample_without_replacement 就会崩溃。

幸运的是,无论如何,随机搜索有点糟糕。查看 optuna 作为替代方案。在你的特征 space 中花时间评估的地方更聪明(更多地关注高性能区域),并且不需要你事先限制你的特征 space 精度(也就是说,你可以省略步长)。更一般地说,查看 AutoML 领域。

但是,如果您坚持随机搜索,则必须找到另一种实现方式。其实,optuna也是supports a random sampler