I am a newbie to XGBoost so pardon my ignorance. Here is the python code :
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']
T_train_xgb = xgb.DMatrix(X_train, Y_train)
params = {"objective": "reg:linear"}
gbm = xgb.train(dtrain=T_train_xgb,params=params)
Y_pred = gbm.predict(xgb.DMatrix(pd.DataFrame({'x':[4,5]})))
print Y_pred
Output is :
[ 24.126194 24.126194]
As you can see the input data is simply a straight line. So the output I expect is
[40,50]
. What am I doing wrong here?
Answer :
It seems that XGBoost uses regression trees as base learners by default. XGBoost (or Gradient boosting in general) work by combining multiple of these base learners. Regression trees can not extrapolate the patterns in the training data, so any input above 3 or below 1 will not be predicted correctly in your case. Your model is trained to predict outputs for inputs in the interval
[1,3] , an input higher than 3 will have the same output as 3, and an input less than 1 will be given the same output as 1.
Additionally, regression trees do not really see your data as a straight line as they are nonparametric models, which means they can theoretically fit any shape that is more complicated than a straight line. Roughly, a regression tree works by assigning your new input data to some of the training data points it have seen during training, and produce the output based on that.
This is in contrast to parametric regressors (like linear regression) which actually look for the best parameters of a hyperplane (straight line in your case) to fit your data. Linear regression does see your data as a straight line with a slope and and intercept.
You can change the base learner of your XGB model to a GLM (generalised linear model) by adding a
booster parameter in your model params :
In general, to debug why your XGBoost model is behaving in a particular way, see the model parameters :
If your base learner is linear model, the get_dump output is :
In your code above, since you tree base learners, the output will be :
Tip : I actually prefer to use xgb.XGBRegressor or xgb.XGBClassifier classes, since they follow thescikit learn API. And because scikit learn has so many machine learning algorithm implementations, using XGB as an additional library does not disturb my workflow only when I use the scikit interface of XGB.

1 comment:
If you need your exgirlfriend or exboyfriend to come crawling back to you on their knees (no matter why you broke up) you need to watch this video
right away...
(VIDEO) Get your ex back with TEXT messages?
Post a Comment