Booster

public class Booster

A Booster of XGBoost, the model of XGBoost.

  • If the Booster is initialized, by simply checking if the BoosterHandle is non-nil.

    Declaration

    Swift

    public var initialized: Bool { get }
  • Constructor of Booster

    Declaration

    Swift

    public init(params: [Param] = [], cache: [DMatrix],
                modelFile: String? = nil) throws

    Parameters

    params

    [(String, String)] - Booster parameters. It was changed from Dictionary to array of set to enable multiple eval_metric, now it does not need to provide evalMetric for multiple eval_metric.

    cache

    [DMatrix]

    modelFile

    String? - If the modelFile param is provided, it will load the model from that file.

    Return Value

    Booster

  • Use .json as filename suffix to save model to json. Refer to XGBoost doc

    Declaration

    Swift

    public func saveModel(toFile fname: String) throws
  • Dump model to a text or json file

    Declaration

    Swift

    public func dumpModel(toFile fname: String,
                          featMapName fmap: String = "",
                          withStats: Bool = false,
                          dumpFormat: String = "text") throws

    Parameters

    toFile

    file name of the output model dump

    featMapName

    file name contains the feature map names

    withStats

    whether output split statistics

    dumpFormat

    the format to dump, “text”, “json” or “dot”

  • Load previously saved model from file

    Declaration

    Swift

    public func loadModel(fromFile fname: String) throws
  • Get an attribute by key

    Declaration

    Swift

    public func attr(key: String) -> String?
  • Get all the attributes

    Declaration

    Swift

    public func attributes() -> [String : String]
  • Set an attribute, pass value as nil to delete an attribute.

    Declaration

    Swift

    public func setAttr(key: String, value: String?)
  • Set Parameters by name and value

    Note: XGBoost C API accepts wrong param key-value pairs when setting, but will throw error during training or evaluating. Check document to make sure they are right.

    Declaration

    Swift

    public func setParam(name k: String, value v: String)
  • Set Parameters by Array of Set in (String, String)

    Note: XGBoost C API accepts wrong param key-value pairs when setting, but will throw error during training or evaluating. Check document to make sure they are right.

    Declaration

    Swift

    public func setParam(_ params: [Param])
  • Set eval_metric. Deprecated use setParam() with parameter name eval_metric instead.

    Declaration

    Swift

    public func setEvalMetric(_ metrics: [String])
  • Update for 1 iteration, should not be called directly

    Declaration

    Swift

    public func update(data: DMatrix, currentIter: Int, fnObj: FuncObj? = nil)
  • Evaluate a set of data

    booster.eval(set: [(train, "train"),
                       (test, "test")], currentIter: 1)
    

    Declaration

    Swift

    public func evalSet(evals: [(DMatrix, String)],
                        currentIter: Int,
                        fnEval: FuncEval? = nil) -> String?

    Parameters

    set

    list of tuples (DMatrix, name of the eval data)

    currentIter

    current iteration

    Return Value

    Evaluation result if successful, a string in a format like “[1]\ttrain-auc:0.938960\ttest-auc:0.948914”,

  • Evaluate data

    Declaration

    Swift

    public func eval(data: DMatrix, name: String, currentIter: Int = 0) -> String?

    Parameters

    data

    DMatrix to be evaluate on

    set

    name of the eval data

    currentIter

    current iteration

    Return Value

    Evaluation result if successful, a string in a format like “[1]\ttrain-auc:0.938960\ttest-auc:0.948914”

  • Predict labels on the data.

    Declaration

    Swift

    public func predict(data: DMatrix,
                        outputMargin: Bool = false,
                        nTreeLimit: Int = 0,
                        predLeaf: Bool = false,
                        predContribs: Bool = false,
                        training: Bool = false) -> [Float]

    Parameters

    data

    DMatrix - The data to predict on

    outputMargin

    bool - Whether to output the untransformed margin value

    nTreeLimit

    Int - Limit the number of trees, set to 0 to use all the trees (default value)

    predLeaf

    Output leaf index of trees instead of leaf value, note leaf index is unique per tree

    predContribs

    Output feature contributions to individual predictions

    training

    whether the predicted value is to be used for training, e.g., for update() 

    Return Value

    [Float]

  • Save model config to a file.

    Declaration

    Swift

    public func saveConfig(toFile fname: String) throws
  • Load model config from a file

    Declaration

    Swift

    public func loadConfig(fromFile fname: String) throws
  • Get feature importance of each feature.

    • importanceType:

      • weight: the number of times a feature is used to split the data
      • gain: the average gain of the feature is used to split across all features
      • total_gain: the total gain of the feature is used to split across all features
      • cover: the average coverage of the feature is used to split across all features
      • total_cover: the total coverage of the feature is used to split across all features

    Declaration

    Swift

    public func getScore(featMapName fmap: String = "",
                         importanceType: String = "weight") throws -> [String: Float]

    Parameters

    featMapName

    file name contains the feature map names

    importanceType

    • weight: the number of times a feature is used to split the data
    • gain: the average gain of the feature is used to split across all features
    • total_gain: the total gain of the feature is used to split across all features
    • cover: the average coverage of the feature is used to split across all features
    • total_cover: the total coverage of the feature is used to split across all features

    Return Value

    a dictionary of feature: score