🚀 原文链接:https://xgboost.readthedocs.io/en/latest/get_started.html

This is a quick start tutorial showing snippets(一小条消息) for you to quickly try out XGBoost on the demo dataset on a binary classification task.

Links to Other Helpful Resources

  • See Installation Guide on how to install XGBoost.
  • See Text Input Format on using text format for specifying training/testing data.
  • See Tutorials for tips and tutorials.
  • See Learning to use XGBoost by Examples for more code examples.

    Python

    1. import xgboost as xgb
    2. # read in data
    3. dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
    4. dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
    5. # specify parameters via map
    6. param = {'max_depth':2, 'eta':1, 'objective':'binary:logistic'}
    7. num_round = 2
    8. bst = xgb.train(param, dtrain, num_round)
    9. # make prediction
    10. preds = bst.predict(dtest)

    R

    1. # load data
    2. data(agaricus.train, package='xgboost')
    3. data(agaricus.test, package='xgboost')
    4. train <- agaricus.train
    5. test <- agaricus.test
    6. # fit model
    7. bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nrounds = 2,
    8. nthread = 2, objective = "binary:logistic")
    9. # predict
    10. pred <- predict(bst, test$data)

    Julia

    1. using XGBoost
    2. # read data
    3. train_X, train_Y = readlibsvm("demo/data/agaricus.txt.train", (6513, 126))
    4. test_X, test_Y = readlibsvm("demo/data/agaricus.txt.test", (1611, 126))
    5. # fit model
    6. num_round = 2
    7. bst = xgboost(train_X, num_round, label=train_Y, eta=1, max_depth=2)
    8. # predict
    9. pred = predict(bst, test_X)

    Scala

    ```scala import ml.dmlc.xgboost4j.scala.DMatrix import ml.dmlc.xgboost4j.scala.XGBoost

object XGBoostScalaExample { def main(args: Array[String]) { // read trainining data, available at xgboost/demo/data val trainData = new DMatrix(“/path/to/agaricus.txt.train”) // define parameters val paramMap = List( “eta” -> 0.1, “max_depth” -> 2, “objective” -> “binary:logistic”).toMap // number of iterations val round = 2 // train the model val model = XGBoost.train(trainData, paramMap, round) // run prediction val predTrain = model.predict(trainData) // save model to the file. model.saveModel(“/local/path/to/model”) } } ```