問題描述:
衆所周知,要使用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)