Error creating Text Classifier Model: validation data missing

I’m getting the following error when attempting to create a Text Classifier Model using CreateML in Playground:

Playground execution terminated: An error was thrown and was not caught:
▿ The validation data provided must contain class.
  ▿ type : 1 element
    - reason : "The validation data provided must contain class."

My code is as follows:

import Cocoa
import CreateML

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ ... .csv"))
let(trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class")
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100

I’m using the code as outlined in the Apple Developer Documentation, however Xcode 11.2.1 requires me to include the textColumn and labelColumn parameters when calling the evaluation method.

How can I resolve this error?

The error message states that the validation data provided must contain class. This means that the testing data used in the evaluation method must have a column named “class” which contains the class labels for each text sample.

To resolve this error, make sure that the CSV file used to create the MLDataTable contains a column named “class” and that this column is also present in the testing data used in the evaluation method.

If the CSV file already contains a column named “class” but it is not being recognized, make sure that the column name is spelled correctly and matches the name used in the code.

Here’s an updated version of the code that includes the textColumn and labelColumn parameters in the evaluation method:

import Cocoa
import CreateML

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ ... .csv"))
let(trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class")
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100