Structure

MLDataTable

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

Overview

A data table stores data you use to train and evaluate an MLRegressor or MLClassifier model.

You can create a data table by adding tabular data directly from MLDataColumn and MLDataValue instances you create, or you can create it from a JSON or CSV file. Use data processing methods like map(_:) or subscript(_:) to change existing values or derive new columns.

After you load and process your data, you use the table to train a machine learning model. For example, the following figure illustrates how you might structure a data table used to train a classification model that predicts the genre of a book.

A table of information about a book. Columns named "title", "author", "number of pages", and "genre". The first row is "Alice in Wonderland", "Lewis Carroll", "124", and "Fantasy".

Topics

Creating a Table

Creating Data Tables for Training and Evaluation

Import and format data to create and evaluate a machine learning model.

init()

Creates an empty table containing no rows or columns.

init(contentsOf: URL)

Creates a data table from a 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 column data.

Deriving Tables and Columns from Existing Tables

subscript(MLDataColumn<Bool>) -> MLDataTable

Uses a typed row mask to filter the rows in an existing table.

subscript(MLUntypedColumn) -> MLDataTable

Uses an untyped row mask to filter the rows in an existing table.

func randomSplit(by: Double, seed: Int) -> (MLDataTable, MLDataTable)

Divides the table into two tables containing random subsets of the original table.

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

Derives a new column from existing table data.

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

Calls the given transform on each row in the table.

Adding and Removing Data

func addColumn(MLUntypedColumn, named: String)

Adds a new untyped column to the table.

func removeColumn(named: String)

Removes the column with the specified name.

func renameColumn(named: String, to: String)

Changes the name of an existing column.

func append(contentsOf: MLDataTable)

Adds the contents of another table to the table.

Accessing Typed Columns

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

Returns the column with the specified name.

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

Accesses the column with the specified name and inferred type for reading and writing.

struct MLDataColumn

A column of typed values in a data table.

Accessing Untyped Columns

subscript(String) -> MLUntypedColumn

Accesses the column with the specified name for reading and writing.

struct MLUntypedColumn

A column of untyped values in a data table.

Accessing Rows

var rows: MLDataTable.Rows

The rows of data in the table.

struct MLDataTable.Rows

A collection of rows in a data table.

struct MLDataTable.Row

A row of untyped values in a data table.

Describing a Data Table

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

The number of rows and columns in the data table.

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.

var description: String

A text representation of the data table.

var playgroundDescription: Any

A description of the data table shown in a playground.

Handling Errors

var isValid: Bool

A Boolean value indicating whether the data table is valid.

var error: Error?

The underlying error present when the data table is invalid.

See Also

Structured Data

Creating Data Tables for Training and Evaluation

Import and format data to create and evaluate a machine learning model.

enum MLDataValue

The value of a cell in a data table.

Beta

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software