本文介绍了XGBoost 找不到 sklearn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 XGBoost,但被我无法弄清楚的错误阻止.我在活动环境中安装了 sklearn,可以通过在同一笔记本中训练 sklearn RandomForestClassifier 来验证它.当我尝试训练 XGBoost 模型时,出现错误 XGBoostError: sklearn need to be installed to use this module

I’m experimenting with XGBoost and am blocked by an error I can’t figure out. I have sklearn installed in the active environment and can verify it by training a sklearn RandomForestClassifier in the same notebook. When I try to train a XGBoost model I get the error XGBoostError: sklearn needs to be installed in order to use this module

这有效:

clf = RandomForestClassifier(n_estimators=200, random_state=0, n_jobs=-1)

这会引发异常:

clf = xgb.XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05).fit(train_X, train_y)

更新:使用完全相同的代码和导入创建了一个 PyCharm 模块,并且它毫无例外地执行.所以这似乎是一个 Jupyter Notebook 问题.PyCharm 指向与 notebook 相同的 Anaconda 环境.

UPDATE: Created a PyCharm module with exactly the same code and imports and it executed without an exception. So this appears to be a Jupyter Notebook issue. PyCharm is pointed to the same Anaconda environment as the notebook.

更新 2:创建一个 笔记本,并从引发异常的笔记本中复制代码.代码在新笔记本中运行正常.叹.案件已结.

UPDATE 2: Created a new notebook and copied the code from the one that was throwing the exception. The code runs OK in the new notebook. Sigh. Case closed.

推荐答案

遇到同样的问题,我安装了 sklearn after 安装 xgboost 同时我的 jupyter 笔记本正在运行.通过重新启动我的 Jupyter 笔记本服务器,xgboost 能够找到 sklearn 安装.

Ran into the same issue, I had installed sklearn after installing xgboost while my jupyter notebook was running. By restarting my Jupyter notebook server, xgboost was able to find the sklearn installation.

在我安装了 sklearn before 安装 xgboost then 启动我的 jupyter 的另一个新环境中对此进行了测试笔记本没有问题.

Tested this in another fresh environment where I've installed sklearn before installing xgboost then starting my jupyter notebook without the issue.

这篇关于XGBoost 找不到 sklearn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 00:10