2

I am running the following code in Xcode 14.3 Playgrounds. I am using macOS Ventura 13.1.

let csvFile = Bundle.main.url(forResource: "all-data", withExtension: "csv")!
let dataTable = try MLDataTable(contentsOf: csvFile)


let (classifierEvaluationTable, classifierTrainingTable) = dataTable.randomSplit(by: 0.20, seed: 5)

let classifier = try MLTextClassifier(trainingData: classifierTrainingTable, textColumn: "text", labelColumn: "sentiment")

I get the following warning:

'init(trainingData:textColumn:labelColumn:parameters:)' was deprecated in macOS 13.0: Use DataSource instead of MLDataTable when initializing.

The problem is that there is no documentation on how to create DataFrame or DataSource.

john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

2

Handling some time on that case. We can use DataFrame, so the warnings will avoid. At this time it's not deprecated.

There is an example, what I found how to rewrite this.

Previous version:

let csvFile = Bundle.main.url(forResource: "all-data", withExtension: "csv")!
let dataTable = try MLDataTable(contentsOf: csvFile)


let (classifierEvaluationTable, classifierTrainingTable) = dataTable.randomSplit(by: 0.20, seed: 5)

let classifier = try MLTextClassifier(trainingData: classifierTrainingTable, textColumn: "text", labelColumn: "sentiment")

Updated:

additionally add this

import TabularData

let csvFile = Bundle.main.url(forResource: "all-data", withExtension: "csv")!
let dataFrame = DataFrame(contentsOfCSVFile: csvFile)

let (classifierEvaluationSlice, classifierTrainingSlice) = dataFrame.split(by: 0.20, seed: 5)


let classifierTrainingFrame = DataFrame(classifierTrainingSlice)
let classifier = try MLTextClassifier(trainingData: classifierEvaluationFrame, textColumn: "text", labelColumn: "sentiment"))

Additionally we can compare & print metrics and save file:

let classifierEvaluationFrame = DataFrame(classifierEvaluationSlice)
let metrics = model.evaluation(on: classifierEvaluationFrame, textColumn: "text", labelColumn: "sentiment"))
print(metrics.classificationError)
    
let modelPath = URL(filePath: "YourPath/YourModelName.mlmodel")
try model.write(to: modelPath)
Arsienij
  • 81
  • 6
  • Also I tried to do same with MLTextClassifier.DataSource but failed, it is really weird at moment. So if someone tried this before, share your solutions please. Thanks :) – Arsienij Jun 13 '23 at 15:52