Deprecation Warning 使用 DataSource 而不是 MLDataTable 在初始化 Create ML 时。

huangapple go评论74阅读模式
英文:

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

问题

我在Xcode 14.3 Playgrounds中运行以下代码。我使用的是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")

我收到以下警告:

'init(trainingData:textColumn:labelColumn:parameters:)'在macOS 13.0中已被弃用:在初始化时使用DataSource而不是MLDataTable

问题在于没有关于如何创建DataFrame或DataSource的文档
英文:

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.

答案1

得分: 2

处理这个情况上的一些时间。我们可以使用DataFrame,这样就可以避免警告。目前还没有被弃用。

有一个示例,我找到了如何重写它的方法。

以前的版本:

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

更新后:

此外,添加这个

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: classifierTrainingFrame, textColumn: "text", labelColumn: "sentiment")

另外,我们可以比较和打印指标,并保存文件:

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)
英文:

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)

huangapple
  • 本文由 发表于 2023年5月25日 02:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定