Search code examples
swiftmachine-learningcreateml

Creating Data Tables for Machine Learning in Swift using CreateML


Apple has recently released a framework that allows to create machine learning models. I am interested in tabular data but I haven't found any example online. Could anyone please provide a piece of code that works? I have tried the following provided by Apple without success:

import CreateML

// Specify Data
let trainingCSV = URL(fileURLWithPath: "/Users/createml/HouseData.csv")
let houseData = MLDataTable(contentsOf: trainingCSV)
let (trainingData,testData) = houseData.randomSplit(by: 0.8, seed: 0)

// Create Model
let pricer = try MLRegressor(trainingData: houseData, targetColumn: "price")

// Evaluate Model
let metrics = try pricer.testingMetrics(on: testData)

// Save Model
try pricer.write(to: URL(fileURLWithPath: "/Users/createml/HousePricer.mlmodel"))

In particular this code throws me the following error on line 4 and 15:

error: MyPlaygroundu.playground:9:22: error: use of unresolved identifier 'URL' try pricer.write(to: URL(fileURLWithPath: "/Users/createml/HousePricer.mlmodel"))


Solution

  • URL is from the Foundation framework.

    Add:

    import Foundation
    

    just before import CreateML.