解决reg:linear is now 和 Series.base is deprecated and will be removed in a future version报错信息

学习赵卫东老师的XGBoost树时,运用老师的代码报了三个错误,经过查询,把代码进行修改,以帮助也学习这门课的朋友们

import pandas as pd
import xgboost as xgb

df = pd.DataFrame({'x':[1,2,3], 'y':[10,20,30]})
X_train = df.drop('y',axis=1)
Y_train = df['y']
xg_reg = xgb.XGBRegressor(objective='reg:squarederror')
T_train_xgb = xgb.DMatrix(X_train, Y_train)

params = {"objective": "reg:linear", "booster":"gblinear"}
gbm = xgb.train(dtrain=T_train_xgb,params=params)
Y_pred = gbm.predict(xgb.DMatrix(pd.DataFrame({'x':[4,5]})))
print(Y_pred)

这个代码报了三个错误,报错信息

  • [18:06:16] WARNING: d:\build\xgboost\xgboost-0.90.git\src\objective\regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
    [32.79174 38.75454] ----【第1个报错】
  • C:\Users\admim\Anaconda3\lib\site-packages\xgboost\core.py:587: FutureWarning: Series.base is deprecated and will be removed in a future version
    if getattr(data, ‘base’, None) is not None and \ ----【第2个报错】
  • C:\Users\admim\Anaconda3\lib\site-packages\xgboost\core.py:588: FutureWarning: Series.base is deprecated and will be removed in a future version
    data.base is not None and isinstance(data, np.ndarray) \ ----【第3个报错】
  1. 针对第1个报错信息,reg:linear is now deprecated in favor of reg:squarederror.说的是:现在不推荐使用reg:linear,而推荐使用reg:squarederror。
    对此,把代码params = {"objective": "reg:linear", "booster":"gblinear"}改为params = {"objective": "reg:squarederror", "booster":"gblinear"}
    (主要是把objective键的值reg:linear改为reg:squarederror)
  2. 针对第2和第3个报错信息,可以添加以下代码忽略掉报错信息
import warnings
warnings.filterwarnings("ignore")

对此,修改后的代码为

import pandas as pd
import xgboost as xgb
import warnings
warnings.filterwarnings("ignore")
df = pd.DataFrame({'x':[1,2,3], 'y':[10,20,30]})
X_train = df.drop('y',axis=1)
Y_train = df['y']
T_train_xgb = xgb.DMatrix(X_train, Y_train)

params = {"objective": "reg:squarederror", "booster":"gblinear"}
gbm = xgb.train(dtrain=T_train_xgb,params=params)
Y_pred = gbm.predict(xgb.DMatrix(pd.DataFrame({'x':[4,5]})))
print(Y_pred)

运行结果:

[32.79174 38.75454]

你可能感兴趣的:(python,机器学习)