Initializer

init(dictionary:)

Creates a data table from a dictionary of column names and data values.

Declaration

init(dictionary: [String : MLDataValueConvertible]) throws

Parameters

dictionary

The dictionary of each column name and its associated data values.

Discussion

Use this initializer to create a data table from an in-memory Dictionary.

A table of information about a book. Columns named "Title", "Author", "Pages", and "Genre". The first row is "Alice in Wonderland", "Lewis Carroll", "124", and "Fantasy".

For example, to create a data table as shown above, first create a dictionary.

let data: [String: MLDataValueConvertible] = [
    "Title": ["Alice in Wonderland", "Hamlet", "Treasure Island", "Peter Pan"],
    "Author": ["Lewis Carroll", "William Shakespeare", "Robert L. Stevenson", "J. M. Barrie"],
    "Pages": [124, 98, 280, 94],
    "Genre": ["Fantasy", "Drama", "Adventure", "Fantasy"]
]

Then, use init(dictionary:) to create a data table from the dictionary.

let bookTable = try MLDataTable(dictionary: data)

The keys of the dictionary become the column names, and the value of each key becomes the element(s) of the corresponding column in the data table.

See Also

Creating a Data Table

Creating a Model from Tabular Data

Train a machine learning model by using Core ML to import and manage tabular data.

init(contentsOf: URL, options: MLDataTable.ParsingOptions)

Creates a data table from an imported JSON or CSV file.

init(namedColumns: [String : MLUntypedColumn])

Creates a data table from a dictionary of column names and untyped columns.

init()

Creates an empty table containing no rows or columns.