Search code examples
iosswiftcoremlcreateml

Deprecation Warning Use DataSource instead of MLDataTable when initializing in Create ML


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.


Solution

  • 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)