Get Started with XGBoost
This is a quick start tutorial showing snippets for you to quickly try out XGBooston the demo dataset on a binary classification task.
Links to Other Helpful Resources
SeeInstallation Guide on how to install XGBoost.
SeeText Input Format on using text format for specifying training/testing data.
SeeTutorials for tips and tutorials.
SeeLearning to use XGBoost by Examples for more code examples.
Python
fromxgboostimportXGBClassifier# read datafromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_splitdata=load_iris()X_train,X_test,y_train,y_test=train_test_split(data['data'],data['target'],test_size=.2)# create model instancebst=XGBClassifier(n_estimators=2,max_depth=2,learning_rate=1,objective='binary:logistic')# fit modelbst.fit(X_train,y_train)# make predictionspreds=bst.predict(X_test)
R
# load datadata(agaricus.train,package='xgboost')data(agaricus.test,package='xgboost')train<-agaricus.traintest<-agaricus.test# fit modelbst<-xgboost(x=train$data,y=factor(train$label),max.depth=2,eta=1,nrounds=2,nthread=2,objective="binary:logistic")# predictpred<-predict(bst,test$data)
Julia
usingXGBoost# read datatrain_X,train_Y=readlibsvm("demo/data/agaricus.txt.train",(6513,126))test_X,test_Y=readlibsvm("demo/data/agaricus.txt.test",(1611,126))# fit modelnum_round=2bst=xgboost(train_X,num_round,label=train_Y,eta=1,max_depth=2)# predictpred=predict(bst,test_X)
Scala
importml.dmlc.xgboost4j.scala.DMatriximportml.dmlc.xgboost4j.scala.XGBoostobjectXGBoostScalaExample{defmain(args:Array[String]){// read trainining data, available at xgboost/demo/datavaltrainData=newDMatrix("/path/to/agaricus.txt.train")// define parametersvalparamMap=List("eta"->0.1,"max_depth"->2,"objective"->"binary:logistic").toMap// number of iterationsvalround=2// train the modelvalmodel=XGBoost.train(trainData,paramMap,round)// run predictionvalpredTrain=model.predict(trainData)// save model to the file.model.saveModel("/local/path/to/model")}}