DMatrix

public class DMatrix

Data class used with Booster.

Data is encapsulation of DMatrixHandle, internal structure used by XGBoost, which is optimized for both memory efficiency and training speed.

  • Name of dataset, for example, “train”.

    Declaration

    Swift

    public var name: String
  • Pointer to underlying DMatrixHandle.

    Declaration

    Swift

    public let dmatrix: DMatrixHandle?
  • Initialize Data with an existing DMatrixHandle pointer.

    Declaration

    Swift

    public init(
        name: String,
        dmatrix: DMatrixHandle?,
        features: [Feature]?,
        label: FloatData?,
        weight: FloatData?,
        baseMargin: FloatData?,
        labelLowerBound: FloatData?,
        labelUpperBound: FloatData?
    ) throws

    Parameters

    name

    Name of dataset.

    dmatrix

    DMatrixHandle pointer.

    features

    Array describing features in this dataset.

    label

    Sets label after DMatrix initialization.

    weight

    Sets weight after DMatrix initialization.

    baseMargin

    Sets baseMargin after DMatrix initialization.

    labelLowerBound

    Sets labelLowerBound after DMatrix initialization.

    labelUpperBound

    Sets labelUpperBound after DMatrix initialization.

  • Initialize Data.

    Declaration

    Swift

    public convenience init(
        name: String,
        from data: FloatData,
        shape: Shape,
        features: [Feature]? = nil,
        label: FloatData? = nil,
        weight: FloatData? = nil,
        baseMargin: FloatData? = nil,
        labelLowerBound: FloatData? = nil,
        labelUpperBound: FloatData? = nil,
        missingValue: Float = Float.greatestFiniteMagnitude,
        threads: Int = 0
    ) throws

    Parameters

    name

    Name of dataset.

    from

    Values source.

    shape

    Shape of resulting DMatrixHandle, needs to be of rank two, e.g. [row, column].

    features

    Names and types of features.

    label

    Sets label after DMatrix initialization.

    weight

    Sets weight after DMatrix initialization.

    baseMargin

    Sets baseMargin after DMatrix initialization.

    labelLowerBound

    Sets labelLowerBound after DMatrix initialization.

    labelUpperBound

    Sets labelUpperBound after DMatrix initialization.

    missingValue

    Value in the input data which needs to be present as a missing value.

    threads

    Number of threads to use for loading data when parallelization is applicable. If 0, uses maximum threads available on the system.

  • Initialize Data.

    Declaration

    Swift

    public convenience init(
        name: String,
        from: FloatData & ShapeData,
        features: [Feature]? = nil,
        label: FloatData? = nil,
        weight: FloatData? = nil,
        baseMargin: FloatData? = nil,
        labelLowerBound: FloatData? = nil,
        labelUpperBound: FloatData? = nil,
        missingValue _: Float = Float.greatestFiniteMagnitude,
        threads: Int = 0
    ) throws

    Parameters

    name

    Name of dataset.

    from

    Data with shape comfortance.

    features

    Names and types of features.

    label

    Sets label after DMatrix initialization.

    weight

    Sets weight after DMatrix initialization.

    baseMargin

    Sets baseMargin after DMatrix initialization.

    labelLowerBound

    Sets labelLowerBound after DMatrix initialization.

    labelUpperBound

    Sets labelUpperBound after DMatrix initialization.

    missingValue

    Value in the input data which needs to be present as a missing value.

    threads

    Number of threads to use for loading data when parallelization is applicable. If 0, uses maximum threads available on the system.

  • Initialize Data from file.

    Declaration

    Swift

    public convenience init(
        name: String,
        from file: String,
        format: DataFormat,
        useCache: Bool = false,
        features: [Feature]? = nil,
        labelColumn: Int? = nil,
        label: FloatData? = nil,
        weight: FloatData? = nil,
        baseMargin: FloatData? = nil,
        labelLowerBound: FloatData? = nil,
        labelUpperBound: FloatData? = nil,
        silent: Bool = true,
        fileQuery: [String] = []
    ) throws

    Parameters

    name

    Name of dataset.

    from

    File to laod from.

    format

    Format of input file.

    useCache

    Use external memory.

    features

    Names and types of features.

    labelColumn

    Which column is for label.

    label

    Sets label after DMatrix initialization.

    weight

    Sets weight after DMatrix initialization.

    baseMargin

    Sets baseMargin after DMatrix initialization.

    labelLowerBound

    Sets labelLowerBound after DMatrix initialization.

    labelUpperBound

    Sets labelUpperBound after DMatrix initialization.

    silent

    Whether print messages during construction.

    fileQuery

    Additional parameters that will be appended to the file path as query.

  • Save DMatrixHandle to binary file.

    Declaration

    Swift

    public func save(
        to path: String,
        silent: Bool = true
    ) throws

    Parameters

    to

    File path.

    silent

    Whether print messages during construction.

  • Save feature map compatible with XGBoost`s inputs.

    Declaration

    Swift

    public func saveFeatureMap(
        to path: String
    ) throws

    Parameters

    to

    Path where feature map will be saved.

  • Load previously saved feature map.

    Declaration

    Swift

    public func loadFeatureMap(
        from path: String
    ) throws

    Parameters

    from

    Path to the feature map.

  • Declaration

    Swift

    public func rowCount() throws -> Int

    Return Value

    The number of rows.

  • Declaration

    Swift

    public func columnCount() throws -> Int

    Return Value

    The number of columns.

  • Declaration

    Swift

    public func shape() throws -> Shape

    Return Value

    The shape of dmatrix, i.e. (rowCount(), columnCount()).

  • Slice and return a new Data that only contains indexes.

    Declaration

    Swift

    public func slice(
        indexes: Int32Data,
        newName: String? = nil,
        allowGroups: Bool = false
    ) throws -> DMatrix

    Parameters

    indexes

    Array of indices to be selected.

    newName

    New name of the returned Data.

    allowGroups

    Allow slicing of a matrix with a groups attribute.

    Return Value

    A new dmatrix class containing only selected indexes.

  • Slice and return a new Data that only indexes from given range..

    Declaration

    Swift

    public func slice(
        indexes: Range<Int32>,
        newName: String? = nil,
        allowGroups: Bool = false
    ) throws -> DMatrix

    Parameters

    indexes

    Range of indices to be selected.

    newName

    New name of the returned Data.

    allowGroups

    Allow slicing of a matrix with a groups attribute.

    Return Value

    A new DMatrix class containing only selected indexes.

  • Set uint type property into the DMatrixHandle.

    Declaration

    Swift

    public func setUIntInfo(
        field: String,
        values: UInt32Data
    ) throws

    Parameters

    field

    The field name of the information.

    values

    The array of data to be set.

  • Set float type property into the DMatrixHandle.

    Declaration

    Swift

    public func setFloatInfo(
        field: String,
        values: FloatData
    ) throws

    Parameters

    field

    The field name of the information.

    values

    The array of data to be set.

  • Get unsigned integer property from the DMatrixHandle.

    Declaration

    Swift

    public func getUIntInfo(
        field: String
    ) throws -> [UInt32]

    Parameters

    field

    The field name of the information.

    Return Value

    An array of unsigned integer information of the data.

  • Get float property from the DMatrixHandle.

    Declaration

    Swift

    public func getFloatInfo(
        field: String
    ) throws -> [Float]

    Parameters

    field

    The field name of the information.

    Return Value

    An array of float information of the data.

  • Set uint type property into the DMatrixHandle.

    Declaration

    Swift

    public func set(
        field: UIntField,
        values: UInt32Data
    ) throws

    Parameters

    field

    The field name of the information.

    values

    The array of data to be set.

  • Set float type property into the DMatrixHandle.

    Declaration

    Swift

    public func set(
        field: FloatField,
        values: FloatData
    ) throws

    Parameters

    field

    The field name of the information.

    values

    The array of data to be set.

  • Save names and types of features.

    Declaration

    Swift

    public func set(
        features: [Feature]?
    ) throws

    Parameters

    features

    Optional array of features.

  • Declaration

    Swift

    public func features(defaultPrefix: String = "f") throws -> [Feature]

    Return Value

    Features of data, generates universal names with quantitative type if not previously set by user.

  • Declaration

    Swift

    public func get(field: FloatField) throws -> [Float]

    Return Value

    An array of float information of the data.

  • Declaration

    Swift

    public func get(field: UIntField) throws -> [UInt32]

    Return Value

    An array of uint information of the data.