我正在尝试使用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
'''


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

最佳答案

似乎您对并行处理有问题,因此在创建n_jobs=1对象时尝试设置RandomizedSearchCV()

此外,您可能想看看Parallalism in gridsearcCV is ending up with a permission error

希望这可以帮助!

10-05 23:17