- Xcode 10.1+
- Create ML
- Create MLUI
The playground imports a CSV file, which contains Martian housing data, into a data table. The data table contains the following columns of information about a habitat on Mars:
Size (area in acres)
Number of greenhouses
Number of solar panels
The playground trains the regressor and classifier models, each with a group of columns relevant to that model. Once trained, the regressor is ready to predict the price of a habitat, and the classifier is ready to predict the purpose of a habitat.
The playground concludes by saving each model to a file, ready for integration into an app.
Import the Data
MLData initializer to import your data in to a data table. In this sample, the playground initializes its first data table with the contents of the CSV file embedded within the playground.
Pass your data table to
print() to see a pretty-printed sample of its contents in the console.
Isolate the Relevant Model Data
If necessary, generate a new data table that includes only relevant columns for your model.
For example, to predict price, the playground’s regressor needs only four columns of the five original columns:
The playground generates a new data table, specifically tailored for the regressor, by passing an array of column names to the first data table’s
To predict the purpose of a habitat, the classifier needs a similar group of columns:
Divide the Data for Training and Evaluation
If you intend to evaluate your model after training, reserve some of your data table’s rows for evaluation by separating them from the training data. By evaluating your model with data not present for training, your model’s evaluation better represents its real-world performance.
The playground creates two data tables per model, one for evaluation and the other for training, by using the
random method of
MLData. The method returns a tuple of two new data tables, each generated by randomly dividing the rows of the original data table. The size of the first data table in the tuple is determined by the
proportion parameter, a floating point value between
1. The second data table in the tuple contains the remaining data rows.
In this example, the playground sets aside 20% of each model’s data rows for evaluation, leaving the remaining 80% for training.
The amount of data your model needs for evaluation versus training will vary with each app. Generally, training your model with more examples leads to better performance.
Train the Regressor
The playground trains the regressor by providing its initializer with the training data table and the name of the target column. The
target parameter determines what information you want the model to provide in its predictions. The playground tells the regressor to predict the price of a habitat by specifying
price as the model’s target column.
During training, Create ML automatically sets aside a small percentage of the training data to use for validating the model’s progress during the training phase. The training process gauges the model’s performance based on the validation data. Depending on the validation accuracy, the training algorithm can adjust values within the model or even stop the training process, if the accuracy is high enough. Because the split is done randomly, you might get a different result each time you train a model.
Evaluate the Regressor
The playground evaluates the regressor’s performance by passing its evaluation data table.
If your regressor’s evaluation performance isn’t good enough, you may need to:
Train the Classifier
The playground trains the classifier to predict the purpose of a habitat. It does so by targeting a classifier on the
purpose column of its training data table.
A classifier can only predict values provided in its training data, unlike a regressor, which can predict numeric values beyond those in its training data. For example, the playground’s classifier can only predict a value of
"general" because those are the only values in the
Evaluate the Classifier
Just as with the regressor, the playground evaluates the classifier’s performance by passing its evaluation data table.
If your classifier’s evaluation performance isn’t good enough, you may need to:
Save the Model
When you are satisfied with your modelʼs performance, save it to a file for later use in your app. The playground saves the price regressor, along with metadata, to the user’s desktop with its
The playground similarly saves the purpose classifier to the user’s desktop.
Add the Model to an App
To add your model to an app, see Integrating a Core ML Model into Your App.