How to Train a Core ML Model for an iOS App

What You’ll Be Creating

Core ML makes it easy for iOS developers to add deep machine learning to their apps. In this post, I’ll show you how you can train a Core ML model to derive intelligent insights.

Machine learning has undoubtedly been one of the hottest topics over the past year, with companies of all kinds trying to make their products more intelligent to improve user experiences and differentiate their offerings. Google invested between $20B and $30B in artificial intelligence just last year alone, according to McKinsey’s State Of Machine Learning And AI, 2017 .

AI is turning into a race for patents and intellectual property (IP) among the world’s leading tech companies…The report cites many examples of internal development including Amazon’s investments in robotics and speech recognition, and Salesforce on virtual agents and machine learning. BMW, Tesla, and Toyota lead auto manufacturers in their investments in robotics and machine learning for use in driverless cars. Toyota is planning to invest $1B in establishing a new research institute devoted to AI for robotics and driverless vehicles. (source: Forbes )

Apple is no exception to this trend, having utilized Machine Learning in their own apps. For example, the Photos app for iOS can recognize faces, objects and landmarks, and Siri infers intent and meaning from speech. Messages for iOS intelligently suggests and predicts words based on previous user behaviors.

In this tutorial, you will learn about how to apply machine learning algorithms to a set of training data, to create a trained model which will subsequently make predictions based on new input. All thanks to Apple’s new Core ML framework.

Objectives of This Tutorial

This tutorial will introduce you to a subset of Machine Learning. You’ll train and integrate a machine learning model in a simple iOS app, using a popular deep learning algorithm framework. In this tutorial, you will:

  • learn some of the basic Machine Learning concepts
  • train your model using sample data
  • integrate the trained model in an iOS app

After going through the theory of NLP, we’ll put our knowledge to practice by working through a simple twitter client, analyzing tweet messages. Go ahead and clone the tutorial’s GitHub repo and take a look at the final version of the app we will create from scratch.

Assumed Knowledge

This tutorial assumes you are a seasoned iOS developer, but although you will be working with machine learning, you don’t need to have any background on the subject. You’ll be using a bit of Python to create your trained model, but you can follow through the tutorial example without prior knowledge of Python.

Machine Learning 101

The goal of machine learning is for a computer to do tasks without being explicitly programmed to do so—the ability to think or interpret autonomously. A high-profile contemporary use-case is autonomous driving: giving cars the ability to visually interpret their environment and drive unaided.

Machine Learning is today leveraged by large companies to make better business decisions based on historical data, by using deep learning algorithms to identify patterns and correlations, which allow them to make better predictions of the future. For instance, you can resolve problems such as “How likely it is for a specific customer to purchase a specific product or service?” with greater confidence based on prior behavior.

Machine learning is best applied to problems where you have a history of answers, as you will discover later in this tutorial when we go through our sample problem. An example of machine learning in action would be your email spam filter, which uses supervised learning (as you flag items as spam or not) to better filter spam over time. The machine learning model encodes all of this knowledge about past results and makes it available to the algorithm for efficient use at run-time.

It may all sound a bit overwhelming at first, but it isn’t complicated, and we will walk you through how to create a trained model shortly. Once you have devised a trained model via an algorithm, you will then convert it to a model that can be consumed by iOS, thanks to Core ML.

Core ML is new to Apple’s family of SDKs, introduced as part of iOS 11 to allow developers to implement a vast variety of machine learning modes and deep learning layer types.

Natural Language Processing (NLP) logically sits within the Core ML framework alongside two other powerful libraries, Vision and GameplayKit . Vision provides developers with the ability to implement computer vision machine learning to accomplish things such as detecting faces, landmarks, or other objects, while GameplayKit provides game developers with tools for authoring games and specific gameplay features.

The benefits of CoreML compared to other solutions is that Apple has optimized machine learning to run on-device, which means reduced memory power consumption and reduced latency. This also containerizes user information within the device, improving privacy.

With an overview of Machine Learning and models out of the way, let’s put the theory into practice by creating your first training model.

Training your Model

In order for a model to be useful, it needs to be trained to recognize data as information that it can subsequently use to assert predictions with an appropriate algorithm. Core ML currently supports the following model types:

In addition to designating an algorithm, the more data you have the better your model will be trained, and the more accurate the predictions will be. Before we start creating our Core ML model, let’s take a look at the example app we will be working with, and in particular, the sample data.

Example App: Las Vegas Hotel Score Predictor

For this tutorial we are going to use an open-sourced set of data on hotel reviews in the Las Vegas Strip, which we had sourced from UCI to illustrate how to train a model and compute correlations. You can take a look at the complete set of comma-delimited CSV file that we will be using in our app. The structure of the data is as follows:

Nr. reviews,    Nr. hotel reviews,	Score,	Hotel name,	Hotel stars
11,	4,	5,	Circus Circus Hotel & Casino Las Vegas,	3
119,	21,	3,	Circus Circus Hotel & Casino Las Vegas,	3

We will be interested in predicting the hotel star ratings for hotels based on the correlation of number of hotel reviews, and general reviews, for each specific hotel, which is quite a contrived example but simple enough to illustrate the concept of training a model with straightforward data.

Download the comma-delimited CSV file into a new folder that you will use for this exercise. Now let’s go ahead and get our hands dirty with some Python, with the aim of accomplishing the following:

  • importing the necessary libraries, including the Python CoreML libraries
  • importing our sample data
  • applying a linear regression algorithm to our data, using a library called SciKit
  • identifying the columns in the data we are interested in modeling ( Nr. reviews , Nr. hotel reviews , Hotel stars )
  • identifying the column that it may be influencing ( Score )
  • converting the trained model into a CoreML model

It may seem like there are quite a few steps, but it is not as daunting as you may think. The Python code we will demonstrate next won’t be difficult to follow regardless of your experience with the language.

First, we are going to setup our required modules an dependencies, including SciKit , coremltools Apple’s official CoreML tools for python, and pandas , a powerful tool for data structure analysis.

Open up a terminal window, navigate to the project folder where you have the CSV file, and enter the following:

sudo -H pip install --ignore-installed coremltools scikit-learn pandas

Next, using an editor of your choice, create a new .py file, and name it something like , adding the following lines to import the libraries you will be using:

from sklearn.linear_model import LinearRegression
import pandas
import coremltools

Straight after the import statements, add the following:

data = pandas.read_csv("LasVegas-Trip-Dataset.csv") #(1) Import CSV file
model = LinearRegression() #(2) Apply linear regression
    data[["Nr. reviews", "Nr. hotel reviews", "Hotel stars"]], data["Score"]) #(3) Data to extrapolate

So far we are simply importing the CSV using the pandas framework, printing out the imported data to the screen, and then using the SciKit framework to establish a linear regression algorithm to apply to the columns we are interested in extrapolating. Don’t worry too much about what a linear regression algorithm means, but just know we are using a simple modeling algorithm technique to make predictions. In this project, we are interested in how it affects the score of our hotel, which we just established using the function.

We now have our trained model, but we still need to convert it into a model that Core ML can consume, which is where coremltools comes in. Insert the following lines of code:

coreml_model = coremltools.converters.sklearn.convert(
    model, ["Nr. reviews", "Nr. hotel reviews", "Hotel stars"], "Score") #(4) Convert"Vegas_Reviews.mlmodel") #(5) Export to a CoreML model object

The last two lines convert your model into a Core ML-compliant model before saving the result as an .mlmodel object, ready to be consumed in your Xcode project. Save the Python script and run it via terminal:


Presuming you haven’t encountered any errors, the Vegas_Reviews.mlmodel file will be generated, and your trained model primed to be imported into Xcode.

Integrating the Trained Model

For the second part of this tutorial, you are going to create a simple app with a single view controller, a few sliders, and a segment control to enable users to toggle various values, allowing you to observe various Core ML predictions. The final app will look close to the following:

In Xcode, create a new Single View App Swift project, and give it a name.

Next make sure you’ve included the generated Vegas_Reviews.mlmodel file in your project, by dragging it into your navigation project pane.

Now, open up the ViewController.swift file, and add the following:

class ViewController: UIViewController{
    let reviews = Vegas_Reviews()

    @IBOutlet weak var stackView: UIStackView!

    @IBOutlet weak var scoreValue: UILabel!

    @IBOutlet weak var nrReviews: UISlider!

    @IBOutlet weak var nrHotelReviews: UISlider!

    @IBOutlet weak var stars: UISegmentedControl!

The first thing you are doing is creating an instance of our model, which you will use to make predictions later in the class. You are also creating a few IBOutlet variables that you will wire up in the storyboard shortly, that map to the individual model properties we want to play around with.

Switch over to Storyboard, and add the corresponding controls we declared in your view controller, making sure you wire up each control to the view controller:

Switch back to the ViewController.swift file, and add the following @IBAction method:

@IBAction func makePrediction(_ sender: Any){
        let nrReviewsSelected = Double(nrReviews.value)
        let nrHotelReviewsSelected = Double(nrHotelReviews.value)
        var starsSelected: Double{
            switch stars.selectedSegmentIndex {
            case 0:
                return 3.0
            case 1:
                return 4.0
            case 2:
                return 5.0
                return 5.0

        if let predictions = try? nrReviewsSelected, Nr__hotel_reviews: nrHotelReviewsSelected, Hotel_stars: starsSelected){

            let scoreFormatter = NumberFormatter()
            scoreFormatter.numberStyle = .decimal
            scoreFormatter.maximumFractionDigits = 1
            self.scoreValue.text = scoreFormatter.string(for: predictions.Score)

This is the primary functional code of our prediction engine, so let’s dissect this method step-by-step. We first cast the various controls into the Double type, which will be passed as arguments when we call our predictions method. Within a try? block, call which is an auto-generated method belonging to our model, along with the expected properties we had defined when importing our trained model.

The result of the predictions block is then passed to the label ScoreValue , to display in you app. We are almost finished, just switch back once more to the storyboard and map each control’s valueChanged: property to the @IBAction method we created in the view controller. You want this method to be called every time you change a slider or segment value. And for good measure, you can also ensure that you also automatically call this method within your viewDidLoad() method so that it updates right from the start:

override func viewDidLoad() {

Build and run the app in Xcode, and in the Simulator toggle the various sliders and observe the score value prediction as it changes based on the other attributing factors of number of hotel reviews and reviews in general. As emphasized earlier, this is indeed a contrived example but it gives you an idea of how to construct your own experiments to correlate, and more importantly, how simple it is to implement trained models in iOS.


Thanks to Core ML in iOS 11, Apple has made it easy for everyday developers without a background in deep learning to be able to add intelligence into their apps. All the processing is done on-device, ensuring greater performance without the privacy concerns of storing data on the cloud. With Apple previously ‘dog-fooding’ its machine learning implementation on built-in apps like Photos and Mail, third-party developers now have the opportunity to recognize patterns, images and textual intent with only a few lines of code.

This is no doubt only the beginning of Apple’s Core ML movement, but it is a great opportunity for developers to start thinking more holistically about data. With Core ML we can provide users with better user experiences while providing product managers with greater business insights into user behaviors.

While you’re here, check out some of our other posts on iOS app development and machine learning!

  • Machine Learning

    Get Started With Image Recognition in Core ML

    Vardhan Agrawal

  • Security

    Secure Coding With Concurrency in Swift 4

    Collin Stuart

  • iOS SDK

    Updating Your App for iOS 11

    Doron Katz

  • Swift

    Get Started With Natural Language Processing in iOS 11

    Doron Katz

Tuts+ All稿源:Tuts+ All (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » How to Train a Core ML Model for an iOS App

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录