我正在从事一个涉及 CreateML 和 MLLinearRegressor 的项目。出于某种原因,每当我尝试预测训练数据中不存在的值时,每次都会得到相同的预测。这在 Swift Playgrounds 和在 Xcode 项目中使用模型时都会发生。为什么会发生这种情况?我在下面发布了我的 Swift Playgrounds 代码。
import CreateML
import CoreML
import Foundation
do {
let data: [String: MLDataValueConvertible] = [
"Processor Name": ["A6", "A7", "A8", "A8X", "A9", "A9X", "A10X", "A10X", "A11"],
"Geekbench Singlecore": [754, 1325, 1660, 1796, 2522, 3052, 3463, 3909, 4219]
]
let CPURegressor = try MLLinearRegressor(trainingData: MLDataTable(dictionary: data), targetColumn: "Geekbench Singlecore", featureColumns: ["Processor Name"])
let testData: [String: MLDataValueConvertible] = [
"Processor Name": ["A6", "A7", "A8", "A8X", "A9", "A9X", "A10X", "A10X", "A11", "A12"],
"Geekbench Singlecore": [754, 1325, 1660, 1796, 2522, 3052, 3463, 3909, 4219,0]
]
print(try CPURegressor.predictions(from: MLDataTable(dictionary: testData))) // Notice how last (A12) and first (A6) values are the same
} catch {
print(error)
}
Processor Name
更新:这是调整类别后我的代码的样子
import CreateML
import CoreML
import Foundation
do {
let data: [String: MLDataValueConvertible] = [
"Processor Name": [6.0, 7.0, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0],
"Geekbench Singlecore": [754, 1325, 1660, 1796, 2522, 3052, 3463, 3909, 4219]
]
print(try MLDataTable(dictionary: data))
let CPURegressor = try MLRegressor(trainingData: MLDataTable(dictionary: data), targetColumn: "Geekbench Singlecore", featureColumns: ["Processor Name"])/*, parameters: MLBoostedTreeRegressor.ModelParameters(validationData: nil, maxDepth: 1000,
maxIterations: 1000,
minLossReduction: 1))*/
/*CPURegressor.modelParameters = MLImageClassifier.ModelParameters(featureExtractor: .scenePrint(revision: 1),
validationData: nil,
maxIterations: 30,
augmentationOptions: [])*/
/* let testData: [String: MLDataValueConvertible] = [
"Processor Name": [0, 1, 2, 3, 4, 5, 6, 7, 8, 14],
"Geekbench Singlecore": [1325, 1660, 1796, 2522, 3052, 3463, 3909, 4219,0, 1325]
]
print(try CPURegressor.predictions(from: MLDataTable(dictionary: testData))) // Notice how last (A12) and first (A6) values are the same*/
} catch {
print(error)
}