File: get_started.rst

package info (click to toggle)
xgboost 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,796 kB
  • sloc: cpp: 67,502; python: 35,503; java: 4,676; ansic: 1,426; sh: 1,320; xml: 1,197; makefile: 204; javascript: 19
file content (97 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
########################
Get Started with XGBoost
########################

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 :doc:`Installation Guide </install>` on how to install XGBoost.
- See :doc:`Text Input Format </tutorials/input_format>` on using text format for specifying training/testing data.
- See :doc:`Tutorials </tutorials/index>` for tips and tutorials.
- See `Learning to use XGBoost by Examples <https://github.com/dmlc/xgboost/tree/master/demo>`_ for more code examples.

******
Python
******

.. code-block:: python

  from xgboost import XGBClassifier
  # read data
  from sklearn.datasets import load_iris
  from sklearn.model_selection import train_test_split
  data = load_iris()
  X_train, X_test, y_train, y_test = train_test_split(data['data'], data['target'], test_size=.2)
  # create model instance
  bst = XGBClassifier(n_estimators=2, max_depth=2, learning_rate=1, objective='binary:logistic')
  # fit model
  bst.fit(X_train, y_train)
  # make predictions
  preds = bst.predict(X_test)

***
R
***

.. code-block:: R

  # load data
  data(agaricus.train, package='xgboost')
  data(agaricus.test, package='xgboost')
  train <- agaricus.train
  test <- agaricus.test
  # fit model
  bst <- xgboost(x = train$data, y = factor(train$label),
                 max.depth = 2, eta = 1, nrounds = 2,
                 nthread = 2, objective = "binary:logistic")
  # predict
  pred <- predict(bst, test$data)

*****
Julia
*****

.. code-block:: julia

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

*****
Scala
*****

.. code-block:: 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")
    }
  }