MLUntypedColumn
A column of untyped values in a data table.
Declaration
struct MLUntypedColumnOverview
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 < 5Then 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:
Filter another untyped column with subscript(_:)
Logically combine with another untyped column of Booleans with the &&(_:_:) and ||(_:_:) operators
Mask rows of an MLDataTable with its subscript(_:)
Topics
Creating an untyped column
init(repeating:count:)init(repeating:count:)init(repeating:count:)init(_:)init(_:)init(_:)init(_:)init(_:)init()