Contents

MLUntypedColumn

A column of untyped values in a data table.

Declaration

struct MLUntypedColumn

Overview

A column is a homogenous collection of data values, similar to an Array. Columns are the main components of an MLDataTable and are designed to efficiently scale with large data sets.

Typically you use MLDataColumn, the typed equivalent to MLUntypedColumn, for its type-specific functionality.

Untyped columns are especially useful when:

  • You’re initializing a data table with columns by using init(namedColumns:).

  • You’re using columns of a non-Boolean type to filter a data table with subscript(_:).

  • You don’t need to work directly with the underlying type.

Each element of an untyped column is an MLDataValue, and has an underlying type that conforms to MLDataValueConvertible. The underlying type is hidden from the Swift compiler and is what makes an MLUntypedColumn untyped. Using an untyped column allows you to quickly write type-agnostic code with Create ML.

let column = MLUntypedColumn([2, 3, 5, 7, 11])
let columnOver2 = column / 2 print(columnOver2)
/* Prints...
 ValueType: Double
 Values:        [1.0, 1.5, 2.5, 3.5, 5.5]
 */

However, by avoiding type safety at compile time, you expose your code to errors at runtime. When an error occurs during an operation, Create ML marks the product of that operation invalid by setting isValid to false and by setting error with a value. For example, using a slash (/) operator to divide a column of integers with a string produces an invalid column.

let column = MLUntypedColumn([2, 3, 5, 7, 11])
let invalidColumn = column / "foo"
print(invalidColumn.isValid) // Prints "false"

Once a column becomes invalid, you can’t use it for any subsequent operation because it will only produce further invalid columns or invalid tables.

Each comparison operator of MLUntypedColumn returns a column of Booleans. However, MLUntypedColumn uses integers as its underlying type for columns of Booleans, because MLDataValue does not have a case for Bool.

For example, create an untyped column of Booleans using the less-than comparison operator(<(_:_:)).

let column = MLUntypedColumn([2, 3, 5, 7, 11])
let lessThan5 = column < 5

Then print the column to see that its underlying ValueType is Int, and each Boolean value of true or false is represented in the column by an integer value of 1 or 0, respectively.

print(lessThan5)
/* Prints...
 ValueType: Int
 Values:        [1, 1, 0, 0, 0]
 */

Use these untyped columns of Booleans just as you would with a typed column of Booleans (``MLDataColumn```<<doc://com.apple.documentation/documentation/swift/bool>>`) to:

Topics

Creating an untyped column

Creating an untyped column by converting another column

Getting the number of elements

Getting an element

Appending to an untyped column

Duplicating a column

Sorting elements to generate a column

Converting a column to generate a data column

Exposing the underlying type to generate a data column

Transforming elements to generate a data column

Masking elements to generate an untyped column

Discarding elements to generate an untyped column

Selecting elements to generate an untyped column

Filling in missing elements to generate an untyped column

Evaluating elements to generate an untyped column

Combining columns

Combining columns to generate an untyped column

Combining a column with a value to generate an untyped column

Combining a value with a column to generate an untyped column

Comparing columns

Comparing columns to generate an untyped column of booleans

Comparing a column with a value to generate an untyped column of booleans

Comparing a value with a column to generate an untyped column of booleans

Combining columns of booleans to generate an untyped column of booleans

Visualizing a column

Getting a description of an untyped column

Handling untyped column errors

Default Implementations

See Also

Adding columns