Contents

MLDataTable.Aggregator.Operations.argmax(outputColumn:)

An operation that retrieves the value in the given column that’s in the same row as the maximum value of the aggregator’s column.

Declaration

case argmax(outputColumn: String)

Discussion

Use this operation to find a value in the given outputColumn that’s in the same row as the maximum value in the aggregator’s column. For example, take the following data table of drink ratings.

let teaVsCoffee = try! MLDataTable(dictionary: [
    "userName" : ["Sara", "Sara", "James", "James"],
    "drink"    : ["tea", "coffee", "tea", "coffee"],
    "rating"   : [5.0, 3.5, 3.1, 4.9]
])

print(teaVsCoffee)

Prints ...
 Columns:
    ratingfloat
    drinkstring
    userNamestring
Rows: 4 Data:
+----------------+----------------+----------------+
| drink          | userName       | rating         |
+----------------+----------------+----------------+
| tea            | Sara           | 5              |
| coffee         | Sara           | 3.5            |
| tea            | James          | 3.1            |
| coffee         | James          | 4.9            |
+----------------+----------------+----------------+
[4 rows x 3 columns]

To find out which person gave the highest rating for any particular beverage, use the argmax operation with the "userName" string. Then use the group(columnsNamed:aggregators:) function group the "drink" column.

var highestRater = MLDataTable.Aggregator(operations: .argmax(outputColumn: "userName"),
                                          of: "rating")

let highestDrinkRatings = teaVsCoffee.group(columnsNamed: "drink",
                                            aggregators: [highestRater])
print(highestDrinkRatings)

Prints ...
+----------------+----------------+
| drink          | ratingArgmax   |
+----------------+----------------+
| tea            | Sara           |
| coffee         | James          |
+----------------+----------------+
[2 rows x 2 columns]

In this example, the drinks column only has two distinct drinks, tea and coffee, which result in a data table with two rows.

  • outputColumn: The name of the column that holds the values associated with the maximum values of the aggregator’s designated column.

See Also

Aggregation operations