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
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 provideevalMetricfor multipleeval_metric.cache[DMatrix]
modelFileString? - 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") throwsParameters
toFilefile name of the output model dump
featMapNamefile name contains the feature map names
withStatswhether output split statistics
dumpFormatthe 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
valueas 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 eval_metric. Deprecated use setParam() with parameter name
eval_metricinstead.Declaration
Swift
public func setEvalMetric(_ metrics: [String]) -
Evaluate a set of data
booster.eval(set: [(train, "train"), (test, "test")], currentIter: 1)Declaration
Parameters
setlist of tuples (DMatrix, name of the eval data)
currentItercurrent 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
dataDMatrix to be evaluate on
setname of the eval data
currentItercurrent 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
dataDMatrix - The data to predict on
outputMarginbool - Whether to output the untransformed margin value
nTreeLimitInt - Limit the number of trees, set to 0 to use all the trees (default value)
predLeafOutput leaf index of trees instead of leaf value, note leaf index is unique per tree
predContribsOutput feature contributions to individual predictions
trainingwhether 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
featMapNamefile 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
View on GitHub
Booster Class Reference