Structure

MLDataTable

A table of data for training or evaluating a machine learning model.

Declaration

struct MLDataTable

Overview

MLDataTable is Create ML’s version of a spreadsheet in which each row represents an entity (such as a book, in the example below) with observable features. Each column (MLDataColumn or MLUntypedColumn) in the table represents an observable feature of that entity, such as a book’s title or author.

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".

In most cases you interact with columns using the typed MLDataColumn, especially when you need to directly access the contents of a column. You can also interact with columns using MLUntypedColumn, if the underlying type of the column isn’t important.

After you create a data table, you can modify it with methods like append(contentsOf:), addColumn(_:named:), and removeColumn(named:). You can also filter or map the contents of the data table to derive new data tables or new columns by using various subscripts and methods like dropDuplicates() or map(_:).

Finally, when your data table is ready, use it to train and evaluate a model from these groups:

Topics

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(dictionary: [String : MLDataValueConvertible])

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

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.

Getting the Size of a Data Table

var size: (rows: Int, columns: Int)

The number of rows and columns in the data table.

Transforming Rows to Generate a Data Column

func map<T>((MLDataTable.Row) -> T) -> MLDataColumn<T>

Creates a new column by applying a given thread-safe transform to every row in the data table.

func map<T>((MLDataTable.Row) -> T?) -> MLDataColumn<T>

Creates a new column, potentially with missing values, by applying a given thread-safe transform to every row in the data table.

Adding Columns

struct MLDataColumn

A column of typed values in a data table.

func addColumn(MLUntypedColumn, named: String)

Adds an untyped column to the table.

struct MLUntypedColumn

A column of untyped values in a data table.

Accessing Columns

subscript<T>(String, T.Type) -> MLDataColumn<T>?

Retrieves a column with the specified name and type.

subscript<Element>(String) -> MLDataColumn<Element>

Retrieves or adds a typed column with the specified name.

subscript(String) -> MLUntypedColumn

Retrieves or adds an untyped column with the specified name.

Renaming Columns

func renameColumn(named: String, to: String)

Changes the name of an existing column.

Removing Columns

func removeColumn(named: String)

Removes the column with the specified name.

Appending to a Data Table

func append(contentsOf: MLDataTable)

Appends the contents of the given data table to the end of this data table.

Generating New Data Tables

Data Table Derivation Operations

Create new data tables by manipulating an existing data table.

Getting Information About a Data Table’s Rows

var rows: MLDataTable.Rows

The rows of data in the table.

struct MLDataTable.Rows

A collection of rows in a data table.

Getting Information About a Data Table’s Columns

var columnNames: MLDataTable.ColumnNames

The names of the columns in the data table.

struct MLDataTable.ColumnNames

A collection of the names of the columns in a data table.

var columnTypes: [String : MLDataValue.ValueType]

The type of the data in each column.

Saving a Data Table

func write(to: URL)

Exports a binary file of the data table to the given directory URL.

func write(toDirectory: String)

Exports a binary file of the data table to the given directory path.

func writeCSV(to: URL)

Exports a CSV file of the data table to the given directory URL.

func writeCSV(toFile: String)

Exports a CSV file of the data table to the given directory path.

Visualizing a Data Table

func show() -> MLStreamingVisualizable

Provides a visualization for the data in the table.

protocol MLStreamingVisualizable

Provides a series of visualizations of a machine learning type.

protocol MLVisualizable

Provides a visualization of a machine learning type.

Describing a Data Table

var description: String

A text representation of the data table.

var playgroundDescription: Any

A description of the data table shown in a playground.

Handling Data Table Errors

var isValid: Bool

A Boolean value that indicates whether the data table is valid.

var error: Error?

The underlying error present when the data table is invalid.

See Also

Tabular Data

Creating a Model from Tabular Data

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

enum MLClassifier

A model you train to classify data into discrete categories.

enum MLRegressor

A model you train to estimate continuous values.

struct MLRecommender

A model you train to make recommendations based on item similarity, grouping, and, optionally, item ratings.

enum MLDataValue

The value of a cell in a data table.