a_zxcvdfgfv 2024-08-20 21:02 采纳率: 0%
浏览 6

随机森林(用的jupyter lab)调参问题

在随机森林调参中(用的jupyter lab),出现了错误,以下是代码

scorel = []
for i in range(0,200,20):
    rfc = RandomForestClassifier(n_estimators=i+1,
                                 n_jobs=-1,
                                 random_state=90)
    score = cross_val_score(rfc,data.data,data.target,cv=10).mean()
    scorel.append(score)
print(max(scorel),(scorel.index(max(scorel))*10)+1)
plt.figure(figsize=[20,5])
plt.plot(range(1,201,10),scorel)
plt.show()

提示错误:
D:\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py:372: FitFailedWarning: 
10 fits failed out of a total of 10.
The score on these train-test partitions for these parameters will be set to nan.
If these failures are not expected, you can try to debug them by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
10 fits failed with the following error:
Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 680, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "D:\Anaconda3\lib\site-packages\sklearn\ensemble\_forest.py", line 450, in fit
    trees = Parallel(
  File "D:\Anaconda3\lib\site-packages\joblib\parallel.py", line 968, in __call__
    n_jobs = self._initialize_backend()
  File "D:\Anaconda3\lib\site-packages\joblib\parallel.py", line 735, in _initialize_backend
    n_jobs = self._backend.configure(n_jobs=self.n_jobs, parallel=self,
  File "D:\Anaconda3\lib\site-packages\joblib\_parallel_backends.py", line 494, in configure
    self._workers = get_memmapping_executor(
  File "D:\Anaconda3\lib\site-packages\joblib\executor.py", line 20, in get_memmapping_executor
    return MemmappingExecutor.get_memmapping_executor(n_jobs, **kwargs)
  File "D:\Anaconda3\lib\site-packages\joblib\executor.py", line 42, in get_memmapping_executor
    manager = TemporaryResourcesManager(temp_folder)
  File "D:\Anaconda3\lib\site-packages\joblib\_memmapping_reducer.py", line 531, in __init__
    self.set_current_context(context_id)
  File "D:\Anaconda3\lib\site-packages\joblib\_memmapping_reducer.py", line 535, in set_current_context
    self.register_new_context(context_id)
  File "D:\Anaconda3\lib\site-packages\joblib\_memmapping_reducer.py", line 560, in register_new_context
    self.register_folder_finalizer(new_folder_path, context_id)
  File "D:\Anaconda3\lib\site-packages\joblib\_memmapping_reducer.py", line 590, in register_folder_finalizer
    resource_tracker.register(pool_subfolder, "folder")
  File "D:\Anaconda3\lib\site-packages\joblib\externals\loky\backend\resource_tracker.py", line 191, in register
    self._send('REGISTER', name, rtype)
  File "D:\Anaconda3\lib\site-packages\joblib\externals\loky\backend\resource_tracker.py", line 204, in _send
    msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 18-20: ordinal not in range(128)

  warnings.warn(some_fits_failed_message, FitFailedWarning)
D:\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py:372: FitFailedWarning: 
10 fits failed out of a total of 10.
The score on these train-test partitions for these parameters will be set to nan.
If these failures are not expected, you can try to debug them by setting error_score='raise'.

Below are more details about the failures:




这个是哪里的问题,该怎么解决






  • 写回答

1条回答 默认 最新

  • 吃不了席 2024-08-23 18:54
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    这个问题是由于在运行随机森林调参时,某些参数组合导致了拟合失败。这可能是由于数据集的某些特性导致的。为了解决这个问题,你可以尝试以下方法:

    1. 增加n_jobs的值。这将允许更多的并行任务执行,可能会加快拟合过程并减少失败的可能性。例如,将n_jobs=-1改为n_jobs=4(根据你的计算机的核心数量进行调整)。

    2. 使用error_score='raise'来查看具体的错误信息。这将在拟合失败时引发异常,从而帮助你了解失败的原因。你可以在cross_val_score函数中添加这个参数,如下所示:

    from sklearn.exceptions import FitFailedWarning
    import warnings
    
    warnings.filterwarnings("ignore", category=FitFailedWarning)
    
    scorel = []
    for i in range(0, 200, 20):
        rfc = RandomForestClassifier(n_estimators=i + 1,
                                     n_jobs=-1,
                                     random_state=90)
        try:
            score = cross_val_score(rfc, data.data, data.target, cv=10, error_score='raise').mean()
            scorel.append(score)
        except Exception as e:
            print(f"Error at n_estimators={i + 1}: {e}")
            scorel.append(np.nan)
    
    print(max(scorel), (scorel.index(max(scorel)) * 10) + 1)
    plt.figure(figsize=[20, 5])
    plt.plot(range(1, 201, 10), scorel)
    plt.show()
    
    1. 如果上述方法仍然无法解决问题,你可以尝试调整其他参数,如max_depthmin_samples_split等,以找到更适合你的数据集的参数组合。同时,确保你的数据集已经进行了适当的预处理,例如缺失值填充、特征缩放等。
    评论

报告相同问题?

问题事件

  • 创建了问题 8月20日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?