使用 CreateML 创建文本分类器模型时接收,"An error was thrown and was not caught: The validation data provided must contain ..."

Receiving, "An error was thrown and was not caught: The validation data provided must contain ..." when creating a Text Classifier Model with CreateML

我正在使用 Playground 使用 CreateML 创建文本分类器模型并不断收到错误消息:

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."

我的代码比较简单,使用数据table中的两列。 textColumn 被标记为 "text" 并且 labelColumn 被标记为 "class":

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

我能找到的与 Apple Developer Documentation 中提供的代码之间的唯一区别是

let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")

他们的文档是:

let evaluationMetrics = sentimentClassifier.evaluation(on: testingData)

如果我尝试使用 Apple Developer Documentation 中的行,Xcode 的 11.2.1 版会失败。

在此先感谢您提供的任何帮助。

试试这个!对我有用

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/justinmacbook/Desktop/twitter-sanders-apple3.csv"))

let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5 )

let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "class", labelColumn: "text")

let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "class", labelColumn: "text")

let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100

这是对我有用的解决方案。我相信这个原始代码只适用于 OSX 10.15+

import Cocoa
import CreateML
import NaturalLanguage

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/m0a04y6/Desktop/iOS/ML/twitter-sanders-apple3.csv"))

let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
    
let parameters = MLTextClassifier.ModelParameters.init(validationData: trainingData, algorithm: MLTextClassifier.ModelAlgorithmType.maxEnt(revision: 1), language: NLLanguage.english, textColumnValidationData: "text", labelColumnValidationData: "class")
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class", parameters: parameters)

let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")     //Training accuracy as a percentage

let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
print(evaluationAccuracy)

let metadata = MLModelMetadata(author: "Madhur Ahuja", shortDescription: "A model trained to classify movie review sentiment", version: "1.0")
try sentimentClassifier.write(to: URL(fileURLWithPath: "/Users/m0a04y6/Desktop/iOS/ML/sentiment.mlmodel"), metadata: metadata)