- iOS 11.0+
- Xcode 9.0+
- Core ML
This sample app uses a trained model,
Mars, to predict habitat prices on Mars.
Add a Model to Your Xcode Project
Add the model to your Xcode project by dragging the model into the project navigator.
You can see information about the model—including the model type and its expected inputs and outputs—by opening the model in Xcode. In this sample, the inputs are the number of solar panels and greenhouses, as well as the lot size of the habitat (in acres). The output is the predicted price of the habitat.
Create the Model in Code
Xcode also uses information about the model’s inputs and outputs to automatically generate a custom programmatic interface to the model, which you use to interact with the model in your code. For
Mars, Xcode generates interfaces to represent the model (
Mars), the model’s inputs (
Mars), and the model’s output (
Use the generated
Mars class’s initializer to create the model:
Get Input Values to Pass to the Model
This sample app uses a
UIPicker to get the model’s input values from the user:
Use the Model to Make Predictions
Mars class has a generated
prediction(solar method that’s used to predict a price from the model’s input values—in this case, the number of solar panels, the number of greenhouses, and the size of the habitat (in acres). The result of this method is a
price property of
mars to get a predicted price and display the result in the app’s UI.
Build and Run a Core ML App
Xcode compiles the Core ML model into a resource that’s been optimized to run on a device. This optimized representation of the model is included in your app bundle and is what’s used to make predictions while the app is running on a device.