【解决python,xgboost问题】XGBoostError: sklearn needs to be installed in order to use this module

问题描述:
众所周知,要使用python的库xgboost必须要提前安装好sklearn。
但是,当我们xgboost和sklearn都安装了,本人在执行以下代码时:

model_regr = xgboost.XGBRegressor(booster='gbtree',
                silent=1, 
                nthread=-1,
                eta=0.01,
                min_child_weight=1,
                max_depth=10, 
                gamma=0,
                subsample=1,
                colsample_bytree=1,
                colsample_bylevel=1,
                alpha =1,
                scale_pos_weight=1,
                objective='reg:linear',
                eval_metric='mae',
                missing=None,
                seed=0)
model_regr.fit(x_train,y_train)

却还出现了以下问题:
XGBoostError: sklearn needs to be installed in order to use this module

D:\Anaconda3\lib\site-packages\xgboost\sklearn.py in __init__(self, max_depth, learning_rate, n_estimators, verbosity, silent, objective, booster, n_jobs, nthread, gamma, min_child_weight, max_delta_step, subsample, colsample_bytree, colsample_bylevel, colsample_bynode, reg_alpha, reg_lambda, scale_pos_weight, base_score, random_state, seed, missing, importance_type, **kwargs)
    144                  importance_type="gain", **kwargs):
    145         if not SKLEARN_INSTALLED:
--> 146             raise XGBoostError('sklearn needs to be installed in order to use this module')
    147         self.max_depth = max_depth
    148         self.learning_rate = learning_rate

XGBoostError: sklearn needs to be installed in order to use this module

在这里插入图片描述
该如何解决呢?

解决步骤:
第一步:
确保安装了sklearn,若安装了,请看下一步。
若没安装,则安装sklearn,具体安装方法网上有很多。

第二步:
检查是否在使用函数前,导入需要用的库

import numpy
import sklearn
import xgboost

第三步:!!!
检查numpy的版本

import numpy
numpy.__version__

如果你的版本是1.18.0,那么你就来对地方了!!目前新版本1.18.0就是导致这个问题的罪归祸首!
因此,降低numpy的版本就能解决该问题!!!

具体操作:
1.关闭各种python程序
2.打开命令提示符(cmd)
3.进入到python安装路径,本人为D:\Anaconda3
4.降低numpy版本:pip install --upgrade numpy==1.14.5

本人将1.18.0版本的numpy降低到1.14.5版本,完美解决该问题!(你也可以尝试其他版本,不一定是1.14.5)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章