import glob
for imagePath in glob.glob('faces/*.jpg'):
if imagePath.endsWith('.jpg'):
continue
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
[ 24.126194 24.126194]
[40,50]
. What am I doing wrong here?
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.

Here we want to set all age > 2 values to NaN. import pandas as pd import numpy as np
df = pd.DataFrame({'age':[1,2,3,4,5], 'income':[10,20,30,40,50]})#guaranteed to work without warnings/errors
df.loc[df.age > 2, 'age'] = np.NaNprint df#won't work
df[df["age"] > 2]["age"] = np.NaN #error#will work, but may give warning
df["age"][df["age"] > 2] = np.NaN
wget https://3230d63b5fc54e62148ec95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda2.3.0Linuxx86_64.sh
bash Anaconda2.3.0Linuxx86_64.sh