首页 > 解决方案 > PermissionError 与 RandomizedSearchCV

问题描述

我正在尝试使用 RandomizedSearchCV 来调整随机森林的超参数,但是在运行代码后我得到了 PermissionError 的瞬间。

最初的运行没有 PermissionError(但是它确实抛出了一个无效的句柄错误),但现在我根本无法运行代码。据我所知,当代码试图在没有适当权限的情况下格式化驱动器时,通常会抛出 WinError 5,但据我所知,RandomizedSearch 并没有试图改变任何东西。我还没有尝试以管理员身份运行,但是访问该帐户会很困难,所以我试图弄清楚是否有其他方法可以解决这个问题。我正在运行 Python 3.7。


n_estimators = [int(x) for x in np.linspace(start=200, stop=2000, num=10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 110, num=11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]

random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf,
               'bootstrap': bootstrap}

print(random_grid)

constructed_data = pd.read_csv('Examples/Test_data.CSV')

forest = RandomForestClassifier()

forest.fit(train, train_labels)

forest_random = RandomizedSearchCV(estimator=forest, param_distributions=random_grid, n_iter=100,
                                   cv=3, verbose=2, n_jobs=-1)

forest_random.fit(train, train_labels)

预期:没有错误和推荐的超参数值

实际的:

Fitting 3 folds for each of 100 candidates, totalling 300 fits
[Parallel(n_jobs=-1)]: Using backend LokyBackend with 4 concurrent workers.
exception calling callback for <Future at 0x1013bc90 state=finished raised BrokenProcessPool>
joblib.externals.loky.process_executor._RemoteTraceback: 
'''
Traceback (most recent call last):
  File "C:\Users\dalinar\PycharmProjects\visualizer\venv\lib\site-packages\joblib\externals\loky\process_executor.py", line 391, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "C:\Users\dalinar\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\queues.py", line 99, in get
    if not self._rlock.acquire(block, timeout):
PermissionError: [WinError 5] Access is denied
'''

在此之后还会引发其他异常,但上面的错误是其他异常的“直接原因”。

标签: pythonmachine-learningscikit-learnrandom-forest

解决方案


看起来您的并行处理有问题,因此n_jobs=1请在创建RandomizedSearchCV()对象时尝试设置。

此外,您可能想看看gridsearcCV 中的并行性最终会出现权限错误

希望这可以帮助!


推荐阅读