python - 使用 scipy 发行版运行 RandomizedSearchCV 时出现 "OverflowError: Python int too large to convert to C long"

标签 python scipy integer-overflow grid-search

我想运行以下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 从特征空间中进行采样。当您的特征空间大于 C 的 long 大小时,scikit-learn 的 sample_without_replacement 就会崩溃。

幸运的是,随机搜索无论如何都很糟糕。查看optuna作为备选。它更智能地决定在特征空间中的哪个位置花费时间进行评估(更多地关注高性能区域),并且不需要您事先限制特征空间精度(即,您可以省略步长)。更一般地说,请查看 AutoML 领域。

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

关于python - 使用 scipy 发行版运行 RandomizedSearchCV 时出现 "OverflowError: Python int too large to convert to C long",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64901096/

相关文章:

python - mpi4py:未加载库:/usr/local/lib/libmpi.1.dylib

python - 访问云端的 S3 文件

python - 如何在 Python 的不同类中使用变量?

c++ - 当 >> 运算符试图输入一个大于变量可以包含的值时会发生什么?

c++ - 如何检测无符号整数溢出?

c++ - 在 C++ 和 Python 之间共享视频数据

python - Avast 将 SciPy 检测为病毒?

python - 是否有使用 scipy 或其他库查找关键的 F 值?

python - IDL 和 Python 之间的 FFT 差异

java - 整数溢出klocwork分析